Ich habe daher eine erste Aufzählungsklasse erstellt, die für verfügbare Metriken definiert werden soll:
Code: Select all
## --- Imports
from enum import auto, Enum
## --- Metrics
class Metric(Enum):
#[
DimensionLess = auto()
Mass = auto()
Frequency = auto()
Length = auto()
# Etc ...
#]
Code: Select all
## --- Units
class Unit(Enum):
#[
## -- Definition
def __init__(self, metric: Metric, isSI: bool, scale: float, offset: float, symbols: set[str], isSpecialdB: bool = False):
#[
self.__metric = metric
self.__isSI = isSI
self.__scale = scale
self.__offset = offset
self.__symbols = symbols
self.__isSpecialdB = isSpecialdB
#]
## -- Make sure attributes are read only
@property
def Metric(self): return self.__metric
# ...
Code: Select all
# If I write this, the interpreter is confused between the class and the property 'Metric'
Once = (Metric.Mass, False, (0.45359237 / 16), 0.0, {'oz', 'once', 'onces'})
Entweder verwende ich globals(), um den Abgleich mit der Klassendefinition zu erzwingen:
Code: Select all
# Force to use 'class Metric'
Once = (globals()['Metric'].Mass, False, (0.45359237 / 16), 0.0, {'oz', 'once', 'onces'})
Code: Select all
## -- Mass units
Kilogram = (Metric.Mass, True, 1.0, 0.0, {'kg', 'kilogram', 'kilograms'})
Gram = (Metric.Mass, False, 1.0e-3, 0.0, {'g', 'gram', 'grams'})
Pound = (Metric.Mass, False, 0.45359237, 0.0, {'lb', 'lbs', 'pound', 'pounds'})
Once = (Metric.Mass, False, (0.45359237 / 16), 0.0, {'oz', 'once', 'onces'})
## -- Etc units ...
# ....
## -- Make sure attributes are read only
## NEED TO BE DEFINED AFTER ENUMERATION VALUES TO AVOID CONFUSION WITH 'class Metric'
@property
def Metric(self): return self.__metric
# ...
#]
Code: Select all
class Foo1:
@staticmethod
def Dummy1(): return globals()['__commonInternalHelperFcn']()
## Second class
class Foo2:
@staticmethod
def Dummy2(): return globals()['__commonInternalHelperFcn']()
## Private
def __commonInternalHelperFcn(): return __answer()
def __answer(): return 42
Mobile version