-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
338 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "packio" | ||
version = "0.0.4" | ||
version = "0.0.3" | ||
description = "IO for multiple python objects to/from a single file" | ||
authors = [{ name = "Zach Kurtz", email = "[email protected]" }] | ||
readme = "README.md" | ||
|
@@ -14,6 +14,9 @@ dev = [ | |
"pytest >=8.3.2", | ||
"sphinx>=8.1.3", | ||
"sphinx-rtd-theme>=3.0.2", | ||
"pandas>=2.2.3", | ||
"dummio>=1.1.0", | ||
"fastparquet>=2024.11.0", | ||
] | ||
|
||
[project.urls] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,26 @@ | ||
"""Main tests of packio.""" | ||
|
||
import json | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
import dummio | ||
import pandas as pd | ||
from packio import Reader, Writer | ||
|
||
|
||
@dataclass | ||
class MyData: | ||
"""A simple data class for testing. | ||
Attributes: | ||
documentation: Description of what this class is all about. | ||
lookup: A dictionary. | ||
""" | ||
|
||
documentation: str | ||
lookup: dict[str, int] | ||
|
||
def save(self, path: Path) -> None: | ||
"""Save the data class to disk.""" | ||
with Writer(path) as writer: | ||
writer.file("documentation.txt").write_text(self.documentation) | ||
with writer.file("lookup.json").open("w") as f: | ||
json.dump(self.lookup, f) | ||
def test_io(tmp_path) -> None: | ||
"""Test the dummio package.""" | ||
# define some objects | ||
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
lookup = {"a": 1, "b": 2} | ||
|
||
@classmethod | ||
def from_file(cls, path: Path) -> "MyData": | ||
"""Load the data class from disk.""" | ||
with Reader(path) as reader: | ||
documentation = reader.file("documentation.txt").read_text() | ||
with reader.file("lookup.json").open() as f: | ||
lookup = json.load(f) | ||
return cls(documentation=documentation, lookup=lookup) | ||
# save the objects to a single file | ||
filepath = tmp_path / "data.packio" | ||
with Writer(filepath) as writer: | ||
df.to_parquet(writer.file("df.parquet")) | ||
dummio.json.save(lookup, filepath=writer.file("lookup.json")) | ||
|
||
# load the objects from the file | ||
with Reader(filepath) as reader: | ||
df2 = pd.read_parquet(reader.file("df.parquet")) | ||
lookup2 = dummio.json.load(reader.file("lookup.json")) | ||
|
||
def test_packio(tmp_path): | ||
"""Test the packio package.""" | ||
data = MyData( | ||
documentation="This is a test.", | ||
lookup={"a": 1, "b": 2}, | ||
) | ||
data.save(tmp_path / "data.mydata") | ||
loaded = MyData.from_file(tmp_path / "data.mydata") | ||
assert loaded.documentation == data.documentation | ||
assert loaded.lookup == data.lookup | ||
assert df.equals(df2) | ||
assert lookup == lookup2 |
Oops, something went wrong.