Skip to content

Units

planck.units.Units ¤

Bases: dict

Units library storing planck.models.Unit models.

Functions¤

convert ¤

convert(value, input_unit, output_unit)

Convert a value from input_unit to output_unit

Parameters:

Name Type Description Default
value Union[float, array]

Value to convert

required
input_unit str

Source unit

required
output_unit str

Target unit

required

Returns:

Type Description
Union[float, array]

Value expressed as output_unit

Examples:

from planck import units

print(units.convert(1.0, "m", "ft"))
#> 3.280839895013124

print(units.convert(0.0, "C", "K"))
#> 273.15
Source code in planck/units.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
def convert(
    self, value: Union[float, np.array], input_unit: str, output_unit: str
) -> Union[float, np.array]:
    """
    Convert a `value` from `input_unit` to `output_unit`

    Parameters
    ----------
    value:
        Value to convert
    input_unit:
        Source unit
    output_unit:
        Target unit

    Returns
    -------
    :
        Value expressed as `output_unit`

    Examples
    --------
    ```py
    from planck import units

    print(units.convert(1.0, "m", "ft"))
    #> 3.280839895013124

    print(units.convert(0.0, "C", "K"))
    #> 273.15
    ```
    """

    # Temperature
    if input_unit.lower() in TEMPERATURE_UNITS:
        if input_unit == "degc":
            input_unit = "c"
        if output_unit == "degc":
            output_unit = "c"
        return sp_constants.convert_temperature(value, input_unit, output_unit)

    return value * self[input_unit][output_unit]

find ¤

find(sub=None, quantity=None)

Return unit keys containing a given string

Parameters:

Name Type Description Default
sub str

Sub-string to search keys for. By default, return all keys.

None
quantity str

Specific quantity ["length", "mass", "volume", "pressure", etc.]

None

Returns:

Name Type Description
keys list

List of keys

Source code in planck/units.py
 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
100
101
102
103
def find(self, sub: str = None, quantity: str = None) -> list:
    """
    Return unit keys containing a given string

    Parameters
    ----------
    sub:
       Sub-string to search keys for. By default, return all keys.
    quantity:
       Specific quantity ["length", "mass", "volume", "pressure", etc.]

    Returns
    ------
    keys:
       List of keys
    """
    if sub is None:
        sub = ""

    # Get Keys
    keys = []
    for k, v in self.items():
        if sub in k:
            if not quantity or v.quantity == quantity.lower():
                keys += [k]
    keys.sort()

    # Return keys
    return keys