Skip to content
Snippets Groups Projects

Docstring refactor of core

Merged Johannes Kung requested to merge docstring_refactor into main
All threads resolved!
1 file
+ 93
41
Compare changes
  • Side-by-side
  • Inline
+ 93
41
@@ -6,8 +6,26 @@ from simudator.core.signal import Signal
class Module:
"""
This class specifies the basic functionality of all module types.
The modules are processor componenets in the processor,
The modules are processor components in the processor,
such as ALU, busses, registers and more.
Parameters
----------
signals : dict[str, Signal]
Dictionary containing all signals connected to the module. Input
signals should have keys beginning with `"in_"` and output signals keys
beginning with "out_".
name : str
Name of the module.
Attributes
----------
name : str
Name of the module.
signals : dict[str, Signal]
The signals connected to this module. The key for a signal can be
regarded as the name of a signal 'port' of the module. The keys should
be prefixed with 'in_' for input signals and 'out_' for output signals.
"""
def __init__(self, signals: dict[str, Signal], name: str = "") -> None:
@@ -19,29 +37,38 @@ class Module:
signal.add_destination(self)
def update_register(self) -> None:
"""Simulate module behaviour for saving input into the internal state
during a clock tick.
This method should be used for any module that has any 'internal'
registers. Exact behaviour is specified in each module subclass.
"""
Simulates module behaviour for saving input into the internal state
during a clock tick. Exact behaviour is specifid in each module type.
"""
raise NotImplemented
raise NotImplementedError
def output_register(self) -> None:
"""Simulate module behaviour for outputting data from internal
registers, if any, to output signals during a clock tick.
Exact behaviour is specified in each module subclass.
"""
Simulates module behaviour for giving data to output signals
during a clock tick. Exact behaviour is specifid in each module type.
"""
raise NotImplemented
raise NotImplementedError
def update_logic(self) -> None:
"""
Simulates behaviour for sending output using the internal state
during a clock tick. Exact behaviour is specifid in each module type.
Simulate behaviour for sending output using the internal state
during a clock tick. Exact behaviour is specified in each module type.
"""
raise NotImplemented
raise NotImplementedError
def get_state(self) -> dict:
"""
Returns a dict of the module states.
Return the state of the module.
Returns
-------
dict[str, Any]
State of the module represented as a dictionary with one key for
each state variable.
"""
state_dict = dict()
state_dict["name"] = self.name
@@ -49,7 +76,12 @@ class Module:
def get_gui_state(self) -> dict:
"""
Returns a dict of the module states that should be displayed in a GUI.
Return the state of the module as should be displayed in a GUI.
See Also
--------
get_state :
Similar method that includes state variables not related to the GUI.
"""
state_dict = dict()
state_dict["name"] = self.name
@@ -57,42 +89,56 @@ class Module:
def set_state(self, state: dict) -> None:
"""
Sets the modules state to one given in dict. Dict format will be
diffrent for each type of module so use get_state to get correct format.
Set the state of the module.
Parameters
----------
state : dict[str, Any]
Module state represented as a dictionary.
"""
self.name = state["name"]
def get_output_signals(self) -> list[Signal]:
"""
Returns the modules output signals. Assumes all output signals are
stored with a key beginning in 'out' in the signals dictionary.
"""Return the output signals of the module.
Assumes all output signals are stored with a key beginning in 'out' in
the signals dictionary.
Returns
-------
list[Signal]
List of all output signals of this module.
"""
return [signal for key, signal in self.signals.items() if key[0:3] == "out"]
def get_input_signals(self) -> list[Signal]:
"""
Returns the modules input signals. Assumes all input signals are stored
with a key beginning in 'in' in the signals dictionary.
"""Return the input signals of the module.
Assumes all input signals are stored with a key beginning in 'in' in
the signals dictionary.
Returns
-------
list[Signal]
List of all input signals of this module.
"""
return [signal for key, signal in self.signals.items() if key[0:2] == "in"]
def reset(self) -> None:
"""
Resets the module to its default state.
"""
raise NotImplemented
"""Reset the module to its default state."""
raise NotImplementedError
def load_from_str(self, state_string):
"""
Sets the moudles state according to a string
Each module type will decide how the string should be formated.
def load_from_str(self, state_string) -> None:
"""Set the module state according to a string.
Each module subclass decides the format of the string.
"""
raise NotImplemented
raise NotImplementedError
def get_longest_line_len(self, ignore_keys=None) -> int:
"""
Helper function for pretty_print that returns the length of
the longest line to print for a module.
"""return the length of the longest line when printing the module.
Helper function for pretty_print.
"""
if ignore_keys == None:
@@ -110,17 +156,23 @@ class Module:
return longest_line_len
def print_module(self) -> None:
"""
Prints the module directly to terminal
"""
"""Print the module."""
print(self.name)
def save_state_to_file(self, file_path: str) -> bool:
"""Save the modules state to file.
Parameters
----------
file_path : str
Path to the file to save to.
Returns
-------
bool
True if saving succeeded, False otherwise.
"""
Tries to save the modules state to a given file.
Returns true on success and false if something went wrong.
"""
raise NotImplemented
raise NotImplementedError
def __str__(self) -> str:
return self.name
Loading