Coder Social home page Coder Social logo

haimkastner / unitsnet-py Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 3.0 1.52 MB

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

Python 99.51% Jinja 0.49%
conversion converter measure measurements measures quantities unit unit-converter units units-of-measure

unitsnet-py's People

Contributors

factoryal-marcopanato avatar haimkastner avatar ivanovmg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

unitsnet-py's Issues

Duplicate comparison logic

There is a duplicate comparison logic for every measure entity (methods __eq__, __lt__).
Those could be extracted into a common abstract class AbstractMeasure.

Python 3.8 support

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.

Probably need to reconsider allowed operations

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

Numpy array support

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.

Public method to convert to other unit

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 users
  • as and in - probably sound more like plain English (i.e., length.in(LengthUnits.Centimeter)), but these are reserved words in Python
  • to - middle ground between convert and as/in

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.