Skip to main content

Package to handle unit conversion.

Project description

Uniter

Is python package, that can handle unit conversion with easy syntax. Comes with ability to perform mathematical operations, and parsing from human-readable string.

Features

Syntax

Here are some examples of features, including bad one to show you what you should not do while using all of these aspects.

Conversion

from uniter.units import *

print(DM(80).convert_to(M))  # prints out 8m in Unit object
print(DM(80)[M])  # same thing but shorter syntax
print(DM(80)[KG])  # raises TypeError: Illegal conversion from Length to object 

Addition / Subtraction

  • Converts to last used unit in calculation
from uniter.units import *

print(KM(50) + M(30))  # prints out 5030m in Unit object
print(KM(50) - M(30))  # prints out 4070m in Unit object
print(KM(50) - KG(30))  # raise TypeError: Subtraction of non-equal units (Length - Mass)

Multiplication / Division / Floor division

  • One of mul/div values needs to be int/float, not Unit type
from uniter.units import *

print(KM(30) * 2)  # prints out 900km in Unit object
print(KM(30) * 80)  # prints out 2400km in Unit object
print(KM(30) / 6)  # prints out 5km in Unit object
print(KM(8) // 6)  # prints out 1km in Unit object
print(KM(8) * KG(6))  # TypeError: Multiplication of Unit with KG, use int/float instead!

Power

from uniter.units import *

print(KM(2) ** 16)  # prints out 65536km in Unit object
print(KM(2) ** KM(3))  # Power of Unit with KM, use int/float instead!

Parsing string to Uniter units

import uniter

print(uniter.parse("2m + 5km"))  # returns 5.002km in Unit object

Custom quantity/units

Before creation of your custom units, you need to know that units are structurized as inheritance of Unit (Base Class) -> {Your name of unit here} -> {Name of unit}. This structure can basically handle that you cannot convert your unit to another incompatible units.

Example of structuring Length and Mass quatities

Unit
├─── Length
│    ├─── KM
│    ├─── M
│    ├─── DM
│    ├─── CM
│    └─── ...
└─── Mass
     ├─── KG
     ├─── G
     ├─── MG
     └─── ...

Now for creation, for making basic units using prefixes like k (kilo), m (mili), M (mega) etc. you can just use defined decorators in Uniter.py file like that:

from uniter.Uniter import Unit, Unitor, Quantitor, UnitType


# In this example I am using keywords arguments to make it clearer for you

# Creates physics quantity of your name and sign
@Quantitor(sign="EQ")
class ExampleQuantity(Unit): pass


# Creating unit of out ExampleQuantity quantity:
#
# - Multiplier is difference between this unit and default one
#
# - IF MULTUPLIER IS 1 THEN THIS UNIT IS CONSIDERED AS DEFAULT
#
# - I've also defined unit type, this is not required, 
#   it is used to filter out specific type of units
@Unitor(symbol="fU", mp=1, unit_type=UnitType.METRIC)
class FirstU(ExampleQuantity): pass

Add custom calculation method

If you are trying to make quantity with different type of calculation, than a regular units multiplication or division, you can define it via __conv__(self, unit) method which returns float.

Here i will show you piece of my code from Angle.py:

from uniter.Uniter import Unit, Quantitor


@Quantitor("°")
class Angle(Unit):
  def __conv__(self, unit):
    from math import pi, degrees, radians
    DEGS = [DEG, MOA, SOA]
    if self.__class__ in DEGS and unit in DEGS:
      return super().__conv__(unit)  # type: ignore
    elif self.__class__ in DEGS and unit is RAD:
      return radians(float(self[DEG]))
    elif self.__class__ is RAD and unit in DEGS:
      return float(DEG(degrees(float(self)))[unit])


...  # other units

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uniter-1.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

uniter-1.0.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file uniter-1.0.0.tar.gz.

File metadata

  • Download URL: uniter-1.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for uniter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7e7b34d24521a1b3858955bb41c3da5fcffb789c7ff99658ed2e6430ae1b1aff
MD5 1bac5b9d6e46352842e108556374822b
BLAKE2b-256 b9dd8b6f3590799c83e758a1125057bbfde17843ea8ff0ed75e4b83677a899f2

See more details on using hashes here.

File details

Details for the file uniter-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: uniter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for uniter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2569c29b6e4c435551608f8ee49fdecd154ebc945c1855561a810b1a8fa70ef3
MD5 89140b4bd49cf5bf6fe0740b890b181a
BLAKE2b-256 abdc53d5277649c275c15ee0592dc6deca2607e7fb492b703d33292ad605b174

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page