diff --git a/eppy/modeleditor.py b/eppy/modeleditor.py index 1124ba00..468f8c13 100644 --- a/eppy/modeleditor.py +++ b/eppy/modeleditor.py @@ -977,6 +977,37 @@ def savecopy(self, filename, lineendings='default', encoding='latin-1'): """ self.save(filename, lineendings, encoding) + def shallowcopy(self): + """Make a shallow copy of the IDF. + + This will remain connected to the original IDF, so that changes made to + either of the IDFs will be reflected in the other one. + + Returns + -------- + IDF object + + """ + return copy.copy(self) + + def deepcopy(self): + """Make a deep copy of the IDF. + + This will not remain connected to the original IDF, and so changes made + to the two IDFs will be independent of each other. + + Returns + -------- + IDF object + + """ + try: + return copy.deepcopy(self) # this will fail on Python 3 + except ValueError: + newidf = IDF() + newidf.initreadtxt(self.idfstr()) + return newidf + def getiddgroupdict(self): """Return a idd group dictionary sample: {'Plant-Condenser Loops': ['PlantLoop', 'CondenserLoop'], diff --git a/eppy/tests/test_modeleditor.py b/eppy/tests/test_modeleditor.py index 469669c6..c7d6a137 100644 --- a/eppy/tests/test_modeleditor.py +++ b/eppy/tests/test_modeleditor.py @@ -428,6 +428,60 @@ def test_savecopy(): assert idf.idfname == 'test.idf' +def test_shallowcopy(): + """Test that copies made of the IDF using idf.copy remain linked. + + Fails if changes to a copy are not reflected in the original, and vice + versa. + + """ + idf_txt = "Building, The White House, , , , , , , ;" + idf1 = IDF() + idf1.initreadtxt(idf_txt) + + # make a shallow copy that should remain "entangled" with idf1 + idf2 = idf1.shallowcopy() + + # change building name in idf1 and check in idf2 + obj = idf1.getobject('BUILDING', 'The White House') + obj.Name = 'Big Ben' + assert idf2.getobject('BUILDING', 'Big Ben') + assert idf1.idfstr() == idf2.idfstr() # the two IDFs are the same + + # change building name in idf2 and check in idf1 + obj = idf2.getobject('BUILDING', 'Big Ben') + obj.Name = 'The Colosseum' + assert idf1.getobject('BUILDING', 'The Colosseum') + assert idf1.idfstr() == idf2.idfstr() # the two IDFs are the same + + +def test_deepcopy(): + """Test that copies made of the IDF using idf.deepcopy do not remain linked. + + Fails if changes to a copy are reflected in the original, and vice + versa. + + """ + idf_txt = "Building, The White House, , , , , , , ;" + idf1 = IDF() + idf1.initreadtxt(idf_txt) + + # make a deep copy that is not linked to the original IDF + idf2 = idf1.deepcopy() + + # change building name in idf1 and check in idf2 + obj = idf1.getobject('BUILDING', 'The White House') + obj.Name = 'Big Ben' + assert idf2.getobject('BUILDING', 'The White House') # unchanged + assert idf1.idfstr() != idf2.idfstr() # the two IDFs are not the same + + # change building name in idf2 and check in idf1 + obj = idf2.getobject('BUILDING', 'The White House') + obj.Name = 'The Colosseum' + assert not idf1.getobject('BUILDING', 'The Colosseum') + assert idf1.idfstr() != idf2.idfstr() # the two IDFs are not the same + + def test_initread(): """Test for IDF.initread() with filename in unicode and as python str. """