Skip to content

Commit

Permalink
Replace Input/Output types with nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoliaw committed Jul 21, 2023
1 parent 62e29f8 commit ee66671
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 35 deletions.
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ it increments.
class CounterDevice(Device):
"""A simple device which increments a value."""
Inputs: type = TypedDict("Inputs", {})
Outputs: type = TypedDict("Outputs", {"value":int})
class Inputs(TypedDict):
...
class Outputs(TypedDict):
value: int
def __init__(self, initial_value: int = 0, callback_period: int = int(1e9)) -> None:
self._value = initial_value
Expand Down
8 changes: 5 additions & 3 deletions docs/user/explanations/devices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ values and requests to be called back for an update sometime later.
"""A trivial toy device which produced a random output and requests a callback."""
#: An empty typed mapping of device inputs
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...
#: A typed mapping containing the 'output' output value
Outputs: type = TypedDict("Outputs", {"output": int})
class Outputs(TypedDict):
output: int
def __init__(self, callback_period: int = int(1e9)) -> None:
self.callback_period = SimTime(callback_period)
Expand All @@ -45,4 +47,4 @@ values and requests to be called back for an update sometime later.
Logic can be implemented into the device via device methods. For an example of
this look at the ``ShutterDevice``. It acts to attenuate the flux of any incoming
value.
value.
6 changes: 4 additions & 2 deletions docs/user/how-to/use-epics-adapter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ outputs a current.
class FemtoDevice(Device):
"""Electronic signal amplifier."""
Inputs: type = TypedDict("Inputs", {"input": float})
Outputs: type = TypedDict("Outputs", {"current": float})
class Inputs(TypedDict):
input: float
class Outputs(TypedDict):
current: float
def __init__(
self,
Expand Down
18 changes: 12 additions & 6 deletions docs/user/tutorials/create-a-device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ maps as members. As such we shall put in the following boilerplate.
class AmplifierDevice(Device):
Inputs: type = TypedDict("Inputs", {})
Outputs: type = TypedDict("Outputs", {})
class Inputs(TypedDict):
...
class Outputs(TypedDict):
...
def __init__(self) -> None:
Expand All @@ -59,8 +61,10 @@ here.
class AmplifierDevice(Device):
Inputs: type = TypedDict("Inputs", {})
Outputs: type = TypedDict("Outputs", {})
class Inputs(TypedDict):
...
class Outputs(TypedDict):
...
def __init__(self, initial_amplification: float = 2) -> None:
self.amplification = initial_amplification
Expand Down Expand Up @@ -95,8 +99,10 @@ we define our inputs and outputs in the maps, and the line of logic in the ``upd
class AmplifierDevice(Device):
Inputs: type = TypedDict("Inputs", {"initial_signal":float})
Outputs: type = TypedDict("Outputs", {"amplified_signal":float})
class Inputs(TypedDict):
initial_signal: float
class Outputs(TypedDict):
amplified_signal: float
def __init__(self, initial_amplification: float = 2.0) -> None:
self.amplification = initial_amplification
Expand Down
7 changes: 5 additions & 2 deletions examples/devices/amplifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
class AmplifierDevice(Device):
"""Amplifier device which multiplies an input signal by an amplification value."""

Inputs: type = TypedDict("Inputs", {"initial_signal": float})
Outputs: type = TypedDict("Outputs", {"amplified_signal": float})
class Inputs(TypedDict):
initial_signal: float

class Outputs(TypedDict):
amplified_signal: float

def __init__(self, initial_amplification: float = 2) -> None:
"""Amplifier constructor which configures the initial amplification.
Expand Down
7 changes: 5 additions & 2 deletions examples/devices/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class CounterDevice(Device):
"""A simple device which increments a value."""

#: An empty typed mapping of input values
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...

#: A typed mapping containing the 'value' output value
Outputs: type = TypedDict("Outputs", {"value": int})
class Outputs(TypedDict):
value: int

def __init__(self, initial_value: int = 0, callback_period: int = int(1e9)) -> None:
"""A constructor of the counter, which increments the input value.
Expand Down
7 changes: 5 additions & 2 deletions examples/devices/isolated_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class IsolatedBoxDevice(Device):
The device has no inputs or outputs and interacts solely through adapters.
"""

Inputs: type = TypedDict("Inputs", {})
Outputs: type = TypedDict("Outputs", {})
class Inputs(TypedDict):
...

class Outputs(TypedDict):
...

def __init__(self, initial_value: float = 2) -> None:
"""Constructor which configures the initial value
Expand Down
7 changes: 5 additions & 2 deletions examples/devices/remote_controlled.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class RemoteControlledDevice(Device):
"""A trivial toy device which is controlled by an adapter."""

#: An empty typed mapping of device inputs
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...

#: A typed mapping containing the 'observed' output value
Outputs: type = TypedDict("Outputs", {"observed": float})
class Outputs(TypedDict):
observed: float

def __init__(
self,
Expand Down
7 changes: 5 additions & 2 deletions examples/devices/shutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class ShutterDevice(Device):
"""

#: A typed mapping containing the 'flux' input value
Inputs: type = TypedDict("Inputs", {"flux": float})
class Inputs(TypedDict):
flux: float

#: A typed mapping containing the 'flux' output value
Outputs: type = TypedDict("Outputs", {"flux": float})
class Outputs(TypedDict):
flux: float

def __init__(
self, default_position: float, initial_position: Optional[float] = None
Expand Down
14 changes: 10 additions & 4 deletions examples/devices/trampoline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ class TrampolineDevice(Device):
"""A trivial toy device which requests a callback every update."""

#: An empty typed mapping of device inputs
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...

#: An empty typed mapping of device outputs
Outputs: type = TypedDict("Outputs", {})
class Outputs(TypedDict):
...

def __init__(self, callback_period: int = int(1e9)) -> None:
"""A constructor of the sink which configures the device callback period.
Expand Down Expand Up @@ -55,9 +58,12 @@ class RandomTrampolineDevice(Device):
"""A trivial toy device which produced a random output and requests a callback."""

#: An empty typed mapping of device inputs
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...

#: A typed mapping containing the 'output' output value
Outputs: type = TypedDict("Outputs", {"output": int})
class Outputs(TypedDict):
output: int

def __init__(self, callback_period: int = int(1e9)) -> None:
"""A constructor of the sink which configures the device callback period.
Expand Down
9 changes: 5 additions & 4 deletions src/tickit/devices/iobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ class IoBoxDevice(Device, Generic[A, V]):
"""

#: A typed mapping containing the 'input' input value
Inputs: type = TypedDict("Inputs", {"updates": NotRequired[List[Tuple[Any, Any]]]})
class Inputs(TypedDict):
updates: NotRequired[List[Tuple[Any, Any]]]

#: A typed mapping of device outputs
Outputs: type = TypedDict(
"Outputs", {"updates": NotRequired[List[Tuple[Any, Any]]]}
)
class Outputs(TypedDict):
updates: NotRequired[List[Tuple[Any, Any]]]

_memory: Dict[A, V]
_change_buffer: List[Tuple[A, V]]
Expand Down
7 changes: 5 additions & 2 deletions src/tickit/devices/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class SinkDevice(Device):
"""A simple device which can take any input and produces no output."""

#: A typed mapping containing the 'input' input value
Inputs: type = TypedDict("Inputs", {"input": Any})
class Inputs(TypedDict):
input: Any

#: An empty typed mapping of device outputs
Outputs: type = TypedDict("Outputs", {})
class Outputs(TypedDict):
...

def update(self, time: SimTime, inputs: Inputs) -> DeviceUpdate[Outputs]:
"""The update method which logs the inputs and produces no outputs.
Expand Down
7 changes: 5 additions & 2 deletions src/tickit/devices/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class SourceDevice(Device):
"""A simple device which produces a pre-configured value."""

#: An empty typed mapping of device inputs
Inputs: type = TypedDict("Inputs", {})
class Inputs(TypedDict):
...

#: A typed mapping containing the 'value' output value
Outputs: type = TypedDict("Outputs", {"value": Any})
class Outputs(TypedDict):
value: Any

def __init__(self, value: Any) -> None:
"""A constructor of the source, which takes the pre-configured output value.
Expand Down

0 comments on commit ee66671

Please sign in to comment.