Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor object copying in EDisGo class #468

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions edisgo/edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ def reinforce(

"""
if copy_grid:
edisgo_obj = copy.deepcopy(self)
edisgo_obj = self.copy()
else:
edisgo_obj = self

Expand Down Expand Up @@ -3121,7 +3121,7 @@ def spatial_complexity_reduction(

"""
if copy_edisgo is True:
edisgo_obj = copy.deepcopy(self)
edisgo_obj = self.copy()
else:
edisgo_obj = self
busmap_df, linemap_df = spatial_complexity_reduction(
Expand Down Expand Up @@ -3335,6 +3335,44 @@ def resample_timeseries(
self.heat_pump.resample_timeseries(method=method, freq=freq)
self.overlying_grid.resample(method=method, freq=freq)

def copy(self, deep=True):
"""
Returns a copy of the object, with an option for a deep copy.

The SQLAlchemy engine is excluded from the copying process and restored
afterward.

Parameters
----------
deep : bool
If True, performs a deep copy; otherwise, performs a shallow copy.

Returns
---------
:class:`~.EDisGo`
Copied EDisGo object.

"""
tmp_engine = (
getattr(self, "engine", None)
if isinstance(getattr(self, "engine", None), Engine)
else None
)

if tmp_engine:
logging.info("Temporarily removing the SQLAlchemy engine before copying.")
self.engine = self.config._engine = None

cpy = copy.deepcopy(self) if deep else copy.copy(self)

if tmp_engine:
logging.info("Restoring the SQLAlchemy engine after copying.")
self.engine = self.config._engine = cpy.engine = cpy.config._engine = (
tmp_engine
)

return cpy


def import_edisgo_from_pickle(filename, path=""):
"""
Expand Down
Loading