Skip to content

Commit daa4239

Browse files
Separate write_json from to_json (CQS)
1 parent dd49569 commit daa4239

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

pins/_adaptors.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,14 @@ class Adaptor:
2424
def __init__(self, data: Any) -> None:
2525
self._d = data
2626

27-
@overload
28-
def write_json(self, file: str) -> None: ...
29-
@overload
30-
def write_json(self, file: None = ...) -> str: ...
31-
def write_json(self, file: str | None = None) -> str | None:
32-
if file is None:
33-
msg = (
34-
f"Writing to JSON string rather than file is not supported for "
35-
f"{type(self._d)}"
36-
)
37-
raise NotImplementedError(msg)
27+
def write_json(self, file: str) -> None:
28+
with open(file, "w") as f:
29+
f.write(self.to_json())
3830

31+
def to_json(self) -> str:
3932
import json
4033

41-
json.dump(self._d, open(file, mode="w"))
34+
return json.dumps(self._d)
4235

4336
def write_joblib(self, file: str) -> None:
4437
import joblib
@@ -101,7 +94,7 @@ def head(self, n: int) -> DFAdaptor: ...
10194
def data_preview(self) -> str:
10295
# TODO(compat) is 100 hard-coded?
10396
# Note that we go df -> json -> dict, to take advantage of type conversions in the dataframe library
104-
data: list[dict[Any, Any]] = json.loads(self.head(100).write_json())
97+
data: list[dict[Any, Any]] = json.loads(self.head(100).to_json())
10598
columns = [
10699
{"name": [col], "label": [col], "align": ["left"], "type": [""]}
107100
for col in self.columns
@@ -135,18 +128,7 @@ def shape(self) -> tuple[int, int]:
135128
def head(self, n: int) -> PandasAdaptor:
136129
return PandasAdaptor(self._d.head(n))
137130

138-
@overload
139-
def write_json(self, file: str) -> None: ...
140-
@overload
141-
def write_json(self, file: None) -> str: ...
142-
def write_json(self, file: str | None = None) -> str | None:
143-
if file is not None:
144-
msg = (
145-
f"Writing to file rather than JSON string is not supported for "
146-
f"{type(self._d)}"
147-
)
148-
raise NotImplementedError(msg)
149-
131+
def to_json(self) -> str:
150132
return self._d.to_json(orient="records")
151133

152134
def write_csv(self, file: str) -> None:

pins/tests/test_adaptors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ def test_head(self):
107107
assert isinstance(adaptor.head(1), PandasAdaptor)
108108
assert_frame_equal(adaptor.head(1)._d, expected._d)
109109

110-
def test_write_json(self):
110+
def test_to_json(self):
111111
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
112112
adaptor = PandasAdaptor(df)
113113
assert isinstance(adaptor, DFAdaptor)
114-
assert adaptor.write_json() == """[{"a":1,"b":4},{"a":2,"b":5},{"a":3,"b":6}]"""
114+
assert adaptor.to_json() == """[{"a":1,"b":4},{"a":2,"b":5},{"a":3,"b":6}]"""
115115

116116
def test_write_csv(self, tmp_path: Path):
117117
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

0 commit comments

Comments
 (0)