Skip to content

Commit 8eeeff6

Browse files
committed
move serializable mixin
1 parent a2017fa commit 8eeeff6

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

datacommons_client/endpoints/response.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from dataclasses import asdict
21
from dataclasses import dataclass
32
from dataclasses import field
4-
import json
53
from typing import Any, Dict, List
64

75
from datacommons_client.models.node import Arcs
@@ -15,42 +13,7 @@
1513
from datacommons_client.models.resolve import Entity
1614
from datacommons_client.utils.data_processing import flatten_properties
1715
from datacommons_client.utils.data_processing import observations_as_records
18-
19-
20-
class SerializableMixin:
21-
"""Provides serialization methods for the Response dataclasses."""
22-
23-
def to_dict(self, exclude_none: bool = True) -> Dict[str, Any]:
24-
"""Converts the instance to a dictionary.
25-
26-
Args:
27-
exclude_none: If True, only include non-empty values in the response.
28-
29-
Returns:
30-
Dict[str, Any]: The dictionary representation of the instance.
31-
"""
32-
33-
def _remove_none(data: Any) -> Any:
34-
"""Recursively removes None or empty values from a dictionary or list."""
35-
if isinstance(data, dict):
36-
return {k: _remove_none(v) for k, v in data.items() if v is not None}
37-
elif isinstance(data, list):
38-
return [_remove_none(item) for item in data]
39-
return data
40-
41-
result = asdict(self)
42-
return _remove_none(result) if exclude_none else result
43-
44-
def to_json(self, exclude_none: bool = True) -> str:
45-
"""Converts the instance to a JSON string.
46-
47-
Args:
48-
exclude_none: If True, only include non-empty values in the response.
49-
50-
Returns:
51-
str: The JSON string representation of the instance.
52-
"""
53-
return json.dumps(self.to_dict(exclude_none=exclude_none), indent=2)
16+
from datacommons_client.utils.data_processing import SerializableMixin
5417

5518

5619
@dataclass

datacommons_client/utils/data_processing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import asdict
2+
import json
23
from typing import Any, Dict
34

45

@@ -93,3 +94,39 @@ def observations_as_records(data: dict, facets: dict) -> list[dict]:
9394
facet_metadata=facets,
9495
)
9596
]
97+
98+
99+
class SerializableMixin:
100+
"""Provides serialization methods for the Response dataclasses."""
101+
102+
def to_dict(self, exclude_none: bool = True) -> Dict[str, Any]:
103+
"""Converts the instance to a dictionary.
104+
105+
Args:
106+
exclude_none: If True, only include non-empty values in the response.
107+
108+
Returns:
109+
Dict[str, Any]: The dictionary representation of the instance.
110+
"""
111+
112+
def _remove_none(data: Any) -> Any:
113+
"""Recursively removes None or empty values from a dictionary or list."""
114+
if isinstance(data, dict):
115+
return {k: _remove_none(v) for k, v in data.items() if v is not None}
116+
elif isinstance(data, list):
117+
return [_remove_none(item) for item in data]
118+
return data
119+
120+
result = asdict(self)
121+
return _remove_none(result) if exclude_none else result
122+
123+
def to_json(self, exclude_none: bool = True) -> str:
124+
"""Converts the instance to a JSON string.
125+
126+
Args:
127+
exclude_none: If True, only include non-empty values in the response.
128+
129+
Returns:
130+
str: The JSON string representation of the instance.
131+
"""
132+
return json.dumps(self.to_dict(exclude_none=exclude_none), indent=2)

0 commit comments

Comments
 (0)