Skip to main content

si type metric

Project description

nist

From the National Institute of Standards and Technology’s Office OF Weights and Measures, one can find a handy list of all the metric prefixes: e.g.

Purpose Name Symbol Factor Name
larger quantities or whole units quetta Q $$10^{30}$$ nonillion
ronna R $$10^{27}$$ octillion
yotta Y $$10^{24}$$ septillion
hecto Example: hectare h $$10^{2}$$ hundred
deka Example: dekameter da $$10^{1}$$ ten
$$10^{o}$$ one
deci Example: decimeter da $$10^{-1}$$ tenth
centi Example: centigram h $$10^{-2}$$ hundredth
yocto Example: yoctosecond y $$10^{-24}$$ septillionth
ronto r $$10^{-27}$$ octillionth
smaller quantities or sub units quecto q $$10^{-30}$$ nonillionth

Oh how nice it would be to have a class like unit which we could subclass and use as follows:

class second(unit): 
    name = 'second'

s1 = second(1)
s1, s1.kilo, s1.milli, s1.to(3), float(s1.to(3))
(1.0 S, 0.001 KS, 1000.0 mS, 0.001 KS, 0.001)

ah so easy to convert between and even have clean formatting.

Install

pip install nist

Usage

fact

While each fact (factor) has base: ClassVar[int] = 10, base is actually an instance variable.

>>> float(kilo()), float(kilo(base=2)), float(kilo(base=5)), float(kilo(base=10))

(1000.0, 8.0, 125.0, 1000.0)

In case that behavior is not obvious fact is really just a named and explicilty signed exponent:

>>> kb = kilo(base=2)
>>> kb.abrv, kb.base, kb.expo, kb.sign, kb.ekey, float(kb)

('kilo', 2, 3, 1, 3, 8.0)

Each fact uses efmt for its representation by default efmt is True, but can be turned off by setting efmt to False.

>>> (
    (decka(), hecto(), kilo(), mega(), giga(), tera()),
    (decka(showefmt=False), hecto(showefmt=False), kilo(showefmt=False), mega(showefmt=False), giga(showefmt=False), tera(showefmt=False))
)

((e+1, e+2, e+3, e+6, e+9, e+12), (F1P, F2P, F3P, F6P, F9P, F12P))

Actually we have three formats to work with:

>>> kilo().fstr, kilo(showbase=True).bstr, kilo(showbase=True).efmt

('F3P', '10^+3', 'e+3')

unit

The goal of the unit class is to make it easy to create units:

class second(unit): 
    name = 'second'

>>> s1 = second(1)
>>> s1, s1.kilo, s1.milli, s1.to(3), float(s1.to(3)), s1.shownumb


(1.0 S, 0.001 KS, 1000.0 mS, 0.001 KS, 0.001, True)

We can also explore all the different ways of viewing formats:

import pandas as pd

results = list()
factors = (tera, decka, deci, centi, milli, pico)
for fcls in factors:
    for flt in (1, 20, 0.03):
        for factrepr in {'abrv', 'name', 'symb'}:
            for shownumb in (True, False):
                for abrvunit in (True, False):
                    res = fmtunit(
                        org = flt, flt=flt / float(fcls()), unt = unit, fct = fcls,
                        factrepr=factrepr, shownumb=shownumb, abrvunit=abrvunit,
                        unitname='second', factname=None, ndig=3
                    )
                    
                    results.append(dict(
                        fname=fcls.name, flt=flt, res=res, org = flt / float(fcls()),
                        shownumb=shownumb, abrvunit=abrvunit, factrepr=factrepr
                    ))

df = pd.DataFrame(results).sort_values(by=['fname', 'res'])
df.head()
fname flt res org shownumb abrvunit factrepr
134 hundredth 0.03 0.03 S 3 False True name
138 hundredth 0.03 0.03 S 3 False True abrv
142 hundredth 0.03 0.03 S 3 False True symb
143 hundredth 0.03 0.03 S 3 False False symb
135 hundredth 0.03 0.03 seconds 3 False False name

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

nist-0.0.3.tar.gz (18.1 kB view hashes)

Uploaded Source

Built Distribution

nist-0.0.3-py3-none-any.whl (19.6 kB view hashes)

Uploaded Python 3

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