Skip to content

Commit

Permalink
fix deepcopy problem with controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
lthurner committed Jan 15, 2020
1 parent 5587aee commit abe3dc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pandapower/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import scipy as sp
from packaging import version
import six
import json

from pandapower.pypower.idx_brch import F_BUS, T_BUS, BR_STATUS
from pandapower.pypower.idx_bus import BUS_I, BUS_TYPE, NONE, PD, QD, VMIN, VMAX, PV
Expand Down Expand Up @@ -164,6 +165,11 @@ def __getattr__(self, key):

return self._build(self[key])

def __deepcopy__(self, memo):
from pandapower.file_io import PPJSONEncoder, from_json_string
json_string = json.dumps(self, cls=PPJSONEncoder)
return from_json_string(json_string)

@classmethod
def _valid_name(cls, key):
"""
Expand Down
9 changes: 9 additions & 0 deletions pandapower/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,15 @@ def from_json(cls, json_string):
d = json.loads(json_string, cls=PPJSONDecoder)
return cls.from_dict(d)

def __copy__(self):
print("hello")


def __deepcopy__(self, memo):
super().__deepcopy__(self, memo)
# cls = self.__class__
print("helloe")


def with_signature(obj, val, obj_module=None, obj_class=None):
if obj_module is None:
Expand Down
7 changes: 7 additions & 0 deletions pandapower/test/api/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandapower.topology as top
from pandapower.test.toolbox import assert_net_equal, create_test_network, tempdir, net_in
import pandapower.networks as nw
import pandapower.control as ct
from pandapower.io_utils import PPJSONEncoder, PPJSONDecoder
import json
import numpy as np
Expand Down Expand Up @@ -242,6 +243,12 @@ def test_json_io_same_net(net_in, tempdir):
net2 = pp.from_json(filename)
assert net2.controller.object.at[0].net is net2

def test_deepcopy_controller():
net = nw.mv_oberrhein()
ct.ContinuousTapControl(net, 114, 1.01)
assert net == net.controller.object.iloc[0].net
net2 = copy.copy(net)
assert net2 == net2.controller.object.iloc[0].net

if __name__ == "__main__":
pytest.main([__file__, "-x"])

0 comments on commit abe3dc9

Please sign in to comment.