-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file_logger): attempt to JSON-serialize objects
- Loading branch information
Showing
3 changed files
with
66 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import jsonpickle | ||
import numpy as np | ||
|
||
|
||
def serialize(message: object) -> object: | ||
""" | ||
Serializes the message to a JSON serializable format. | ||
### Parameters | ||
---------- | ||
`message`: The message to serialize. | ||
### Returns | ||
---------- | ||
The serialized message. | ||
### Raises | ||
---------- | ||
- `TypeError`: If the message cannot be serialized. | ||
""" | ||
|
||
if isinstance(message, np.ndarray): | ||
return message.tolist() | ||
elif isinstance(message, dict): | ||
for key, value in message.items(): | ||
message[key] = serialize(value) | ||
return message | ||
elif hasattr(message, "toJSON"): | ||
return message.toJSON() # type:ignore[reportAttributeAccessIssue] | ||
elif hasattr(message, "to_json"): | ||
return message.to_json() # type:ignore[reportAttributeAccessIssue] | ||
elif hasattr(message, "to_dict"): | ||
return message.to_dict() # type:ignore[reportAttributeAccessIssue] | ||
elif hasattr(message, "__str__") and callable(getattr(message, "__str__")): | ||
return str(message) | ||
else: | ||
try: | ||
return dict(message) # type:ignore[reportCallIssue, reportArgumentType] | ||
except Exception: | ||
try: | ||
return jsonpickle.encode(message) | ||
except Exception as e: | ||
raise TypeError( | ||
f"Could not serialize the message: {message}. Error: {e}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build" | |
|
||
[project] | ||
name = "mloggers" | ||
version = "1.3.1" | ||
version = "1.3.2" | ||
authors = [ | ||
{ name = "Sergio Hernandez Gutierrez", email = "[email protected]" }, | ||
{ name = "Matteo Merler", email = "[email protected]" }, | ||
|
@@ -17,7 +17,15 @@ classifiers = [ | |
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
] | ||
dependencies = ["aenum", "numpy", "omegaconf", "termcolor", "wandb", "rich"] | ||
dependencies = [ | ||
"aenum", | ||
"jsonpickle", | ||
"numpy", | ||
"omegaconf", | ||
"termcolor", | ||
"wandb", | ||
"rich", | ||
] | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/serhez/mloggers" | ||
|