Skip to content

Unit

planck.models.Unit ยค

Unit(symbol, quantity, name=None, si_prefixes=None, order=1, values=None)

Bases: dict

Parameters:

Name Type Description Default
symbol str

Unit symbol

required
quantity str

Unit quantity

required
name str

Unit name

None
si_prefixes list

List of supported international system prefixes

None
order int

Order of the quantity

1
values Dict[str, float]

Values expressed in other units

None

Examples:

from planck import models
from planck import sp_constants

unit = models.Unit(
    symbol="m",
    quantity="length",
    values={
        "in": 1.0 / sp_constants.inch,
        "ft": 1.0 / sp_constants.foot,
        "mi": 1.0 / sp_constants.mile,
        "NM": 1.0 / sp_constants.nautical_mile,
    },
    si_prefixes=["m", "c", "", "k"],
)
print(unit)
'''
metre [m] - unit of length:
{'in': 39.37007874015748, 'ft': 3.280839895013124, 'mi': 0.000621371192237334, 'NM': 0.0005399568034557236, 'mm': 1000.0, 'cm': 100.0, 'm': 1.0, 'km': 0.001}
'''
Source code in planck/models/unit.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def __init__(
    self,
    symbol: str,
    quantity: str,
    name: str = None,
    si_prefixes: list = None,
    order: int = 1,
    values: Dict[str, float] = None,
):
    """
    Unit model

    Parameters
    ----------
    symbol:
        Unit symbol
    quantity:
        Unit quantity
    name:
        Unit name
    si_prefixes:
        List of supported international system prefixes
    order:
        Order of the quantity
    values:
        Values expressed in other units

    Examples
    --------
    ```py
    from planck import models
    from planck import sp_constants

    unit = models.Unit(
        symbol="m",
        quantity="length",
        values={
            "in": 1.0 / sp_constants.inch,
            "ft": 1.0 / sp_constants.foot,
            "mi": 1.0 / sp_constants.mile,
            "NM": 1.0 / sp_constants.nautical_mile,
        },
        si_prefixes=["m", "c", "", "k"],
    )
    print(unit)
    '''
    metre [m] - unit of length:
    {'in': 39.37007874015748, 'ft': 3.280839895013124, 'mi': 0.000621371192237334, 'NM': 0.0005399568034557236, 'mm': 1000.0, 'cm': 100.0, 'm': 1.0, 'km': 0.001}
    '''
    ```
    """
    # Default mutable values
    if values is None:
        values = {}
    if si_prefixes is None:
        si_prefixes = []
    if name is None:
        name = all_units.get(symbol)

    super().__init__(values)

    self.symbol = symbol
    self.quantity = quantity
    self.name = name
    self.si_prefixes = si_prefixes
    self.order = order

    self.add_si_prefixes()