Skip to content

Commit 8cbf535

Browse files
committed
Added support for Python 3
1 parent 03401bf commit 8cbf535

6 files changed

Lines changed: 26 additions & 6 deletions

File tree

dpath/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
# Python version flags for Python 3 support
4+
PY2 = ( sys.version_info.major == 2 )
5+
PY3 = ( sys.version_info.major == 3 )

dpath/path.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dpath import PY3
12
import dpath.exceptions
23
import re
34
import fnmatch
@@ -76,8 +77,17 @@ def paths(obj, dirs=True, leaves=True, path=[], skip=False, separator="/"):
7677
7778
"""
7879
if isinstance(obj, dict):
79-
for (k, v) in obj.iteritems():
80-
if issubclass(k.__class__, (basestring)) and skip and k[0] == '+':
80+
81+
# Python 3 support
82+
if PY3:
83+
iteritems = obj.items()
84+
string_class = str
85+
else: # Default to PY2
86+
iteritems = obj.iteritems()
87+
string_class = basestring
88+
89+
for (k, v) in iteritems:
90+
if issubclass(k.__class__, (string_class)) and skip and k[0] == '+':
8191
continue
8292
newpath = path + [[k, v.__class__]]
8393
validate(newpath, separator=separator)
@@ -125,7 +135,11 @@ def match(path, glob):
125135
ss_glob = glob[:ss] + glob[ss + 1:]
126136

127137
if path_len == len(ss_glob):
128-
return all(map(fnmatch.fnmatch, map(str, paths_only(path)), map(str, ss_glob)))
138+
# Python 3 support
139+
if PY3:
140+
return all(map(fnmatch.fnmatch, list(map(str, paths_only(path))), list(map(str, ss_glob))))
141+
else: # Default to Python 2
142+
return all(map(fnmatch.fnmatch, map(str, paths_only(path)), map(str, ss_glob)))
129143

130144
return False
131145

dpath/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def delete(obj, glob, separator="/", afilter=None):
3838
prev = cur
3939
try:
4040
cur = cur[item[0]]
41-
except AttributeError, e:
41+
except AttributeError as e:
4242
# This only happens when we delete X/Y and the next
4343
# item in the paths is X/Y/Z
4444
pass

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'License :: OSI Approved :: MIT License',
3838
'Natural Language :: English',
3939
'Programming Language :: Python :: 2.7',
40+
'Programming Language :: Python :: 3',
4041
'Topic :: Software Development :: Libraries :: Python Modules',
4142
],
4243
)

tests/test_util_merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_merge_typesafe_and_separator():
1616
}
1717
try:
1818
dpath.util.merge(dst, src, flags=(dpath.util.MERGE_ADDITIVE | dpath.util.MERGE_TYPESAFE), separator=";")
19-
except TypeError, e:
19+
except TypeError as e:
2020
assert(str(e).endswith("dict;integer"))
2121
return
2222
raise Exception("MERGE_TYPESAFE failed to raise an exception when merging between str and int!")

tests/test_util_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def afilter(x):
4242
}
4343
}
4444
dpath.util.set(dict, '/a/*', 31337, afilter=afilter)
45-
print dict
45+
print(dict)
4646
assert (dict['a']['b'] == 0)
4747
assert (dict['a']['c'] == 1)
4848
assert (dict['a']['d'] == 31337)

0 commit comments

Comments
 (0)