conversions.prefix_conversions_string

Inspired by prefix_conversion.py file in this repository by lance-pyles

URL: https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes URL: https://en.wikipedia.org/wiki/Binary_prefix

Attributes

T

Classes

BinaryUnit

Create a collection of name/value pairs.

SIUnit

Create a collection of name/value pairs.

Functions

add_binary_prefix(→ str)

Function that converts a number to his version with Binary prefix

add_si_prefix(→ str)

Function that converts a number to his version with SI prefix

Module Contents

class conversions.prefix_conversions_string.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 = 60
giga = 30
kilo = 10
mega = 20
peta = 50
tera = 40
yotta = 80
zetta = 70
class conversions.prefix_conversions_string.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.

classmethod get_negative() dict

Returns a dictionary with only the elements of this enum that has a negative value @example >>> from itertools import islice >>> negative = SIUnit.get_negative() >>> inc = iter(negative.items()) >>> dict(islice(inc, len(negative) // 2)) {‘deci’: -1, ‘centi’: -2, ‘milli’: -3, ‘micro’: -6, ‘nano’: -9} >>> dict(inc) {‘pico’: -12, ‘femto’: -15, ‘atto’: -18, ‘zepto’: -21, ‘yocto’: -24}

classmethod get_positive() dict

Returns a dictionary with only the elements of this enum that has a positive value >>> from itertools import islice >>> positive = SIUnit.get_positive() >>> inc = iter(positive.items()) >>> dict(islice(inc, len(positive) // 2)) {‘yotta’: 24, ‘zetta’: 21, ‘exa’: 18, ‘peta’: 15, ‘tera’: 12} >>> dict(inc) {‘giga’: 9, ‘mega’: 6, ‘kilo’: 3, ‘hecto’: 2, ‘deca’: 1}

atto = -18
centi = -2
deca = 1
deci = -1
exa = 18
femto = -15
giga = 9
hecto = 2
kilo = 3
mega = 6
micro = -6
milli = -3
nano = -9
peta = 15
pico = -12
tera = 12
yocto = -24
yotta = 24
zepto = -21
zetta = 21
conversions.prefix_conversions_string.add_binary_prefix(value: float) str

Function that converts a number to his version with Binary prefix @input value (an integer) @example: >>> add_binary_prefix(65536) ‘64.0 kilo’

conversions.prefix_conversions_string.add_si_prefix(value: float) str

Function that converts a number to his version with SI prefix @input value (an integer) @example: >>> add_si_prefix(10000) ‘10.0 kilo’

conversions.prefix_conversions_string.T