Skip to content

replace evil eval with asteval#28

Merged
DanPorter merged 7 commits intomasterfrom
replace_evil_eval_with_asteval
Jul 29, 2025
Merged

replace evil eval with asteval#28
DanPorter merged 7 commits intomasterfrom
replace_evil_eval_with_asteval

Conversation

@DanPorter
Copy link
Copy Markdown
Collaborator

Add asteval as a dependency and add some new behaviours for the eval_hdf function.

This provides a fix for #27

Main changes

eval_functions.py

  • eval_hdf refactored
  • add asteval functions, replacing eval
  • added d_name pattern to generate_namespace
  • change find_identifiers to use astval_get_ast_names
  • defaults in generate_namespace are no longer generated for builtins
  • prepare_expression() added

hdfmap_class.py

  • added call('expression') behaviour

pyproject.toml

  • added asteval

examples

  • added example_eval_hdf.py

docs

  • various changes to README.md, docs

tests

  • added additional tests to test_hdfmap_class.py for eval patterns
  • np. removed in expressions as asteval populates namespace with numpy functions
  • all tests pass

DanPorter and others added 2 commits July 22, 2025 16:04
eval_functions.py
 - add asteval functions, replacing eval
 - change find_identifiers to use astval_get_ast_names
 - defaults in generate_namespace are no longer generated for builtins
 - prepare_expression() added
 - eval_hdf refactored

pyproject.toml
 - added asteval

tests
 - eval 'np.max' changed to 'max' in tests
 - tests pass
eval_functions.py
 - added d_*name* pattern to generate_namespace
 - added to doc strings about this
 - removed check_unsafe_eval as no longer required
 - added generate_image_roi_slice() although not currently used.
 - added create_interpreter() although not currently used

hdfmap_class.py
 - added __call__('expression') behaviour

examples
 - added example_eval_hdf.py

docs
 - various changes to README.md, docs

tests
 - added additional tests to test_hdfmap_class.py for eval patterns
 - corrected test in test_edge_cases.py for asteval
 - all tests pass
@DanPorter DanPorter requested a review from PeterC-DLS July 24, 2025 12:28
@DanPorter DanPorter self-assigned this Jul 24, 2025
@@ -261,56 +252,58 @@ def generate_namespace(hdf_file: h5py.File, hdf_namespace: dict[str, str], ident
for name in identifiers
if name.startswith('s_') and hdf_namespace.get(name[2:]) and hdf_namespace[name[2:]] in hdf_file
Copy link
Copy Markdown
Collaborator

@PeterC-DLS PeterC-DLS Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a function and re-use it. Also you can simplify to hdf_namespace.get(name[:2]) in hdf_file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks!
See 3223bde

also:
 - changes to docs
 - added example eval_performance_time.py
@DanPorter
Copy link
Copy Markdown
Collaborator Author

Thanks very much @PeterC-DLS for the review! I'll merge this now.

@DanPorter DanPorter merged commit 2f203f6 into master Jul 29, 2025
10 checks passed
@DanPorter DanPorter deleted the replace_evil_eval_with_asteval branch July 29, 2025 10:06
@DanPorter DanPorter mentioned this pull request Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants