Skip to main content

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

Metadata

Release files for uniter 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uniter 1.0.0
File Size Uploaded
uniter-1.0.0.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uniter 1.0.0
File Interpreter ABI Platform
uniter-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.5 kB

Release files / uniter-1.0.0.tar.gz

Download URL uniter-1.0.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7e7b34d24521a1b3858955bb41c3da5fcffb789c7ff99658ed2e6430ae1b1aff
BLAKE2b-256 checksum
How to use checksums
b9dd8b6f3590799c83e758a1125057bbfde17843ea8ff0ed75e4b83677a899f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / uniter-1.0.0-py3-none-any.whl

Download URL uniter-1.0.0-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2569c29b6e4c435551608f8ee49fdecd154ebc945c1855561a810b1a8fa70ef3
BLAKE2b-256 checksum
How to use checksums
abdc53d5277649c275c15ee0592dc6deca2607e7fb492b703d33292ad605b174
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page