Skip to content

Expose value and unit in python.#102

Merged
g-bauer merged 2 commits intoitt-ustutt:masterfrom
volubepossum:master
Mar 12, 2026
Merged

Expose value and unit in python.#102
g-bauer merged 2 commits intoitt-ustutt:masterfrom
volubepossum:master

Conversation

@volubepossum
Copy link
Contributor

As of now the direct attributes are not exposed at all in python, so the only way of getting them is through parsing the string representation, which needless to say is very tedious, especially for the units.

I ran into the issue while trying to store, read, then use the dimensioned values for further computation. An example use-case of this feature could be like this:

from si_units import *
import json

foo = 3 * METER
bar = json.dumps({"value": foo.value, "unit": foo.unit})
baz = json.loads(bar)
qux = SIObject(baz["value"], baz["unit"])
print(qux)

@prehner
Copy link
Contributor

prehner commented Mar 8, 2026

Hi, thank you for the suggestion! Internally, we use the __getnewargs__ function for these kind of use cases. However, I agree that having simple getters would be useful for cases like serialization in JSON. @g-bauer what do you think?

Just one small comment on the implementation: PyO3 provides descriptors that you can use to generate getters:

#[pyo3(get)]
value: Py<PyAny>,

For the unit, this does not work, but you can replace the getter for the value this way.

@g-bauer
Copy link
Contributor

g-bauer commented Mar 11, 2026

I think that's a useful addition! Looks good to me.

@g-bauer g-bauer merged commit 3bd6e2a into itt-ustutt:master Mar 12, 2026
21 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.

3 participants