Rewrite bfxapi/rest/_Requests.py with type hinting. Add None values erasement in bfxapi/utils/JSONEncoder.py. Update code with new improvements.

This commit is contained in:
Davide Casale
2023-02-06 19:15:58 +01:00
parent 929ae62d2f
commit c588d9f20c
6 changed files with 86 additions and 96 deletions

View File

@@ -8,13 +8,16 @@ JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, Type[None]]
class JSONEncoder(json.JSONEncoder):
def encode(self, obj: JSON) -> str:
def _strip(dictionary: Dict) -> Dict:
return { key: value for key, value in dictionary.items() if value != None}
def _convert_float_to_str(data: JSON) -> JSON:
if isinstance(data, float):
return format(Decimal(repr(data)), "f")
elif isinstance(data, list):
return [ _convert_float_to_str(sub_data) for sub_data in data ]
elif isinstance(data, dict):
return { key: _convert_float_to_str(value) for key, value in data.items() }
return _strip({ key: _convert_float_to_str(value) for key, value in data.items() })
else: return data
data = _convert_float_to_str(obj)