conversions.prefix_conversions

Convert International System of Units (SI) and Binary prefixes

Classes

BinaryUnit

Create a collection of name/value pairs.

SIUnit

Create a collection of name/value pairs.

Functions

convert_binary_prefix(→ float)

Wikipedia reference: https://en.wikipedia.org/wiki/Metric_prefix

convert_si_prefix(→ float)

Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix

Module Contents

class conversions.prefix_conversions.BinaryUnit(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

    >>> Color.RED
    <Color.RED: 1>
    
  • value lookup:

    >>> Color(1)
    <Color.RED: 1>
    
  • name lookup:

    >>> Color['RED']
    <Color.RED: 1>
    

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

exa = 6
giga = 3
kilo = 1
mega = 2
peta = 5
tera = 4
yotta = 8
zetta = 7
class conversions.prefix_conversions.SIUnit(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

    >>> Color.RED
    <Color.RED: 1>
    
  • value lookup:

    >>> Color(1)
    <Color.RED: 1>
    
  • name lookup:

    >>> Color['RED']
    <Color.RED: 1>
    

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

atto
centi
deca = 1
deci
exa = 18
femto
giga = 9
hecto = 2
kilo = 3
mega = 6
micro
milli
nano
peta = 15
pico
tera = 12
yocto
yotta = 24
zepto
zetta = 21
conversions.prefix_conversions.convert_binary_prefix(known_amount: float, known_prefix: str | BinaryUnit, unknown_prefix: str | BinaryUnit) float

Wikipedia reference: https://en.wikipedia.org/wiki/Metric_prefix >>> convert_binary_prefix(1, BinaryUnit.giga, BinaryUnit.mega) 1024 >>> convert_binary_prefix(1, BinaryUnit.mega, BinaryUnit.giga) 0.0009765625 >>> convert_binary_prefix(1, BinaryUnit.kilo, BinaryUnit.kilo) 1 >>> convert_binary_prefix(1, ‘giga’, ‘mega’) 1024 >>> convert_binary_prefix(1, ‘gIGa’, ‘mEGa’) 1024

conversions.prefix_conversions.convert_si_prefix(known_amount: float, known_prefix: str | SIUnit, unknown_prefix: str | SIUnit) float

Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units >>> convert_si_prefix(1, SIUnit.giga, SIUnit.mega) 1000 >>> convert_si_prefix(1, SIUnit.mega, SIUnit.giga) 0.001 >>> convert_si_prefix(1, SIUnit.kilo, SIUnit.kilo) 1 >>> convert_si_prefix(1, ‘giga’, ‘mega’) 1000 >>> convert_si_prefix(1, ‘gIGa’, ‘mEGa’) 1000