core.utils.external

External Code

Some external code used in this project. Of course one could also use pip somtimes, but for adding a single class it is a bit overkill.

=> See licenses and links in the docstrings of the classes.

View Source
"""
# External Code

Some external code used in this project. Of course one could also use pip somtimes, but
for adding a single class it is a bit overkill.

=> See licenses and links in the docstrings of the classes.
"""

import json
import numpy as np

class NumpyEncoder(json.JSONEncoder):
	"""
		Custom encoder for numpy data types, used by `core.utils.functions.write_json_file`
		
		Source from https://github.com/hmallen/numpyencoder  
		MIT License  
		Copyright (c) 2018 Hunter Allen
	"""
	def default(self, obj):
		if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
			np.int16, np.int32, np.int64, np.uint8,
			np.uint16, np.uint32, np.uint64)):

			return int(obj)

		elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
			return float(obj)
		
		elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
			return {'real': obj.real, 'imag': obj.imag}
		
		elif isinstance(obj, (np.ndarray,)):
			return obj.tolist()
	
		elif isinstance(obj, (np.bool_)):
			return bool(obj)

		elif isinstance(obj, (np.void)): 
			return None

		return json.JSONEncoder.default(self, obj)
#   class NumpyEncoder(json.encoder.JSONEncoder):
View Source
class NumpyEncoder(json.JSONEncoder):
	"""
		Custom encoder for numpy data types, used by `core.utils.functions.write_json_file`
		
		Source from https://github.com/hmallen/numpyencoder  
		MIT License  
		Copyright (c) 2018 Hunter Allen
	"""
	def default(self, obj):
		if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
			np.int16, np.int32, np.int64, np.uint8,
			np.uint16, np.uint32, np.uint64)):

			return int(obj)

		elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
			return float(obj)
		
		elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
			return {'real': obj.real, 'imag': obj.imag}
		
		elif isinstance(obj, (np.ndarray,)):
			return obj.tolist()
	
		elif isinstance(obj, (np.bool_)):
			return bool(obj)

		elif isinstance(obj, (np.void)): 
			return None

		return json.JSONEncoder.default(self, obj)

Custom encoder for numpy data types, used by core.utils.functions.write_json_file

Source from https://github.com/hmallen/numpyencoder
MIT License
Copyright (c) 2018 Hunter Allen

#   def default(self, obj):
View Source
	def default(self, obj):
		if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
			np.int16, np.int32, np.int64, np.uint8,
			np.uint16, np.uint32, np.uint64)):

			return int(obj)

		elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
			return float(obj)
		
		elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
			return {'real': obj.real, 'imag': obj.imag}
		
		elif isinstance(obj, (np.ndarray,)):
			return obj.tolist()
	
		elif isinstance(obj, (np.bool_)):
			return bool(obj)

		elif isinstance(obj, (np.void)): 
			return None

		return json.JSONEncoder.default(self, obj)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this::

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
Inherited Members
json.encoder.JSONEncoder
JSONEncoder
item_separator
key_separator
encode
iterencode