Skip to content

Commit

Permalink
Add idf.copy and idf.deepcopy methods and tests
Browse files Browse the repository at this point in the history
Implements #118
  • Loading branch information
jamiebull1 committed Sep 7, 2016
1 parent b03428e commit ae85145
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
31 changes: 31 additions & 0 deletions eppy/modeleditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
54 changes: 54 additions & 0 deletions eppy/tests/test_modeleditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down

0 comments on commit ae85145

Please sign in to comment.