3 ms·
I regularly use "pint" for Python: https://pint.readthedocs.io/en/stable/ https://pint.readthedocs.io/en/stable/ I'm sure it's not as good as a true type syst
by heisenzombie 5y ago
I regularly use "pint" for Python:
https://pint.readthedocs.io/en/stable/ https://pint.readthedocs.io/en/stable/
I'm sure it's not as good as a true type system could be, but it works pretty transparently due to Python's duck typing.
- techdragon 5y agoI’ll second the utility of “pint” for anything unit or quantity related in Python. I’m literally in the middle of writing a rate limited continuously run service wrapper around some existing one shot task code and while I initially thought about just parsing the rate definition out myself because it’s pretty simple to just split on “/“ or “per”, but after thinking through the usage scenarios I just ended up using pint to convert anything it can understand as a frequency. So anything of the form x / time unit or x per time unit, and converting it to the appropriate scale for use by the rest of my code. A whole bunch of string matching code replaced by a one line (two if I count the defensive unit type check to prevent false positive parsing problems) that looks as simple as this… default_sleep_interval = (1/ureg(default_rate)).to(“seconds”).magnitude