Module unitexpr.unit_symbol
Provides UnitSymbol an immutable class representing base unit symbols.
None
View Source
"""
Provides `UnitSymbol` an immutable class representing base unit symbols.
"""
from typing import NamedTuple
import re
class UnitSymbol(
NamedTuple("_UnitSymbol", symbol=str, name=str, quantity=str),
):
"""
Immutable class inheriting from `NamedTuple` representing a
base unit symbol with fields:
* symbol: The symbol displayed in unit expressions. Should be a valid
Python identifier.
* name: The unit name.
* quantity: The physical quantity represented by the unit.
``` python
m = UnitSymbol('m', 'meter', 'length')
s = UnitSymbol(symbol='s', name='second', quantity='time')
```
"""
__slots__ = ()
__reg_expr = "^[A-Za-z_][A-Za-z0-9_]*"
def __new__(cls, symbol: str, name: str, quantity: str):
if not UnitSymbol.is_valid_symbol(symbol):
raise ValueError(
f"Symbol {type(symbol)}:{symbol} is not a valid identifier."
)
return super().__new__(cls, symbol, name, quantity)
@classmethod
def is_valid_symbol(cls, value: str) -> bool:
"""
Returns `True` if `value` matches the regular expression:
'^[A-Za-z_][A-Za-z0-9_]*'
"""
if not isinstance(value, str):
return False
if re.fullmatch(UnitSymbol.__reg_expr, value) is None:
return False
return True
Classes
UnitSymbol
class UnitSymbol(
/,
*args,
**kwargs
)
View Source
class UnitSymbol(
NamedTuple("_UnitSymbol", symbol=str, name=str, quantity=str),
):
"""
Immutable class inheriting from `NamedTuple` representing a
base unit symbol with fields:
* symbol: The symbol displayed in unit expressions. Should be a valid
Python identifier.
* name: The unit name.
* quantity: The physical quantity represented by the unit.
``` python
m = UnitSymbol('m', 'meter', 'length')
s = UnitSymbol(symbol='s', name='second', quantity='time')
```
"""
__slots__ = ()
__reg_expr = "^[A-Za-z_][A-Za-z0-9_]*"
def __new__(cls, symbol: str, name: str, quantity: str):
if not UnitSymbol.is_valid_symbol(symbol):
raise ValueError(
f"Symbol {type(symbol)}:{symbol} is not a valid identifier."
)
return super().__new__(cls, symbol, name, quantity)
@classmethod
def is_valid_symbol(cls, value: str) -> bool:
"""
Returns `True` if `value` matches the regular expression:
'^[A-Za-z_][A-Za-z0-9_]*'
"""
if not isinstance(value, str):
return False
if re.fullmatch(UnitSymbol.__reg_expr, value) is None:
return False
return True
Ancestors (in MRO)
- unitexpr.unit_symbol._UnitSymbol
- builtins.tuple
Class variables
name
quantity
symbol
Static methods
is_valid_symbol
def is_valid_symbol(
value: str
) -> bool
Returns True if value matches the regular expression:
'^[A-Za-z_][A-Za-z0-9_]*'
View Source
@classmethod
def is_valid_symbol(cls, value: str) -> bool:
"""
Returns `True` if `value` matches the regular expression:
'^[A-Za-z_][A-Za-z0-9_]*'
"""
if not isinstance(value, str):
return False
if re.fullmatch(UnitSymbol.__reg_expr, value) is None:
return False
return True
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.