Skip to content

Commit 169030c

Browse files
authored
Merge pull request #4 from jamesrynn/master
Updated to be compatible with numpy>=2.0.0
2 parents f9f65a3 + 99d5c05 commit 169030c

4 files changed

Lines changed: 57 additions & 33 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ from numpyencoder import NumpyEncoder
1010
numpy_data = np.array([0, 1, 2, 3])
1111

1212
with open(json_file, 'w') as file:
13-
json.dump(numpy_data, file, indent=4, sort_keys=True,
14-
separators=(', ', ': '), ensure_ascii=False,
15-
cls=NumpyEncoder)
13+
json.dump(
14+
numpy_data,
15+
file,
16+
indent=4,
17+
sort_keys=True,
18+
separators=(', ', ': '),
19+
ensure_ascii=False,
20+
cls=NumpyEncoder
21+
)
1622
```

numpyencoder/numpyencoder.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,45 @@
33

44

55
class NumpyEncoder(json.JSONEncoder):
6-
""" Custom encoder for numpy data types """
6+
"""Custom encoder for numpy data types"""
7+
78
def default(self, obj):
8-
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
9-
np.int16, np.int32, np.int64, np.uint8,
10-
np.uint16, np.uint32, np.uint64)):
9+
if isinstance(
10+
obj,
11+
(
12+
np.int_,
13+
np.intc,
14+
np.intp,
15+
np.int8,
16+
np.int16,
17+
np.int32,
18+
np.int64,
19+
np.uint8,
20+
np.uint16,
21+
np.uint32,
22+
np.uint64,
23+
),
24+
):
1125

1226
return int(obj)
1327

14-
elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
28+
elif isinstance(obj, (np.float16, np.float32, np.float64)):
1529
return float(obj)
16-
30+
1731
elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
18-
return {'real': obj.real, 'imag': obj.imag}
19-
32+
return {"real": obj.real, "imag": obj.imag}
33+
2034
elif isinstance(obj, (np.ndarray,)):
2135
return obj.tolist()
22-
36+
2337
elif isinstance(obj, (np.bool_)):
2438
return bool(obj)
2539

26-
elif isinstance(obj, (np.void)):
40+
elif isinstance(obj, (np.void)):
2741
return None
2842

2943
return json.JSONEncoder.default(self, obj)
3044

3145

32-
if __name__ == '__main__':
46+
if __name__ == "__main__":
3347
numpy_encoder = NumpyEncoder()

setup.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
from setuptools import setup
22

3-
with open('README.md', 'r') as fh:
3+
with open("README.md", "r") as fh:
44
long_description = fh.read()
55

66
setup(
7-
name='numpyencoder',
8-
version='0.2.0',
9-
author='Hunter M. Allen',
10-
author_email='allenhm@gmail.com',
11-
license='MIT',
12-
#packages=find_packages(),
13-
packages=['numpyencoder'],
14-
install_requires=['numpy>=1.14.3'],
15-
description='Python JSON encoder for handling Numpy data types.',
7+
name="numpyencoder",
8+
version="0.2.0",
9+
author="Hunter M. Allen",
10+
author_email="allenhm@gmail.com",
11+
license="MIT",
12+
# packages=find_packages(),
13+
packages=["numpyencoder"],
14+
install_requires=["numpy>=1.14.3"],
15+
description="Python JSON encoder for handling Numpy data types.",
1616
long_description=long_description,
17-
long_description_content_type='text/markdown',
18-
url='https://github.com/hmallen/numpyencoder',
19-
keywords=['numpy', 'json', 'encoder'],
17+
long_description_content_type="text/markdown",
18+
url="https://github.com/hmallen/numpyencoder",
19+
keywords=["numpy", "json", "encoder"],
2020
classifiers=(
21-
'Programming Language :: Python :: 3',
22-
'License :: OSI Approved :: MIT License',
23-
'Operating System :: OS Independent',
21+
"Programming Language :: Python :: 3",
22+
"License :: OSI Approved :: MIT License",
23+
"Operating System :: OS Independent",
2424
),
2525
)

tests/test_example.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ def test_array_of_int():
66
numpy_data = np.array([np.int64(0), np.int32(1), 2, 3])
77
baseline_data = [0, 1, 2, 3]
88

9-
j_np = json.dumps(numpy_data, sort_keys=True,
10-
separators=(', ', ': '), ensure_ascii=False,
11-
cls=NumpyEncoder)
9+
j_np = json.dumps(
10+
numpy_data,
11+
sort_keys=True,
12+
separators=(", ", ": "),
13+
ensure_ascii=False,
14+
cls=NumpyEncoder,
15+
)
1216

1317
assert json.dumps(baseline_data, sort_keys=True) == j_np

0 commit comments

Comments
 (0)