Skip to content

Commit 853424d

Browse files
committed
Fix check for duplicate atom labels.
Raise IndexError when atom cannot be uniquely looked up by its string label.
1 parent c111a66 commit 853424d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

diffpy/Structure/structure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,11 @@ def __getitem__(self, idx):
395395
return rv
396396
# here we need to resolve at least one string label
397397
# build a map of labels to indices and mark duplicate labels
398-
duplicate = ()
398+
duplicate = object()
399399
labeltoindex = {}
400400
for i, a in enumerate(self):
401401
labeltoindex[a.label] = (
402-
a.label in labeltoindex and duplicate or i)
402+
duplicate if a.label in labeltoindex else i)
403403
def _resolveindex(aid):
404404
aid1 = aid
405405
if type(aid) is str:

diffpy/Structure/tests/TestStructure.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ def test___getitem__(self):
256256
self.failUnless(cdse[0] is cdse['Hohenzollern'])
257257
self.assertEqual([cdse[0], cdse[3], cdse[1]],
258258
cdse['Hohenzollern', 3:0:-2].tolist())
259+
stru.label = ['A', 'B']
260+
self.assertTrue(stru[0] is stru['A'])
261+
self.assertTrue(stru[1] is stru['B'])
262+
stru[1].label = 'A'
263+
self.assertRaises(IndexError, stru.__getitem__, 'A')
259264
return
260265

261266

0 commit comments

Comments
 (0)