Skip to content

Commit ae85145

Browse files
committed
Add idf.copy and idf.deepcopy methods and tests
Implements #118
1 parent b03428e commit ae85145

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

eppy/modeleditor.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,37 @@ def savecopy(self, filename, lineendings='default', encoding='latin-1'):
977977
"""
978978
self.save(filename, lineendings, encoding)
979979

980+
def shallowcopy(self):
981+
"""Make a shallow copy of the IDF.
982+
983+
This will remain connected to the original IDF, so that changes made to
984+
either of the IDFs will be reflected in the other one.
985+
986+
Returns
987+
--------
988+
IDF object
989+
990+
"""
991+
return copy.copy(self)
992+
993+
def deepcopy(self):
994+
"""Make a deep copy of the IDF.
995+
996+
This will not remain connected to the original IDF, and so changes made
997+
to the two IDFs will be independent of each other.
998+
999+
Returns
1000+
--------
1001+
IDF object
1002+
1003+
"""
1004+
try:
1005+
return copy.deepcopy(self) # this will fail on Python 3
1006+
except ValueError:
1007+
newidf = IDF()
1008+
newidf.initreadtxt(self.idfstr())
1009+
return newidf
1010+
9801011
def getiddgroupdict(self):
9811012
"""Return a idd group dictionary
9821013
sample: {'Plant-Condenser Loops': ['PlantLoop', 'CondenserLoop'],

eppy/tests/test_modeleditor.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,60 @@ def test_savecopy():
428428
assert idf.idfname == 'test.idf'
429429

430430

431+
def test_shallowcopy():
432+
"""Test that copies made of the IDF using idf.copy remain linked.
433+
434+
Fails if changes to a copy are not reflected in the original, and vice
435+
versa.
436+
437+
"""
438+
idf_txt = "Building, The White House, , , , , , , ;"
439+
idf1 = IDF()
440+
idf1.initreadtxt(idf_txt)
441+
442+
# make a shallow copy that should remain "entangled" with idf1
443+
idf2 = idf1.shallowcopy()
444+
445+
# change building name in idf1 and check in idf2
446+
obj = idf1.getobject('BUILDING', 'The White House')
447+
obj.Name = 'Big Ben'
448+
assert idf2.getobject('BUILDING', 'Big Ben')
449+
assert idf1.idfstr() == idf2.idfstr() # the two IDFs are the same
450+
451+
# change building name in idf2 and check in idf1
452+
obj = idf2.getobject('BUILDING', 'Big Ben')
453+
obj.Name = 'The Colosseum'
454+
assert idf1.getobject('BUILDING', 'The Colosseum')
455+
assert idf1.idfstr() == idf2.idfstr() # the two IDFs are the same
456+
457+
458+
def test_deepcopy():
459+
"""Test that copies made of the IDF using idf.deepcopy do not remain linked.
460+
461+
Fails if changes to a copy are reflected in the original, and vice
462+
versa.
463+
464+
"""
465+
idf_txt = "Building, The White House, , , , , , , ;"
466+
idf1 = IDF()
467+
idf1.initreadtxt(idf_txt)
468+
469+
# make a deep copy that is not linked to the original IDF
470+
idf2 = idf1.deepcopy()
471+
472+
# change building name in idf1 and check in idf2
473+
obj = idf1.getobject('BUILDING', 'The White House')
474+
obj.Name = 'Big Ben'
475+
assert idf2.getobject('BUILDING', 'The White House') # unchanged
476+
assert idf1.idfstr() != idf2.idfstr() # the two IDFs are not the same
477+
478+
# change building name in idf2 and check in idf1
479+
obj = idf2.getobject('BUILDING', 'The White House')
480+
obj.Name = 'The Colosseum'
481+
assert not idf1.getobject('BUILDING', 'The Colosseum')
482+
assert idf1.idfstr() != idf2.idfstr() # the two IDFs are not the same
483+
484+
431485
def test_initread():
432486
"""Test for IDF.initread() with filename in unicode and as python str.
433487
"""

0 commit comments

Comments
 (0)