Extract shared factory dispatch into ComponentFactory base class
All four name-dispatch factories repeated the same if/elif lazy-import pattern. Replace with a ComponentFactory base class holding a callable registry and a _lazy() helper for the common import-and-construct case. PlantFactory is an orchestrator, not a dispatcher, and is left as-is. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tqxrk8uj4M3w3d3eXm3xK8
This commit is contained in:
@@ -1,11 +1,8 @@
|
|||||||
|
from components.factory import ComponentFactory, _lazy
|
||||||
|
|
||||||
|
|
||||||
class HeaterFactory:
|
class HeaterFactory(ComponentFactory):
|
||||||
@staticmethod
|
_registry = {
|
||||||
def create(name, *args, **kwargs):
|
"sim": _lazy("components.actor.heater_sim", "HeaterSim"),
|
||||||
if "sim" in name:
|
"Hendi": _lazy("components.actor.heater_hendi", "HeaterHendi"),
|
||||||
from components.actor.heater_sim import HeaterSim
|
}
|
||||||
return HeaterSim(*args, **kwargs)
|
|
||||||
elif "Hendi" in name:
|
|
||||||
from components.actor.heater_hendi import HeaterHendi
|
|
||||||
return HeaterHendi(*args, **kwargs)
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
from components.factory import ComponentFactory, _lazy
|
||||||
|
|
||||||
|
|
||||||
class StirrerFactory:
|
def _sim(dt, params):
|
||||||
@staticmethod
|
from components.actor.stirrersim import StirrerSim
|
||||||
def create(name, dt, params):
|
return StirrerSim(dt)
|
||||||
if "sim" in name:
|
|
||||||
from components.actor.stirrersim import StirrerSim
|
|
||||||
return StirrerSim(dt)
|
class StirrerFactory(ComponentFactory):
|
||||||
elif "1376" in name:
|
_registry = {
|
||||||
from components.actor.stirrerpololu1376 import StirrerPololu1376
|
"sim": _sim,
|
||||||
return StirrerPololu1376(dt, params)
|
"1376": _lazy("components.actor.stirrerpololu1376", "StirrerPololu1376"),
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import importlib
|
||||||
|
|
||||||
|
|
||||||
|
def _lazy(module, classname):
|
||||||
|
"""Return a builder that lazily imports module.classname on first call."""
|
||||||
|
def build(*args, **kwargs):
|
||||||
|
return getattr(importlib.import_module(module), classname)(*args, **kwargs)
|
||||||
|
return build
|
||||||
|
|
||||||
|
|
||||||
|
class ComponentFactory:
|
||||||
|
_registry = {}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create(cls, name, *args, **kwargs):
|
||||||
|
for key, builder in cls._registry.items():
|
||||||
|
if key in name:
|
||||||
|
return builder(*args, **kwargs)
|
||||||
|
raise ValueError(f"{cls.__name__}: unknown name {name!r}")
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
|
from components.factory import ComponentFactory, _lazy
|
||||||
|
|
||||||
|
|
||||||
class PidFactory:
|
class PidFactory(ComponentFactory):
|
||||||
@staticmethod
|
_registry = {
|
||||||
def create(name, *args, **kwargs):
|
"Normal": _lazy("components.pid.temp_controller", "TempController"),
|
||||||
if "Normal" in name:
|
"Smith": _lazy("components.pid.temp_controller_smith", "TempController"),
|
||||||
from components.pid.temp_controller import TempController
|
}
|
||||||
return TempController(*args, **kwargs)
|
|
||||||
elif "Smith" in name:
|
|
||||||
from components.pid.temp_controller_smith import TempController
|
|
||||||
return TempController(*args, **kwargs)
|
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
|
from components.factory import ComponentFactory, _lazy
|
||||||
|
|
||||||
|
|
||||||
class TempSensorFactory:
|
class TempSensorFactory(ComponentFactory):
|
||||||
@staticmethod
|
_registry = {
|
||||||
def create(name, *args, **kwargs):
|
"sim": _lazy("components.sensor.tempSensorSim", "TempSensorSim"),
|
||||||
if "sim" in name:
|
"31865": _lazy("components.sensor.tempSensor_max31865", "TempSensor_max31865"),
|
||||||
from components.sensor.tempSensorSim import TempSensorSim
|
}
|
||||||
return TempSensorSim(*args, **kwargs)
|
|
||||||
elif "31865" in name:
|
|
||||||
from components.sensor.tempSensor_max31865 import TempSensor_max31865
|
|
||||||
return TempSensor_max31865(*args, **kwargs)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user