Skip to content

Commit

Permalink
add dry_run docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
ahalev committed Mar 31, 2023
1 parent 2365982 commit f94b4e3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/pymgrid/utils/dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@

@contextmanager
def dry_run(pymgrid_object: Union['pymgrid.Microgrid', 'pymgrid.modules.base.BaseMicrogridModule']):
"""
A context manager to test modifications of a pymgrid object without modifying said object.
Parameters
----------
pymgrid_object : :class:`pymgrid.Microgrid` or :class:`pymgrid.modules.base.BaseMicrogridModule`
Microgrid or module to try run
Returns
-------
test_object : copy of object to test usage of, without modifying original object.
Examples
--------
>>> from pymgrid.modules import BatteryModule
>>> module = BatteryModule(0, 100, 50, 50, 0.9, init_soc=0.5)
>>> with dry_run(module) as test_module:
>>> test_module.step(test_module.max_act, normalized=False)
>>> print(f'Current step: {test_module.current_step}; current charge: {test_module.current_charge}')
Current step: 1; current charge: 0.0
>>> print(f'Current step: {module.current_step}; current charge: {module.current_charge}')
Current step: 0; current charge: 50.0
"""

serialized = yaml.safe_dump(pymgrid_object)

try:
Expand Down

0 comments on commit f94b4e3

Please sign in to comment.