Skip to content

Commit 0a0b8b9

Browse files
committed
Raise IndexError for Atom lookup by invalid type.
Fix check for any string label in structure indices.
1 parent 853424d commit 0a0b8b9

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

diffpy/Structure/structure.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""This module defines class Structure.
1717
"""
1818

19+
import collections
1920
import copy
2021
import numpy
2122
from diffpy.Structure.lattice import Lattice
@@ -381,8 +382,10 @@ def __getitem__(self, idx):
381382
except TypeError:
382383
pass
383384
# check if there is any string label that should be resolved
384-
scalarlabel = type(idx) is str
385-
hasstringlabel = scalarlabel or str in map(type, idx)
385+
scalarstringlabel = isinstance(idx, basestring)
386+
hasstringlabel = scalarstringlabel or (
387+
isinstance(idx, collections.Iterable) and
388+
any(isinstance(ii, basestring) for ii in idx))
386389
# if not, use numpy indexing to resolve idx
387390
if not hasstringlabel:
388391
idx1 = idx
@@ -410,7 +413,7 @@ def _resolveindex(aid):
410413
raise IndexError("Atom label %r is not unique." % aid)
411414
return aid1
412415
# generate new index object that has no strings
413-
if scalarlabel:
416+
if scalarstringlabel:
414417
idx2 = _resolveindex(idx)
415418
# for iterables preserved the tuple object type
416419
else:

diffpy/Structure/tests/TestStructure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def test___getitem__(self):
261261
self.assertTrue(stru[1] is stru['B'])
262262
stru[1].label = 'A'
263263
self.assertRaises(IndexError, stru.__getitem__, 'A')
264+
self.assertRaises(IndexError, stru.__getitem__, stru[0])
264265
return
265266

266267

0 commit comments

Comments
 (0)