diff --git a/client/python/cryoet_data_portal/src/cryoet_data_portal/_gql_base.py b/client/python/cryoet_data_portal/src/cryoet_data_portal/_gql_base.py index aa8a2a326..0ab58d193 100644 --- a/client/python/cryoet_data_portal/src/cryoet_data_portal/_gql_base.py +++ b/client/python/cryoet_data_portal/src/cryoet_data_portal/_gql_base.py @@ -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 @@ -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): diff --git a/docs/cryoet_data_portal_docsite_quick_start.md b/docs/cryoet_data_portal_docsite_quick_start.md index 73595a1d3..d39c35a61 100644 --- a/docs/cryoet_data_portal_docsite_quick_start.md +++ b/docs/cryoet_data_portal_docsite_quick_start.md @@ -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()