Skip to content

Commit

Permalink
do not treat all as special keywords;
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoqing committed May 5, 2022
1 parent 03d84b1 commit 4acf348
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions miss_hit/mh_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ def identifier_visitor(self, node: Identifier, n_parent, relation):
# Python and Matlab keywords set are different. Luckily, Matlab forbids identifier start with `_`.
prefix = '_' if value in (
*keyword.kwlist, # python keyword can not be overwritten
'all', 'any', "slice", "eval", # python build-in names do not suggest overwriting
'I', 'M', 'C', # mat2py keywords
) else ''
self[node] = f'{prefix}{value}'
Expand Down Expand Up @@ -656,7 +655,7 @@ def process_one_file(path: [Path, str], options=None, mh=None):
raise SyntaxError(msg.message)


def process_one_block(src: str, inline=True):
def process_one_block(src: str, inline=True, format=False):
with NamedTemporaryFile('w', suffix='.m') as f:
f.write(src)
f.flush()
Expand All @@ -670,7 +669,9 @@ def process_one_block(src: str, inline=True):
"--python-alongside",
f.name])
if inline is True:
options.inline_mode = inline
options.inline_mode = True
if format is True:
options.format = True

process_one_file(f.name, options)
return target_path.read_text()
Expand Down
11 changes: 7 additions & 4 deletions util/convert_builtin_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ def main():
if body.strip() != "":
body = body + "\n"

build_in_funcs.setdefault(sub_toolbox, []).append(
build_in_funcs.setdefault(sub_toolbox, []).append((name,(
f"def {name}(*args):\n"
f"{body}"
f' raise NotImplementedError("{name}")\n'
)
)))

for k, v in build_in_funcs.items():
(Path(options.output) / k.parent).mkdir(parents=True, exist_ok=True)
Expand All @@ -120,10 +120,13 @@ def main():
"# type: ignore \n"
"from mat2py.common.backends import numpy as np\n"
"from ._internal.array import M \n"
"from ._internal.helper import argout_wrapper_decorators \n"
"from ._internal.helper import mp_argout_wrapper_decorators \n"
""
)
fd.write("\n".join(v))
name, code = zip(*v)
name = ', '.join(f'"{n}"' for n in name)
fd.write(f'__all__ = [{name}]\n\n')
fd.write("\n".join(code))


if __name__ == "__main__":
Expand Down

0 comments on commit 4acf348

Please sign in to comment.