Module unitexpr.si_units
Si Unit System based on meter, second, kilogram, Ampere, Kelvin, mole, candela.
None
View Source
"""
Si Unit System based on meter, second, kilogram, Ampere, Kelvin, mole, candela.
"""
from math import pi
from .unit import UnitBase, UnitMeta
from .unit_symbol import UnitSymbol
unit_symbols = (
UnitSymbol("m", "meter", "length"),
UnitSymbol("s", "second", "time"),
UnitSymbol("kg", "kilogram", "mass"),
UnitSymbol("A", "ampere", "electric current"),
UnitSymbol("K", "kelvin", "temperature"),
UnitSymbol("mol", "mole", "amount of substance"),
UnitSymbol("cd", "candela", "luminous intensity"),
)
class SiUnit(UnitBase, metaclass=UnitMeta, unit_symbols=unit_symbols):
"""
Class representing SI unitexpr. The fundamental units are available as:
`SiUnit.m`, `SiUnit.s`, `SiUnit.kg`, `SiUnit.A`, `SiUnit.K`, `SiUnit.mol`
and `SiUnit.cd`.
"""
__slots__ = ()
# Base units
m = SiUnit.m
"""
Base unit: Meter
"""
s = SiUnit.s
kg = SiUnit.kg
A = SiUnit.A
K = SiUnit.K
mol = SiUnit.mol
cd = SiUnit.cd
# Derived units
au = SiUnit("au", "astronomical unit", "length", expr=149597870700 * m)
sr = SiUnit("sr", "steradian", "solid angle", expr=SiUnit.expr_type.one)
N = SiUnit("N", "newton", "force", expr=kg * m * s ** -2)
J = SiUnit("J", "joule", "energy", expr=N * m)
eV = SiUnit("eV", "electronvolt", "energy", expr=1.602176634e-19 * J)
W = SiUnit("W", "watt", "power", expr=J / s)
C = SiUnit("C", "coulomb", "charge", expr=A * s)
V = SiUnit("V", "volt", "electric potential difference", expr=J / C)
F = SiUnit("F", "farad", "capacitance", expr=C / V)
ohm = SiUnit("ohm", "ohm", "resistance", expr=V / A)
Pa = SiUnit("Pa", "pascal", "pressure", expr=N * m ** -2)
S = SiUnit("S", "siemens", "electrical conductance", 1 / ohm)
Wb = SiUnit("Wb", "weber", "magnetic flux", expr=V * s)
T = SiUnit("T", "tesla", "magnetic flux density", expr=Wb * m ** -2)
H = SiUnit("H", "henry", "inductance", expr=Wb / A)
lm = SiUnit("lm", "lumen", "luminous flux", expr=cd / sr)
Bq = SiUnit("Bq", "bequerel", "radioactivity (decays per second)", expr=1 / s)
lx = SiUnit("lx", "lux", "illuminance", expr=lm * m ** -2)
Gy = SiUnit("Gy", "gray", "absorbed dose of ionizing radiation", expr=J / kg)
# Constants with exact value
delta_nu_Cs = SiUnit(
"delta_nu_cs",
"hyperfine transition of frequency of Cs",
"frequency",
expr=91926377 / s,
)
c = SiUnit("c", "speed of light", "velocity", expr=299792458 * m / s)
h = SiUnit("h", "Planck constant", "angular momentum", 6.62607015e-34 * J * s)
h_bar = SiUnit(
"h_bar", "Reduced Planck constant", "angular momentum", h / (2 * pi)
)
e = SiUnit("e", "elementary charge", "charge", expr=1.606176634e-19 * C)
k = SiUnit(
"k", "Boltzmann constant", "energy/Kelvin", expr=1.380649e-23 * J / K
)
m_e = SiUnit("m_e", "electron mass", "weigth", expr=9.1093837015e-31 * kg)
N_a = SiUnit(
"N_a",
"Avogadro constant",
"number of molecules per mole",
expr=6.02214076e-23 / mol,
)
K_cd = SiUnit(
"K_cd",
"luminous efficacy",
"luminous efficacy of 540 THz radiation",
expr=683 * lm / W,
)
Variables
A
Bq
C
F
Gy
H
J
K
K_cd
N
N_a
Pa
S
T
V
W
Wb
au
c
cd
delta_nu_Cs
e
eV
h
h_bar
k
kg
lm
lx
m
Base unit: Meter
m_e
mol
ohm
pi
s
sr
unit_symbols
Classes
SiUnit
class SiUnit(
/,
*args,
**kwargs
)
View Source
class SiUnit(UnitBase, metaclass=UnitMeta, unit_symbols=unit_symbols):
"""
Class representing SI unitexpr. The fundamental units are available as:
`SiUnit.m`, `SiUnit.s`, `SiUnit.kg`, `SiUnit.A`, `SiUnit.K`, `SiUnit.mol`
and `SiUnit.cd`.
"""
__slots__ = ()
Ancestors (in MRO)
- unitexpr._unitexpr.UnitExprMixin
- unitexpr.unit.UnitBase
- unitexpr.unit._UnitBase
- builtins.tuple
Class variables
A
K
base_exponents
base_exponents_zero
base_factor
base_units
cd
expr_type
info_type
kg
m
mol
name
quantity
s
sub_exponents
sub_factor
sub_terms
symbol
valid_types
Instance variables
base_expr
Returns an expression representing self in terms of base units.
exponents
Returns the exponents of the unit expression.
expr
Returns an expression representing self in terms of
sub_terms.
J = SiUnit('J', 'Joule', 'energy', expr=N*m)
print(J.expr) # prints: N*m
factor
Returns the (scaling) factor of the unit expression.
Always returns 1.0.
info
Returns a NamedTuple containing detailed object information.
self_expr
Returns an expression representing self in terms of self.
An alternative method of converting a unit to a unit expression is multiplication by 1.0.
J = SiUnit('J', 'Joule', 'energy', expr=N*m)
assert J.self_expr == J
assert J.self_expr == 1.0*J
assert type(J.self_expr) == SiUnit.expr_type
print(J_expr) # Prints: J
terms
Returns the terms of the unit expression.
Methods
count
def count(
self,
value,
/
)
Return number of occurrences of value.
index
def index(
self,
value,
start=0,
stop=9223372036854775807,
/
)
Return first index of value.
Raises ValueError if the value is not present.
proportional_to
def proportional_to(
self,
other
) -> 'bool'
Returns True if the unit
self can be converted to to the unit expression or unit other
using a constant factor.
Returns False otherwise.
View Source
def proportional_to(self, other) -> bool:
"""
Returns `True` if the unit
`self` can be converted to to the unit expression or unit `other`
using a constant factor.
Returns `False` otherwise.
"""
if isinstance(other, self.valid_types):
if other.base_exponents != self.base_exponents:
return False
return True
if isinstance(other, (int, float)):
if self.base_exponents != self.base_exponents_zero:
return False
return True
return False
scaling_factor
def scaling_factor(
self,
other
) -> 'Union[float, None]'
Returns the scaling factor that converts the unit
self to the unit expression or unit other.
Returns None if self cannot be converted to other
by multiplication with a number of type int or float.
View Source
def scaling_factor(self, other) -> Union[float, None]:
"""
Returns the scaling factor that converts the unit
`self` to the unit expression or unit `other`.
Returns `None` if `self` cannot be converted to `other`
by multiplication with a number of type `int` or `float`.
"""
if isinstance(other, self.valid_types):
if other.base_exponents != self.base_exponents:
return None
return other.base_factor / self.base_factor
if isinstance(other, (int, float)):
if self.base_exponents != self.base_exponents_zero:
return None
return other / self.base_factor
return None