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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
from datetime import datetime
import json
import logging
from datetime import date, datetime
from importlib import import_module
from typing import Any, Dict, Iterable, Optional

Expand Down Expand Up @@ -215,6 +217,17 @@ 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 _serialize_datetime(self, value):
if isinstance(value, (datetime, date)):
return value.isoformat()
raise TypeError("Unknown type")

def print_json(self) -> None:
"""Prints a JSON representation of this object's scalar attributes"""
logging.info(
json.dumps(self.to_dict(), indent=2, default=self._serialize_datetime),
)

@classmethod
@functools.lru_cache(maxsize=32)
def _get_scalar_fields(cls):
Expand Down
2 changes: 1 addition & 1 deletion docs/cryoet_data_portal_docsite_quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ for tomo in tomos:
print(tomo.name)

# Print the tomogram metadata as a json string
print(json.dumps(tomo.to_dict(), indent=4))
tomo.print_json()

# Download a tomogram in the MRC format (uncomment to actually download files)
# tomo.download_mrcfile()
Expand Down
Loading