Skip to content

Commit 5ba9300

Browse files
committed
MNT: add isiterable utility function
Also fix deprecation warning in Python 3.7 on `Iterable` move to `collections.abc`.
1 parent 708a754 commit 5ba9300

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/diffpy/structure/structure.py

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

19-
import collections
2019
import copy
2120
import numpy
2221
import codecs
@@ -25,6 +24,7 @@
2524
from diffpy.structure.lattice import Lattice
2625
from diffpy.structure.atom import Atom
2726
from diffpy.structure.utils import _linkAtomAttribute, atomBareSymbol
27+
from diffpy.structure.utils import isiterable
2828

2929
# ----------------------------------------------------------------------------
3030

@@ -388,7 +388,7 @@ def __getitem__(self, idx):
388388
# check if there is any string label that should be resolved
389389
scalarstringlabel = isinstance(idx, six.string_types)
390390
hasstringlabel = scalarstringlabel or (
391-
isinstance(idx, collections.Iterable) and
391+
isiterable(idx) and
392392
any(isinstance(ii, six.string_types) for ii in idx))
393393
# if not, use numpy indexing to resolve idx
394394
if not hasstringlabel:

src/diffpy/structure/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@
1616
"""Small shared functions.
1717
"""
1818

19+
import six
1920
import numpy
2021

22+
if six.PY2:
23+
from collections import Iterable as _Iterable
24+
else:
25+
from collections.abc import Iterable as _Iterable
26+
27+
28+
def isiterable(obj):
29+
"""True if argument is iterable."""
30+
rv = isinstance(obj, _Iterable)
31+
return rv
32+
2133

2234
def isfloat(s):
2335
"""True if argument can be converted to float"""

0 commit comments

Comments
 (0)