Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handling datetime in client to_dict() #1314

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

manasaV3
Copy link
Contributor

@manasaV3 manasaV3 commented Nov 6, 2024

Calling to_dict() method raises serialization errors for objects that have a date time object. Ex: Dataset, Tomogram,..

This fix serializes the date time into the iso_format string.

Copy link
Contributor

@jgadling jgadling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This converts datetime objects (which imnsho are correct) to strings. Why do we need to do this?

def to_dict(self) -> Dict[str, Any]:
"""Return a dictionary representation of this object's attributes"""
return {k: getattr(self, k) for k in self._get_scalar_fields()}

def to_json_dict(self) -> Dict[str, Any]:
"""Return a JSON encoder friendly representation of this object's attributes"""
return {key: self._serialize(val) for key, val in self.to_dict().items()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python's json module already supplies a way to convert python values to json-compatible values (docs here and example implementation here ). Do we need to implement this functionality in our client, or can we leave it to users to work with python's standard library?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @uermel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants