Skip to content

Add v0.2.0 features: discovery API, caching, constants, and Jupyter support - #6

Merged
0-k merged 5 commits into
masterfrom
claude/explore-complete-project-Jx508
Feb 15, 2026
Merged

Add v0.2.0 features: discovery API, caching, constants, and Jupyter support#6
0-k merged 5 commits into
masterfrom
claude/explore-complete-project-Jx508

Conversation

@0-k

@0-k 0-k commented Feb 15, 2026

Copy link
Copy Markdown
Owner

Summary

This release adds significant quality-of-life improvements to EnergyUnits, including a discovery API for exploring available units and substances, performance optimizations through caching, IDE-friendly unit constants, and rich Jupyter notebook support.

Key Changes

Discovery API

  • Added Quantity.list_units(dimension=None) to list available units, optionally filtered by dimension
  • Added Quantity.list_dimensions() to list all available dimensions (ENERGY, POWER, MASS, etc.)
  • Added Quantity.list_substances(has_property=None) to list substances with optional property filtering
  • Added Quantity.list_currencies() to list supported currencies
  • Added SubstanceRegistry.get_properties(substance_id) to retrieve all properties for a substance
  • Added corresponding methods to UnitRegistry for direct registry access

Performance Optimization

  • Implemented @lru_cache(maxsize=256) on UnitRegistry.get_dimension() and get_conversion_factor() for up to 10x speedup on repeated conversions
  • Cache is automatically invalidated when custom units are loaded via load_units()

Unit Constants Module

  • Created new energyunits.units module with IDE-friendly string constants
  • Enables autocompletion and typo prevention: from energyunits.units import MWh, GJ, USD
  • Constants work identically to raw strings while providing better developer experience

Subtraction Operator

  • Implemented __sub__() method on Quantity class
  • Supports automatic unit conversion like addition
  • Preserves substance and basis metadata when operands match

Jupyter/IPython Support

  • Added _repr_html_() method for rich HTML display in notebooks
  • Color-coded badges for substance, basis, and reference year metadata
  • Handles both scalar and array values with appropriate formatting

Improved Error Messages

  • Unknown unit names now suggest close matches via difflib.get_close_matches()
  • Unknown substance names provide similar suggestions
  • Cross-dimensional conversion errors hint about specifying a substance
  • Substance conversion errors include usage examples

Substance Metadata Handling

  • Enhanced __add__() and __sub__() to properly handle None values for substance/basis
  • Added warnings when combining quantities with different substances
  • Warnings are only raised when both operands have non-None, non-matching substances

Implementation Details

  • Updated UnitRegistry.get_dimension() to provide helpful error messages with suggestions
  • Modified SubstanceRegistry.__getitem__() to suggest close matches on lookup failure
  • Enhanced ExchangeRateRegistry.get_supported_currencies() to return sorted list
  • Updated documentation and examples to showcase new discovery API and unit constants
  • Added comprehensive test suite (test_v020_features.py) with 83 tests covering all new features
  • Updated version to 0.2.0 in pyproject.toml and __init__.py
  • Enhanced README with new feature descriptions and usage examples
  • Updated ARCHITECTURE.md with new module structure and statistics

Testing

Added 83 new tests in test_v020_features.py covering:

  • Conversion caching behavior and cache info access
  • Unit and substance discovery with filtering
  • Unit constants functionality
  • Substance mismatch warnings
  • Subtraction operations with unit conversion
  • Jupyter HTML representation
  • Improved error messages with suggestions

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm

…pyter repr

Add conversion factor caching (@lru_cache) for up to 10x speedup on
repeated lookups. Add unit/substance/currency discovery methods
(Quantity.list_units(), list_substances(), list_currencies(),
list_dimensions()). Add unit constants module (energyunits.units) for
IDE autocompletion. Implement __sub__ operator. Add _repr_html_ for
rich Jupyter display. Improve error messages with close-match
suggestions and conversion hints. Warn on substance metadata loss
during arithmetic with mixed substances. 291 tests passing.

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm
README: fix inaccurate substance list (removed nonexistent biogas,
ethanol, steel, cement, aluminum), add v0.2.0 features (discovery API,
unit constants, subtraction, Jupyter repr, currency conversions),
document all 5 currencies, add time units section, fix License heading.

ARCHITECTURE: bump to v0.2.0, update component diagram with
ExchangeRateRegistry and units module, update test count (291+),
mark caching as implemented, update future enhancements roadmap.

Examples: add discovery_api(), unit_constants_example(),
subtraction_example(), currency_conversion_example(). Fix ugly
manual .unit assignment pattern. Import unit constants.

Fix duplicate USD in ExchangeRateRegistry.get_supported_currencies().

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm
1. Precision: replace hardcoded 0.2778 with 1/3.6 in heating value
   conversions (registry.py). Eliminates ~0.008% systematic error
   that compounds in large-scale calculations.

2. Data: fix wood_pellets LHV from 17.0 to 18.5 MJ/kg to match
   DATA_SOURCES.md documented value and physical expectations
   (HHV/LHV ratio was 0.85, now correct 0.925).

3. Security: escape user-provided strings (substance, basis, unit)
   in _repr_html_() to prevent HTML injection in Jupyter notebooks.

4. Consistency: align substance metadata logic in __add__/__sub__
   with __mul__/__truediv__ — preserve substance when one operand
   has None instead of always dropping it.

5. Bug: renewable CO2 conversion no longer hardcodes "t" unit —
   preserves source mass unit (e.g., "kg" stays "kg").

6. Logic: currency detection now uses component-based matching
   instead of substring matching, preventing false positives
   (e.g., "EUR" no longer matches inside "NEURON").

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm
- Add Fixed section to CHANGELOG with all v0.2.0 bug fixes
- Fix pyproject.toml license format for twine compatibility
- Add License classifier
- Replace non-existent readthedocs URL with ARCHITECTURE.md link

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm
- Update conf.py: furo theme, autodoc, intersphinx, myst, version from __init__
- Add API reference page with autodoc for all public classes
- Add architecture and changelog pages (include from root .md files)
- Add .readthedocs.yaml with Python 3.11 and pip install
- Update docs/requirements.txt with furo and myst-parser
- Point pyproject.toml Documentation URL to readthedocs

https://claude.ai/code/session_015zwEzhrnDuP91JXPxwMkJm
@0-k
0-k merged commit d3aad26 into master Feb 15, 2026
5 checks passed
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