Skip to main content

Simple Unit (Python implementation)

example workflow

Anaconda-Server Badge Anaconda-Server Badge Anaconda-Server Badge Anaconda-Server Badge Anaconda-Server Badge

PyPI repository Badge

This package is the Python Reference Implementation (RI) of the Simple Unit specification. Nevertheless, it also contains some extensions to the specification standard.

Standard usage

The standard usage refers to methods and classes defined in the Simple Unit specification.

Usage of transformed units:

from simpleunit import FundamentalUnit

m = FundamentalUnit()
km = m.scale_multiply(1000)
cm = m.scale_divide(100)
cmToKm = cm.get_converter_to(km)

cmToKm.convert(3) # 0.00003
cmToKm.inverse().convert(0.00003) # 3

Usage of derived units:

from simpleunit import FundamentalUnit, DerivedUnit

m = FundamentalUnit()
km = m.scale_multiply(1000)

km2 = DerivedUnit(km.factor(2))
cm = m.scale_divide(100)
cm2 = DerivedUnit(cm.factor(2))
km2Tocm2 = km2.get_converter_to(cm2)

km2Tocm2.convert(3) # 30000000000
km2Tocm2.inverse().convert(30000000000) # 3

Usage of derived units combining dimensions:

from simpleunit import FundamentalUnit, DerivedUnit

m = FundamentalUnit()
kg = FundamentalUnit()
g = kg.scale_divide(1000)
ton = kg.scale_multiply(1000)
gPerM2 = DerivedUnit(g, m.factor(-2))
km = m.scale_multiply(1000)
tonPerKm2 = DerivedUnit(ton, km.factor(-2))
cm = m.scale_divide(100)
tonPerCm2 = DerivedUnit(ton, cm.factor(-2))
gPerM2ToTonPerKm2 = gPerM2.get_converter_to(tonPerKm2)
gPerM2ToTonPerCm2 = gPerM2.get_converter_to(tonPerCm2)

gPerM2ToTonPerKm2.convert(1) # 1
gPerM2ToTonPerKm2.inverse().convert(3) # 3
gPerM2ToTonPerCm2.convert(1) # 1e-4
gPerM2ToTonPerCm2.convert(3) # 3e-10
gPerM2ToTonPerCm2.offset() # 0.0
gPerM2ToTonPerCm2.scale() # 1e-10
gPerM2ToTonPerCm2.inverse().offset() # -0.0
gPerM2ToTonPerCm2.inverse().convert(3e-10) # 3

Usage of temperatures (affine and linear conversions):

from simpleunit import FundamentalUnit, DerivedUnit

k = FundamentalUnit()
c = k.shift(273.15)
kToC = k.get_converter_to(c)

kToC.convert(0) # -273.15
kToC.inverse().convert(0) # 273.15

# combined with other units, temperatures only keep their linear conversion part
m = FundamentalUnit()
cPerM = DerivedUnit(c, m.factor(-1))
kPerM = DerivedUnit(k, m.factor(-1))
kPerMToCPerM = kPerM.get_converter_to(cPerM)

kPerMToCPerM.convert(3) # 3
kPerMToCPerM.inverse().convert(3) # 3

Usage of non-decimal conversions:

from simpleunit import FundamentalUnit, DerivedUnit

m = FundamentalUnit()
km = m.scale_multiply(1000.)

s = FundamentalUnit()
h = s.scale_multiply(3600.)

ms = DerivedUnit(m, s.factor(-1))
kmh = DerivedUnit(km, h.factor(-1))

msToKmh = ms.get_converter_to(kmh)

msToKmh.convert(100.) # 360
msToKmh.inverse().convert(18.) # 5

Operator overloading usage

The Simple Unit Python implementation provides an extension of the base specification to overloads some language operators.

Usage of transformed units:

from simpleunit import FundamentalUnit

m = FundamentalUnit()
km = m * 1000
cm = m / 100
cmToKm = cm >> km

cmToKm(3) # 0.00003
(~cmToKm)(0.00003) # 3

Usage of derived Units:

from simpleunit import FundamentalUnit

m = FundamentalUnit()
km = m * 1000

km2 = km ** 2
cm = m / 100
cm2 = cm ** 2
km2Tocm2 = km2 >> cm2

km2Tocm2(3) # 30000000000
(~km2Tocm2)(30000000000) # 3

Usage of derived units combining dimensions:

from simpleunit import FundamentalUnit

m = FundamentalUnit()
kg = FundamentalUnit()
g = kg / 1000
ton = kg * 1000
gPerM2 = g / m ** 2
km = m * 1000
tonPerKm2 = ton * ~km ** 2
cm = m / 100
tonPerCm2 = ton / cm ** 2
gPerM2ToTonPerKm2 = gPerM2 >> tonPerKm2
gPerM2ToTonPerCm2 = tonPerCm2 << gPerM2

gPerM2ToTonPerKm2(1) # 1
(~gPerM2ToTonPerKm2)(3) # 3
gPerM2ToTonPerCm2(1) # 1e-10
gPerM2ToTonPerCm2(3) # 3e-10
gPerM2ToTonPerCm2.offset() # 0.0
gPerM2ToTonPerCm2.scale() # 1e-10
(~gPerM2ToTonPerCm2).offset() # -0.0
(~gPerM2ToTonPerCm2)(3e-10) # 3

Usage of temperatures (affine and linear conversions):

from simpleunit import FundamentalUnit

k = FundamentalUnit()
c = k + 273.15
kToC = k >> c

kToC(0) # -273.15
(~kToC)(0) # 273.15

# combined with other units, temperatures only keep their linear conversion part
m = FundamentalUnit()
cPerM = c / m
kPerM = k / m
kPerMToCPerM = kPerM >> cPerM

kPerMToCPerM(3) # 3
(~kPerMToCPerM)(3) # 3

Usage of non-decimal conversions:

from simpleunit import FundamentalUnit

m = FundamentalUnit()
km = m * 1000.

s = FundamentalUnit()
h = s * 3600.

ms = m / s
kmh = km / h

msToKmh = ms >> kmh

msToKmh(100.) # 360
(~msToKmh)(18.) # 5

Documentation

Latest release

Trunk

Metadata

Release files for simpleunit 1.0.30

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

Source distribution (sdist)

Source distribution for simpleunit 1.0.30
File Size Uploaded
simpleunit-1.0.30.tar.gz 9.9 kB Details

Built distribution (wheel)

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

Total release size: 17.8 kB

Release files / simpleunit-1.0.30.tar.gz

Download URL simpleunit-1.0.30.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
814568ad5f53389a45beca7145dba3452597d4a3c23e4f9d5cc778f95b664515
BLAKE2b-256 checksum
How to use checksums
0057a27f2757a486addd5381dc33ca79a6ed0f26bdef01b985467eb857133887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.1

Release files / simpleunit-1.0.30-py3-none-any.whl

Download URL simpleunit-1.0.30-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65dc26cd21171c74945c6db20f6226ac72afa7a977c9538bd5e4e491f765f643
BLAKE2b-256 checksum
How to use checksums
c25dd45e2a0d82ca0253eeadd139b6a41b22b77c78d6ae88c2147d5a09815368
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.1

Release history Release notifications | RSS feed

This release

1.0.30 This release

2 release files

1.0.27

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