feat: added formula api to declare unit values#109
Conversation
The formula api accessible via `declare_unit` allows users to build a quantity from a numeric value and an SI unit expression (in string). Example: ```python from si_units import declare_unit, KILO, WATT, HOUR declare_unit(5.0, "kWh") == 5.0 * KILO * WATT * HOUR True ```
|
Interesting addition. I wonder if it makes more sense to have this in I could clean that up and we can expose it to python. The interface could be the same as yours. let p: Pressure = "1.3 N/m^2".parse()?;
let p: Pressure = "15 bar".parse()?;
let mw: MolarWeight = "15 g/mol".parse()?;
let v: Velocity = "9.81 m/s".parse()?;
let a: Acceleration = "9.81 m/s^2".parse()?;@prehner what do you think? Is this in scope for this crate? |
|
@g-bauer I was thinking about the same thing. Just thought these kind of string parsing is more of a Python (Pythonic?) thing -- unyt has a similar API, though it is overloaded under object init, which I always find to be really awkward (think this is more elegant). I think this is more for Python projects where presentation to or collaboration with less programming-oriented stakeholders are required. You know, demo in Jupyter notebooks etc. Not sure if the same API is as useful if you are working with a compiled language like Rust though. Supposed importing the const directly provides better type clarity than an adhoc declaration with strings. Really depends on your use case I suppose? I leave this to your better judgement. |
The formula api accessible via
declare_unitallows users to build a quantity from a numeric value and an SI unit expression (in string).Example:
This one is just a quality of life feature that I think would be quite useful. Also apologies if it seems like I did a massive edit on
__init__.pyI have auto-lint on save setup for my IDE and the linter dislike the non-alphabetically sorted imports. Aside from reordering the imports, all the edit realistically do is exposedeclare_unit.