REFACTOR: Update register unit functionality#532
Conversation
seanpearsonuk
left a comment
There was a problem hiding this comment.
Approving subject to checks by @mkundu1. Thanks!
|
@hpohekar The following code gives some error: I need a consistent way of accessing units when I'm using the custom registry. |
There's no need to rush to a solution here. We could alternatively just add micron for now, and revisit improvements later. |
…/pyansys-units into fix/refactor_register_unit
@mkundu1 I have refactored to fix this. Please confirm. Thank you. |
@hpohekar This is working fine now, thanks |
This pull request introduces significant improvements to the
UnitRegistryclass, focusing on more flexible and robust custom unit registration, improved string-based lookup, and clearer error handling for unit name collisions. The documentation and tests are updated to reflect these changes, making it easier for users to register and access custom units both at construction and after instantiation.Key changes:
Custom Unit Registration & Lookup
custom_unitsparameter to theUnitRegistryconstructor, allowing users to register custom units at the time of registry creation using a list of dictionaries. This enables more convenient and declarative unit setup. [1] [2] [3] [4]get_unitmethod and aQuantitymethod onUnitRegistryfor string-based lookup and creation of quantities using custom units, improving usability and consistency.Name Collision & Error Handling
UnitAlreadyRegisteredexception with a more specificUnitNameAlreadyRegisteredexception, used consistently throughout the codebase for name-only collision detection (i.e., duplicate unit names, including built-ins). The check is now explicitly name-based, allowing equivalent units with different names. [1] [2] [3] [4] [5] [6] [7] [8]Documentation Updates
These changes make it easier and safer to extend the unit system with custom definitions, while providing clear feedback and documentation for users.