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

Add idf.copy and idf.deepcopy methods and tests #124

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions eppy/modeleditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,35 @@ 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

"""
newidf = IDF()
newidf.initreadtxt(self.idfstr())
return newidf


@wrapped_help_text(run)
def run(self, **kwargs):
"""
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 @@ -453,6 +453,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