haimkastner / unitsnet-py Goto Github PK
View Code? Open in Web Editor NEWA better way to hold unit variables and easily convert to the destination unit
Home Page: https://pypi.org/project/unitsnet-py/
License: MIT License
A better way to hold unit variables and easily convert to the destination unit
Home Page: https://pypi.org/project/unitsnet-py/
License: MIT License
There is a duplicate comparison logic for every measure entity (methods __eq__
, __lt__
).
Those could be extracted into a common abstract class AbstractMeasure
.
Thank you for the package! This is exactly what I was looking for with units as objects!
However, I would like to use it on my projects using Python 3.8, while your package has lower bound for Python >= 3.10.
It seems to me that the package can easily be extended to support earlier versions of python.
At least, I tried and build the package for Python 3.8 and then was able to install it and use successfully.
I noticed that the code generation section uses type annotations compatible with Python>=3.9 only, but they could simply replaced with 3.8-compatible counterparts, such as from typing import List
.
It seems that it is necessary to re-consider allowed operations for the measurement objects.
Consider this example:
>>> from unitsnet_py import Length
>>> length = Length.from_meters(10) * Length.from_centimeters(30)
>>> length.base_value
3.0
>>> length.centimeters
300.0
>>> length.meters
3.0
We actually multiplied 10 meters by 30 centimeters to obtain 3 squared meters.
However the object length
is treated as 3 meters, not squared meters.
Maybe it will be a good idea to ban multiply, divide, mod and pow operations for now.
IMHO, it is necessary to create specialized classes for these operations:
class Multiply(Measure):
def __init__(self, measure_1: Measure, measure_2: Measure):
self.measure_1 = measure_1
self.measure_2 = measure_2
def convert(self, multiply_unit: MultiplyUnits) -> float:
return (
self.measure_1.convert(multiply_unit.unit_1)
* self.measure_2.convert(multiply_unit.unit_2)
)
@property
def base_value(self) -> float:
return self.measure_1.base_value * self.measure_2.base_value
class MultiplyUnits:
def __init__(self, unit_1: Unit, unit_2: Unit):
self.unit_1 = unit_1
self.unit_2 = unit_2
It is impossible to instantiate an object from numpy array due to the check in __init__
:
https://github.com/haimkastner/unitsnet-py/blob/670627d586af35d7b799783c9ec34d41065a518c/unitsnet_py/units/volume.py#L293C1-L294D1
Without the check the conversion actually works for numpy arrays, since the operations involved are all compatible with numpy arrays.
I suggest that the check on math.isnan
is removed from the __init__
.
Instead the code would be throwing type error during runtime if unable to perform math operations.
As a user of this library I would like to use Units
enum explicitly to convert measurements to.
For example:
length = Length(1, LengthUnits.Meter)
centimeters = length.convert(LengthUnits.Centimeter)
assert centimeters == length.centimeters
This is useful when we select the unit to convert to at runtime.
I was thinking of several options to call conversion method.
convert
- looks OK, probably will be well understood by most usersas
and in
- probably sound more like plain English (i.e., length.in(LengthUnits.Centimeter)
), but these are reserved words in Pythonto
- middle ground between convert
and as/in
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.