Skip to content

Remove exec magic from sasdata.quantities.units#204

Open
rprospero wants to merge 979 commits into
refactor_24from
units_without_exec
Open

Remove exec magic from sasdata.quantities.units#204
rprospero wants to merge 979 commits into
refactor_24from
units_without_exec

Conversation

@rprospero
Copy link
Copy Markdown
Contributor

My previous work on refactoring out the units module depended heavily on writing a module at run time and then running the code through exec. While this removed the need to keep _build_tables.py and units.py in sync, it was also very ugly code that could be very frightening to edit.

This PR produces a modified version that edits an exiting units module instead of trying to write a new one from scratch.

  • cleaner and easier to read than old code
    • No use of StringIO
    • No use of exec
    • Easier to follow data paths (e.g. the fid.write(f" '{name}': {name}") pattern obscures the actual value in the dictionary).
  • units module code isn't run if units module isn't called (the exec version would build the units module the moment someone imported sasdata.quantities instead of sasdata.quantities.units)

Again providing guidance:

  • Everything before line 899 of units.py was just moved directly from __init__.py
  • The variable name this was chosen to match convention from the Javascript community. I had also considered using self, which is more Pythonic, but had the potential to be confusing, since it is usually attached to a class method.
  • The remaining code is still a modification of units.py. However, values are defined directly and moved into the module through setattr.

rprospero and others added 21 commits March 17, 2026 11:33
This is clearer than ArbitraryUnit, which might be confused for
uncalibrated scattering data.
Current the only invalid characters are Space, /, and ^.  I've also
refactored the argument parsing to remove duplication between the
numerator and denominator.
* Simplify quotes in table building

* Dynamically create modules

* Descriptive comments

* Ruff formatting

* [pre-commit.ci lite] apply automatic fixes for ruff linting errors

* Tell windows that code is in utf8

* Removes unused module

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: PaulSharp <44529197+DrPaulSharp@users.noreply.github.com>
codescene-delta-analysis[bot]

This comment was marked as outdated.

codescene-delta-analysis[bot]

This comment was marked as outdated.

@rprospero rprospero force-pushed the units_without_exec branch from 10a93d9 to e732111 Compare May 14, 2026 16:18
Copy link
Copy Markdown

@codescene-delta-analysis codescene-delta-analysis Bot left a comment

Choose a reason for hiding this comment

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

No quality gates enabled for this code.

See analysis details in CodeScene

Quality Gate Profile: Custom Configuration
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

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.

6 participants