Skip to content

Commit 42c0cc0

Browse files
authored
Add multiply method to Section class
Added a multiply method to the Section class for numeric values.
1 parent 6c49102 commit 42c0cc0

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/rheelDM/main.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ def add(self, key: str, value: Any):
189189
else:
190190
raise TypeError(f"Cannot add to type {typ}")
191191

192+
def multiply(self, key: str, factor: int | float):
193+
"""
194+
Multiply a numeric value by a factor.
195+
196+
Works for int and float types. Fractional values act as division.
197+
198+
Raises:
199+
TypeError if the current value is not int or float.
200+
"""
201+
if key not in self._items:
202+
raise KeyError(f"{key} does not exist in section {self.name}")
203+
typ, value = self._items[key]
204+
if typ not in (int, float):
205+
raise TypeError(f"Cannot multiply non-numeric type {typ}")
206+
self._items[key] = (typ, value * factor)
207+
192208
def extend(self, key: str, value: Any):
193209
"""
194210
Dynamically extend or combine values based on type:
@@ -433,4 +449,5 @@ def from_dict(cls, data: dict) -> Obj:
433449
section = obj.section(section_name)
434450
for key, (typ, value) in items.items():
435451
section.set(key, typ, value)
436-
return obj
452+
453+
return obj

0 commit comments

Comments
 (0)