Skip numpy types serialization errors on Message.Encoder.

This commit is contained in:
Fabio Manganiello 2023-10-06 22:54:19 +02:00
parent e5a5ac5ffb
commit 9acd71944c
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -34,21 +34,21 @@ class Message:
def parse_numpy(obj):
try:
import numpy as np
except ImportError:
return
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, decimal.Decimal):
return float(obj)
if isinstance(obj, (bytes, bytearray)):
return '0x' + ''.join([f'{x:02x}' for x in obj])
if callable(obj):
return '<function at {}.{}>'.format(obj.__module__, obj.__name__)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, decimal.Decimal):
return float(obj)
if isinstance(obj, (bytes, bytearray)):
return '0x' + ''.join([f'{x:02x}' for x in obj])
if callable(obj):
return '<function at {}.{}>'.format(obj.__module__, obj.__name__)
except (ImportError, TypeError):
pass
return