Skip to content
Snippets Groups Projects
Commit ddb95637 authored by Johannes Kung's avatar Johannes Kung
Browse files

Document and refactor breakpoints

parent 7e583355
No related branches found
No related tags found
1 merge request!27Docstring refactor of core
Pipeline #132038 failed
class Breakpoint: class Breakpoint:
"""
Base class for beakpoints, intended to be subclassed.
Attributes
----------
is_enabled : bool
Used to check and toggle if a breakpoint is on or off.
"""
def __init__(self): def __init__(self):
self.is_enabled = True self.is_enabled = True
def is_break(self) -> bool: def is_break(self) -> bool:
"""Check if the breakpoint has been reached.
Returns
-------
bool
``True`` if the breakpoint has been reached, ``False`` otherwise.
"""
return False return False
def set_enabled(self, is_enabled: bool) -> None: def set_enabled(self, is_enabled: bool) -> None:
"""Toggle the breakpoint on or off.
Parameters
----------
is_enabled : bool
``True`` to toggle on, ``False`` to toggle off.
"""
self.is_enabled = is_enabled self.is_enabled = is_enabled
...@@ -4,14 +4,30 @@ from simudator.core.breakpoint import Breakpoint ...@@ -4,14 +4,30 @@ from simudator.core.breakpoint import Breakpoint
class LambdaBreakpoint(Breakpoint): class LambdaBreakpoint(Breakpoint):
"""
A breakpoint that uses a given function to decide whether the breakpoint
has been reached or not.
Parameters
----------
lambda_func : Callable
The function used to decide if the breakpoint has been reached.
``lambda_func(**kwargs) -> bool``
Should return ``True`` if the breakpoint has been reached, ``False``
otherwise.
**kwargs
Arguments to pass to `lambda_func`.
"""
def __init__(self, lambda_func: Callable[..., bool], **kwargs) -> None: def __init__(self, lambda_func: Callable[..., bool], **kwargs) -> None:
super().__init__() super().__init__()
self.lambda_func = lambda_func self._lambda_func = lambda_func
self.kwargs = kwargs self._kwargs = kwargs
def is_break(self): def is_break(self):
return self.lambda_func(**self.kwargs) return self._lambda_func(**self._kwargs)
def __str__(self) -> str: def __str__(self) -> str:
s = f"Lambda {self.lambda_func.__name__} with kwargs {self.kwargs}" s = f"Lambda {self._lambda_func.__name__} with kwargs {self._kwargs}"
return s return s
...@@ -5,15 +5,29 @@ from simudator.core.modules import Memory ...@@ -5,15 +5,29 @@ from simudator.core.modules import Memory
class MemoryBreakpoint(Breakpoint): class MemoryBreakpoint(Breakpoint):
def __init__(self, memory: Memory, adress: int, value: Any) -> None: """
A breakpoint class for memory modules to break when a certain address
of the memory has a certain value.
Parameters
----------
memory : Memory
Memory module to check the contents of.
address : int
Address of the memory module to break at.
value : Any
Value of the address that triggers the breakpoint.
"""
def __init__(self, memory: Memory, address: int, value: Any) -> None:
super().__init__() super().__init__()
self.memory = memory self._memory = memory
self.adress = adress self._address = address
self.value = value self._value = value
def is_break(self): def is_break(self):
memory_content = self.memory.get_state()["memory"] memory_content = self._memory.get_state()["memory"]
return memory_content[self.adress] == self.value return memory_content[self._address] == self._value
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.memory.name} {self.adress}: {self.value}" return f"{self._memory.name} {self._address}: {self._value}"
...@@ -5,14 +5,28 @@ from simudator.core.module import Module ...@@ -5,14 +5,28 @@ from simudator.core.module import Module
class StateBreakpoint(Breakpoint): class StateBreakpoint(Breakpoint):
"""
A breakpoint class to break when the state variable for a module reaches
a specified value.
Parameters
----------
module : Module
Module to check the state of.
state : str
Name of the state variable of the module to check the value of.
value : Any
Value of the state variable to break at.
"""
def __init__(self, module: Module, state: str, value: Any) -> None: def __init__(self, module: Module, state: str, value: Any) -> None:
super().__init__() super().__init__()
self.module = module self._module = module
self.state = state self._state = state
self.value = value self._value = value
def is_break(self): def is_break(self):
return self.module.get_state()[self.state] == self.value return self._module.get_state()[self._state] == self._value
def __str__(self) -> str: def __str__(self) -> str:
return self.module.name + " " + self.state + " == " + str(self.value) return self._module.name + " " + self._state + " == " + str(self._value)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment