Skip to content

Commit c36de26

Browse files
edavidajamachow
andauthored
feat: implement pin_write/read json type (#168)
* added implementation for json type * ci: do not test azure on external PRs * tests: check board pin_write type json Co-authored-by: Michael Chow <[email protected]>
1 parent ee580dd commit c36de26

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ jobs:
123123
- name: Run tests
124124
run: |
125125
# TODO: better way to disable all cloud backend tests?
126-
pytest pins -m 'not fs_rsc and not fs_s3 and not fs_gcs and not skip_on_github'
126+
pytest pins -m 'not fs_rsc and not fs_s3 and not fs_gcs and not fs_abfs and not skip_on_github'
127127
128128
129129
check-cross-compatibility:

pins/drivers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ def load_data(
105105
# TODO: update to handle multiple files
106106
return [str(Path(fs.open(path_to_file).name).absolute())]
107107

108+
elif meta.type == "json":
109+
import json
110+
111+
return json.load(fs.open(path_to_file))
112+
108113
raise NotImplementedError(f"No driver for type {meta.type}")
109114

110115

@@ -151,6 +156,12 @@ def save_data(
151156
import joblib
152157

153158
joblib.dump(obj, final_name)
159+
160+
elif type == "json":
161+
import json
162+
163+
json.dump(obj, open(final_name, "w"))
164+
154165
else:
155166
raise NotImplementedError(f"Cannot save type: {type}")
156167

pins/tests/test_boards.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ def test_board_pin_write_rsc_index_html(board, tmp_dir2, snapshot):
128128

129129

130130
@parametrize(
131-
"obj, type_", [(df, "csv"), (df, "joblib"), ({"a": 1, "b": [2, 3]}, "joblib")]
131+
"obj, type_",
132+
[
133+
(df, "csv"),
134+
(df, "joblib"),
135+
({"a": 1, "b": [2, 3]}, "joblib"),
136+
({"a": 1, "b": [2, 3]}, "json"),
137+
],
132138
)
133139
def test_board_pin_write_type(board, obj, type_, request):
134140
with rm_env(PINS_ENV_INSECURE_READ):

pins/tests/test_drivers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ def test_driver_roundtrip(tmp_dir2, type_):
7777
assert df.equals(obj)
7878

7979

80+
@pytest.mark.parametrize(
81+
"type_",
82+
[
83+
"json",
84+
],
85+
)
86+
def test_driver_roundtrip_json(tmp_dir2, type_):
87+
88+
df = {"x": [1, 2, 3]}
89+
90+
fname = "some_df"
91+
full_file = f"{fname}.{type_}"
92+
93+
p_obj = tmp_dir2 / fname
94+
res_fname = save_data(df, p_obj, type_)
95+
96+
assert Path(res_fname).name == full_file
97+
98+
meta = MetaRaw(full_file, type_, "my_pin")
99+
obj = load_data(meta, fsspec.filesystem("file"), tmp_dir2, allow_pickle_read=True)
100+
101+
assert df == obj
102+
103+
80104
def test_driver_feather_write_error(tmp_dir2):
81105
import pandas as pd
82106

0 commit comments

Comments
 (0)