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

added Type hints (#7) for the Template Manager and InstructionBlock classes

parent 6bf6c245
Branches
No related tags found
1 merge request!9Infrastructure: Type Hints, Instruction class and lists of instructions
from __future__ import annotations
import typing
from scripts.Infrastructure.Instruction import Instruction
from scripts.Infrastructure.MPICall import MPI_Call
......@@ -16,7 +21,7 @@ class InstructionBlock:
- `__str__(self)`: Converts the InstructionBlock instance to a string, replacing placeholders.
"""
def __init__(self, name=None):
def __init__(self, name: str = None):
"""
Initialize an empty InstructionBlock
......@@ -29,7 +34,7 @@ class InstructionBlock:
self.name = name
# TODO test if op also can be a list of operations
def register_operation(self, op, kind='all'):
def register_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all'):
"""
Registers an operation based on rank.
......@@ -50,21 +55,9 @@ class InstructionBlock:
self.operations[as_int] = []
self.operations[as_int].append(op)
def register_operations(self, ops, kind='all'):
"""
Registers a list of operations based on rank.
# TODO implement list part
Parameters:
- ops: The operations to register.
- kind: Rank to execute the operation ('all', 'not0', or integer).
- all: all Ranks execute this operation
- not0: all Ranks but the Root (rank 0) execute
- Or the integer of the rank that should execute
"""
for op in ops:
self.register_operation(op, kind)
def get_version(self):
def get_version(self) -> str:
"""
Retrieves the minimum required MPI version.
Returns:
......@@ -103,7 +96,7 @@ class InstructionBlock:
return result_str
def has_operation(self, kind='all', index=0):
def has_operation(self, kind: int | str = 'all', index: int = 0) -> bool:
"""
Checks if the Block has an operation with the given index and kind
Parameters:
......@@ -118,7 +111,7 @@ class InstructionBlock:
except (KeyError, IndexError) as e:
return False
def get_operation(self, kind='all', index=0):
def get_operation(self, kind: int | str = 'all', index: int = 0) -> Instruction | typing.List[Instruction]:
"""
Retrieve the operation registered. will Raise IndexError if not present
Parameters:
......@@ -129,17 +122,9 @@ class InstructionBlock:
"""
return self.operations[kind][index]
def get_operations(self, kind='all'):
"""
Retrieve all operations registered for the given kind.
Parameters:
- kind ('all','not0' or integer): which ranks should execute the operation
Returns:
str: List of all operations with given kind
"""
return self.operations[kind]
# todo implement list case
def replace_operation(self, op, kind='all', index=0):
def replace_operation(self, op: Instruction | typing.List[Instruction], kind: str | int = 'all', index: int = 0):
"""
Replace the operation registered. will Raise IndexError if not present
Parameters:
......@@ -151,7 +136,10 @@ class InstructionBlock:
raise IndexError("Operation Not Found")
self.operations[kind][index] = op
def insert_operation(self, op, kind='all', before_index=0):
# todo implement list caee
def insert_operation(self, op: 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
Parameters:
......@@ -163,7 +151,9 @@ class InstructionBlock:
raise IndexError("Operation Not Found")
self.operations[kind].insert(before_index, op)
def remove_operation(self, kind='all', index=0):
# todo implement list caee
def remove_operation(self, kind: str | int = 'all', index: int = 0):
"""
Removes the operation registered. will Raise IndexError if not present
Parameters:
......
......@@ -71,7 +71,7 @@ class TemplateManager:
- `get_short_descr(self)`: Retrieves the short description .
"""
def __init__(self, min_ranks=2, thread_level=None, has_finalize=True, has_init=True):
def __init__(self, min_ranks: int = 2, thread_level: str = None, has_finalize: bool = True, has_init: bool = True):
"""
Initialize the TemplateManager
......@@ -117,7 +117,7 @@ class TemplateManager:
.replace("@{version}@", version)
.replace("@{test_code}@", block_string))
def register_instruction_block(self, block):
def register_instruction_block(self, block: InstructionBlock):
"""
Registers an instruction block with the template. inserting it at the end, before the mpi finalize
Parameters:
......@@ -125,7 +125,7 @@ class TemplateManager:
"""
self._blocks.append(block)
def get_version(self):
def get_version(self) -> str:
"""
Retrieves the minimum required MPI version.
Returns:
......@@ -137,7 +137,7 @@ class TemplateManager:
max_v = max(block.get_version(), max_v)
return max_v
def set_description(self, descr_short, descr_full):
def set_description(self, descr_short: str, descr_full: str):
"""
Sets the short and full descriptions for the template.
Parameters:
......@@ -148,7 +148,7 @@ class TemplateManager:
self._descr_short = descr_short
# TODO one could write a function to check if short desc = filename is conforming with the naming sceme
def get_short_descr(self):
def get_short_descr(self) -> str:
"""
Retrieves the short description to use as a filename.
Returns:
......@@ -157,7 +157,7 @@ class TemplateManager:
assert self._descr_short != ""
return self._descr_short
def get_block(self, block_name=None, idx=None):
def get_block(self, block_name: str = None, idx: int = None) -> InstructionBlock:
"""
Retrieves the given Instruction Block Either by name or by index
Raises IndexError if the specified block is not found
......@@ -187,7 +187,7 @@ class TemplateManager:
raise ValueError("Neither Both block name nor index is given")
def insert_block(self, new_block, after_block_name=None, after_idx=None):
def insert_block(self, new_block: InstructionBlock, after_block_name: str = None, after_idx: int = None):
"""
inserts the given Instruction Block AFTER the one specified Either by name or by index
Raises IndexError if the specified block is not found
......@@ -217,7 +217,7 @@ class TemplateManager:
raise ValueError("Neither Both block name nor index is given")
def remove_block(self, block_name=None, idx=None):
def remove_block(self, block_name: str = None, idx: int = None):
"""
Removes the given Instruction Block Either by name or by index
Raises IndexError if the specified block is not found
......@@ -244,7 +244,7 @@ class TemplateManager:
raise ValueError("Neither Both block name nor index is given")
def replace_block(self, new_block, block_name=None, idx=None):
def replace_block(self, new_block: InstructionBlock, block_name: str = None, idx: int = None):
"""
Removes the given Instruction Block Either by name or by index
Raises IndexError if the specified block is not found
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment