Skip to content

Commit 7fddbfa

Browse files
committed
td.py: allow upper limit for block creation (raises ValueError in case)
1 parent 6c98a52 commit 7fddbfa

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/pyblocksim/td.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,17 @@ def register_block(self, block: "TDBlock"):
112112

113113

114114
class TDBlock:
115+
max_blocks_of_this_type = float("inf") # might be overwritten by by subclasses
115116
n_states = None
116117
instance_counter: int = None
117118

118119
def __init__(self, name: str = None, input1=None, params=None, **kwargs):
119120
# increment the correct class attribute
120121
type(self).instance_counter += 1
122+
if self.instance_counter > self.max_blocks_of_this_type:
123+
msg = f"Cannot create another instance of '{type(self)}'. Max. instance number reached."
124+
raise ValueError(msg)
125+
121126
self.state_vars = ds.get_state_vars(self.n_states)
122127

123128
# to calculate and store the output

0 commit comments

Comments
 (0)