Skip to content

Commit 4ce786d

Browse files
authored
Refactor flatten_resolve_response (#228)
Move the logic from a standalone `flatten_respolve_response` function, to a method of the Response object. That way it complements `to_dict`, `to_json` with `to_flat_dict`.
1 parent 4ab26e9 commit 4ce786d

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

datacommons_client/endpoints/resolve.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
from typing import Any, Optional
1+
from typing import Optional
22

33
from datacommons_client.endpoints.base import API
44
from datacommons_client.endpoints.base import Endpoint
55
from datacommons_client.endpoints.payloads import ResolveRequestPayload
66
from datacommons_client.endpoints.response import ResolveResponse
77

88

9-
def flatten_resolve_response(
10-
data: ResolveResponse) -> dict[str, list[str] | str]:
11-
"""
12-
Flattens resolved candidate data into a dictionary where each node maps to its candidates.
13-
14-
Args:
15-
data (ResolveResponse): The response object containing the resolved data.
16-
17-
Returns:
18-
dict[str, Any]: A dictionary mapping nodes to their candidates.
19-
If a node has only one candidate, it maps directly to the candidate instead of a list.
20-
"""
21-
items: dict[str, Any] = {}
22-
23-
for entity in data.entities:
24-
node = entity.node
25-
if len(entity.candidates) == 1:
26-
items[node] = entity.candidates[0].dcid
27-
else:
28-
items[node] = [candidate.dcid for candidate in entity.candidates]
29-
30-
return items
31-
32-
339
def _resolve_correspondence_expression(from_type: str,
3410
to_type: str,
3511
entity_type: str | None = None) -> str:

datacommons_client/endpoints/response.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,22 @@ def from_json(cls, json_data: Dict[str, Any]) -> "ResolveResponse":
250250
return cls(entities=[
251251
Entity.from_json(entity) for entity in json_data.get("entities", [])
252252
])
253+
254+
def to_flat_dict(self) -> dict[str, list[str] | str]:
255+
"""
256+
Flattens resolved candidate data into a dictionary where each node maps to its candidates.
257+
258+
Returns:
259+
dict[str, Any]: A dictionary mapping nodes to their candidates.
260+
If a node has only one candidate, it maps directly to the candidate instead of a list.
261+
"""
262+
items: dict[str, Any] = {}
263+
264+
for entity in self.entities:
265+
node = entity.node
266+
if len(entity.candidates) == 1:
267+
items[node] = entity.candidates[0].dcid
268+
else:
269+
items[node] = [candidate.dcid for candidate in entity.candidates]
270+
271+
return items

datacommons_client/tests/endpoints/test_resolve_endpoint.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from datacommons_client.endpoints.base import API
44
from datacommons_client.endpoints.resolve import _resolve_correspondence_expression
5-
from datacommons_client.endpoints.resolve import flatten_resolve_response
65
from datacommons_client.endpoints.resolve import ResolveEndpoint
76
from datacommons_client.endpoints.response import ResolveResponse
87

@@ -119,7 +118,7 @@ def test_flatten_resolve_response():
119118
])
120119

121120
# Call the function
122-
result = flatten_resolve_response(mock_data)
121+
result = mock_data.to_flat_dict()
123122

124123
# Expected output
125124
expected = {

0 commit comments

Comments
 (0)