Created
April 29, 2020 13:24
-
-
Save JoelBender/5333432e9d1a16a41237106b7949a05b to your computer and use it in GitHub Desktop.
Chips and wires
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Chips | |
""" | |
from typing import Any, List | |
_all_chips: List["Chip"] = [] | |
_all_wires: List["Wire"] = [] | |
class Chip: | |
def __init__(self) -> None: | |
_all_chips.append(self) | |
def execute(self) -> None: | |
raise NotImplementedError() | |
class Wire: | |
def __init__( | |
self, srcObject: object, srcAttr: str, dstObject: object, dstAttr: str | |
) -> None: | |
_all_wires.append(self) | |
self.srcObject = srcObject | |
self.srcAttr = srcAttr | |
self.dstObject = dstObject | |
self.dstAttr = dstAttr | |
def execute(self) -> None: | |
setattr(self.dstObject, self.dstAttr, getattr(self.srcObject, self.srcAttr)) | |
class RampUp(Chip): | |
outPin: float | |
def __init__(self, offset: float, limit: float) -> None: | |
super().__init__() | |
self.outPin = 0.0 | |
self.offset = offset | |
self.limit = limit | |
def execute(self) -> None: | |
self.outPin += self.offset | |
if self.outPin > self.limit: | |
self.outPin = 0 | |
class PrintValue(Chip): | |
inPin: Any | |
def __init__(self) -> None: | |
super().__init__() | |
self.inPin = None | |
def execute(self) -> None: | |
print(self.inPin) | |
# | |
# | |
# | |
ramp_up = RampUp(0.5, 10.0) | |
print_value = PrintValue() | |
some_wire = Wire(ramp_up, "outPin", print_value, "inPin") | |
# loop a few times | |
for i in range(20): | |
for chip in _all_chips: | |
chip.execute() | |
for wire in _all_wires: | |
wire.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment