Вы можете добавить start
и end
параметры, которые указывают, где в схеме находятся компоненты.
class BaseComponent:
def __init__(self, start, end):
self.start = start
self.end = end
def __repr__(self):
return f'<{self.__class__.__name__} start={self.start} end={self.end} at 0x{id(self):x}>'
def __eq__(self, other):
if not type(self)==type(other):
return False
return all(v==getattr(other, k) for k, v in self.__dict__.items())
class Battery(BaseComponent):
def __init__(self, start, end, voltage=None):
super().__init__(start, end)
self.voltage = voltage
class Resistor(BaseComponent):
def __init__(self, start, end, resistance=None):
super().__init__(start, end)
self.resistance = resistance
class Wire(BaseComponent):
def __init__(self, start, end):
super().__init__(start, end)
Затем вы можете создать макет с помощью:
V = Battery(8, 1, voltage=9)
w1, w2, w3 = Wire(1, 2), Wire(2, 3), Wire(3, 4)
R1, R2, R3 = Resistor(2, 7, 10e3), Resistor(3, 6, 2e3), Resistor(4, 5, 1e3)
w5, w6, w7 = Wire(5, 6), Wire(6, 7), Wire(7, 8)
Если вы хотите получить фантазию, вы также можете превратить ее в график.
class Circuit:
"""A directed graph structure for building circuits"""
def __init__(self):
"""Creates the graph. Note only one edge is allowed between nodes"""
self._outward = {}
self._inward = {}
@staticmethod
def _norm(name):
return str(name)
def add_node(self, name):
"""
Adds a single node to the graph. The `name` is converted to a string.
"""
node = self._norm(name)
# check for existance
if node in self._outward:
return node
self._outward.setdefault(node, {})
self._inward.setdefault(node, {})
return node
def add_component(self, component):
"""
Adds a circuitry component to the circuit.
"""
s = self._norm(component.start)
e = self._norm(component.end)
if self.has_edge(s, e):
raise KeyError(f'A link from {s} to {e} already exists')
start = self.add_node(component.start)
end = self.add_node(component.end)
self._outward[start][end] = component
self._inward[end][start] = component
def has_node(self, node):
return self._norm(node) in self._outward
def has_component(self, component):
start = self._norm(component.start)
end = self._norm(component.end)
return component==self._outward.get(start, {}).get(end)
def has_edge(self, start, end):
s = self._norm(start)
e = self._norm(end)
return bool(e in self._outward.get(s, {}))
def __iadd__(self, other):
self.add_component(other)
return self
Отсюда вы можете построить схему с помощью:
circuit = Circuit()
for c in (V, w1, w2, w3, R1, R2, R3, w5, w6, w7):
circuit.add_component(c)