Skip to content

feat: added formula api to declare unit values#109

Open
dhfwc18 wants to merge 1 commit into
itt-ustutt:masterfrom
dhfwc18:feat/python-formula-api
Open

feat: added formula api to declare unit values#109
dhfwc18 wants to merge 1 commit into
itt-ustutt:masterfrom
dhfwc18:feat/python-formula-api

Conversation

@dhfwc18
Copy link
Copy Markdown
Contributor

@dhfwc18 dhfwc18 commented Apr 23, 2026

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:

from si_units import declare_unit, KILO, WATT, HOUR
declare_unit(5.0, "kWh") == 5.0 * KILO * WATT * HOUR
True

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__.py I 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 expose declare_unit.

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
```
@g-bauer
Copy link
Copy Markdown
Contributor

g-bauer commented Apr 23, 2026

Interesting addition. I wonder if it makes more sense to have this in quantity so that we can have unit-checked parsing from a string there as well (and in serde so that we can use SI units to parse JSON). I have a local branch that enables parsing via winnow.

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?

@dhfwc18
Copy link
Copy Markdown
Contributor Author

dhfwc18 commented Apr 23, 2026

@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.

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