Python library to represent numbers with units
Project description
Python library to represent numbers with units
Example:
import units as u
print u.Unit(1, u.metre)
Upcoming features
Short and long units (currently only short units)
Multipliers e.g. kilo (k), mega (M), kibi (KiB), ergs etc.
Arbitrary units e.g. bits (b), cars etc.
Features
Basic units
import units as u
from random import randint
# ampere
print u.Unit(randint(1, 100), u.ampere)
# candela
print u.Unit(randint(1, 100), u.candela)
# kelvin
print u.Unit(randint(1, 100), u.kelvin)
# kilogram
print u.Unit(randint(1, 100), u.kilogram)
# metre
print u.Unit(randint(1, 100), u.metre)
# mole
print u.Unit(randint(1, 100), u.mole)
# second
print u.Unit(randint(1, 100), u.second)
Operations on units
import units as u
from random import random
x = u.Unit(random(), u.second)
y = u.Unit(random(), u.ampere)
# addition
print x + y
print y + x
# subtraction
print x - y
print y - x
# multiplication
print x * y
print y * x
# division/true division
print x / y
print y / x
# floor division
print x // y
print y // x
# modulus
print x % y
# power
print x ** y
print y ** x
Operations between scalars and units
import units as u
from random import random
x = u.Unit(random(), u.second)
a = random() * 10
# addition
print a + y
print y + a
# subtraction
print a - y
print y - a
# multiplication
print a * y
print y * a
# division/true division
print a / y
print y / a
# floor division
print a // y
print y // a
# modulus
print a % y
# power
print a ** y
print y ** a
Derived units
import units as u
from random import random
# hertz
print u.Unit(random() * 10, u.hertz)
# newton
print u.Unit(random() * 10, u.newton)
# pascal
print u.Unit(random() * 10, u.pascal)
# joule
print u.Unit(random() * 10, u.joule)
# watt
print u.Unit(random() * 10, u.watt)
# coulomb
print u.Unit(random() * 10, u.coulomb)
# volt
print u.Unit(random() * 10, u.volt)
# farad
print u.Unit(random() * 10, u.farad)
# ohm
print u.Unit(random() * 10, u.ohm)
# siemems
print u.Unit(random() * 10, u.siemens)
# weber
print u.Unit(random() * 10, u.weber)
# tesla
print u.Unit(random() * 10, u.tesla)
# henry
print u.Unit(random() * 10, u.henry)
# degree celcius
print u.Unit(random() * 10, u.degree_celcius)
# lumen
print u.Unit(random() * 10, u.lumen)
# lux
print u.Unit(random() * 10, u.lux)
# becquerel
print u.Unit(random() * 10, u.becquerel)
# gray
print u.Unit(random() * 10, u.gray)
# sievert
print u.Unit(random() * 10, u.sievert)
# katal
print u.Unit(random() * 10, u.katal)
Unpacking derived units
import units as u
from random import random
# hertz
print u.Unit(random() * 10, u.hertz).full_units
# newton
print u.Unit(random() * 10, u.newton).full_units
# pascal
print u.Unit(random() * 10, u.pascal).full_units
# joule
print u.Unit(random() * 10, u.joule).full_units
# watt
print u.Unit(random() * 10, u.watt).full_units
# coulomb
print u.Unit(random() * 10, u.coulomb).full_units
# volt
print u.Unit(random() * 10, u.volt).full_units
# farad
print u.Unit(random() * 10, u.farad).full_units
# ohm
print u.Unit(random() * 10, u.ohm).full_units
# siemems
print u.Unit(random() * 10, u.siemens).full_units
# weber
print u.Unit(random() * 10, u.weber).full_units
# tesla
print u.Unit(random() * 10, u.tesla).full_units
# henry
print u.Unit(random() * 10, u.henry).full_units
# degree celcius
print u.Unit(random() * 10, u.degree_celcius).full_units
# lumen
print u.Unit(random() * 10, u.lumen).full_units
# lux
print u.Unit(random() * 10, u.lux).full_units
# becquerel
print u.Unit(random() * 10, u.becquerel).full_units
# gray
print u.Unit(random() * 10, u.gray).full_units
# sievert
print u.Unit(random() * 10, u.sievert).full_units
# katal
print u.Unit(random() * 10, u.katal).full_units
Arbitrary derived units
import units as u
speed = u.DerivedUnit.define('speed', metre / second)
v = Unit(10, speed)
Arbitrary custom units
from __future__ import division
from units import BaseUnit
class CommUnit(BaseUnit):
"""Template class for communication units"""
def __init__(self, *args, **kwargs):
super(CommUnit, self).__init__(*args, **kwargs)
# redefine the base units
self.unit_dict = {
'b': 0,
's': 0,
'B': 0,
}
@classmethod
def define(cls, key, value=1):
"""Constructor"""
obj = cls()
assert key in obj.unit_dict.keys()
assert isinstance(value, int)
obj.unit_dict[key] = value
return obj
which will be used as follows
bit = CommUnit.define('b') # define a bit as referring to the 'b' unit
second = CommUnit.define('s') # a second is 's'
data = Unit(32, bit)
T = Unit(4, second)
# data rate
print data / T # 8.0 b·s^-1 - bits per second
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distributions
File details
Details for the file python_units-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: python_units-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64fa47991f8f7c4fc4496aec2130c849fc6c206cb208465cfcc36033049290be |
|
MD5 | 1507714d825043d67e7670a4485951d8 |
|
BLAKE2b-256 | 23dcb1fe3d4365611f05ec20f42b54e4aa37e6bcbee96556165da8c034f0e2d0 |
File details
Details for the file python_units-0.1.3-py2-none-any.whl
.
File metadata
- Download URL: python_units-0.1.3-py2-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2913255e2afdc83053ff6f7b6896870f7829b015f7d6dd40c743c2d3dfee266d |
|
MD5 | d1fc94fcbb21dcdc6e2f5c1a194abcfc |
|
BLAKE2b-256 | fffc4a4a31d3ed7488e282a3d60dae3397e52dc7d1dc6ff6d7d01508141cfe56 |