Skip to content

Commit d8bac51

Browse files
committed
Add Structure.copy method for convenience.
Produce deep copy of a Structure or its derived type.
1 parent a427c01 commit d8bac51

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

diffpy/Structure/structure.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def __init__(self, atoms=[], lattice=None, title=None,
8888
return
8989

9090

91+
def copy(self):
92+
'''Return a deep copy of this Structure object.
93+
'''
94+
return copy.copy(self)
95+
96+
9197
def __copy__(self, target=None):
9298
'''Create a deep copy of this instance.
9399

diffpy/Structure/tests/TestStructure.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ def test_cartesian(self):
7575
# """
7676
# return
7777

78+
def test_copy(self):
79+
"""check Structure.copy()
80+
"""
81+
class MyDerivedStructure(Structure):
82+
def __copy__(self):
83+
rv = MyDerivedStructure()
84+
Structure.__copy__(self, target=rv)
85+
return rv
86+
pass
87+
pbte = self.pbte
88+
pbte2 = pbte.copy()
89+
self.assertFalse(pbte2.lattice is pbte.lattice)
90+
self.assertTrue(numpy.array_equal(pbte.xyz_cartn, pbte2.xyz_cartn))
91+
self.assertTrue(numpy.array_equal(pbte.U, pbte2.U))
92+
stru = MyDerivedStructure()
93+
stru += pbte2[pbte2.element.startswith('Pb')]
94+
pb3 = stru.copy()
95+
self.assertTrue(isinstance(pb3, MyDerivedStructure))
96+
self.assertTrue(all(pb3.element == 'Pb2+'))
97+
self.assertEqual(4, len(pb3))
98+
return
99+
78100
def test___copy__(self):
79101
"""check Structure.__copy__()
80102
"""

0 commit comments

Comments
 (0)