Skip to content
Snippets Groups Projects
Commit 15c33d89 authored by Jammer, Tim's avatar Jammer, Tim
Browse files

Allowed the Instruction block to create instructions in place from srings for convenience

parent f8da1715
No related branches found
No related tags found
1 merge request!9Infrastructure: Type Hints, Instruction class and lists of instructions
......@@ -33,7 +33,7 @@ class InstructionBlock:
assert not isinstance(name, int)
self.name = name
def register_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all'):
def register_operation(self, op: str | Instruction | typing.List[Instruction], kind: str | int = 'all'):
"""
Registers an operation based on rank.
......@@ -43,7 +43,11 @@ class InstructionBlock:
- all: all Ranks execute this operation
- not0: all Ranks but the Root (rank 0) execute
- Or the integer of the rank that should execute
Note: if a str is passed as the operation, it will create a new Instruction from the given string
"""
if isinstance(op, str):
op = Instruction(op)
if kind == 'all':
if isinstance(op, list):
self.operations['all'].extend(op)
......@@ -132,7 +136,7 @@ class InstructionBlock:
as_int = int(index) # will Raise ValueError if not integer
return self.operations[kind][as_int]
def replace_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all',
def replace_operation(self, op: str | Instruction | typing.List[Instruction], kind: str | int = 'all',
index: str | int = 0):
"""
Replace the operation registered. will Raise IndexError if not present
......@@ -142,20 +146,25 @@ class InstructionBlock:
- index ('all' or int): the index of the operation within the given kind; 'all' means all operations will be replaced with the given list
Notes : if one wants to replace all operations one needs to provide a list
if one only wants to replace one operation: no list of operations is allowed
if a string is passed as the operation, it will create a new Instruction
"""
if isinstance(op, str):
op = Instruction(op)
if index == 'all':
if not isinstance(op, list):
raise ValueError('Provide List for replacement')
self.operations[kind] = op
else:
as_int = int(index) # will Raise ValueError if not integer
print(op)
if not isinstance(op, Instruction):
raise ValueError('Provide Instruction')
if len(self.operations[kind]) < as_int:
raise IndexError("Operation Not Found")
self.operations[kind][as_int] = op
def insert_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all',
def insert_operation(self, op: str | Instruction | typing.List[Instruction], kind: str | int = 'all',
before_index: int = 0):
"""
Inserts an operation before the specified one. will Raise IndexError if not present
......@@ -163,7 +172,10 @@ class InstructionBlock:
- op the new operation or list of operations
- kind ('all','not0' or integer): which ranks should execute the operation
- index (int): the index of the operation within the given kind
note: if str is passed as the operation, it will Create a New Instruction
"""
if isinstance(op, str):
op = Instruction(op)
as_int = int(before_index) # will Raise ValueError if not integer
if len(self.operations[kind]) < before_index:
raise IndexError("Operation Not Found")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment