-
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
5 changed files
with
63 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
__all__ = ["Csv", "Tsv"] | ||
|
||
from io import BytesIO | ||
from typing import Any | ||
|
||
from pandas import DataFrame, read_csv | ||
|
||
from iokit.state import State | ||
|
||
|
||
class Csv(State, suffix="json"): | ||
def __init__(self, frame: DataFrame, *, index: bool = False, **kwargs: Any): | ||
with BytesIO() as buffer: | ||
frame.to_csv(buffer, index=index) | ||
super().__init__(buffer.getvalue(), **kwargs) | ||
|
||
def load(self) -> DataFrame: | ||
return read_csv(self.buffer) | ||
|
||
|
||
class Tsv(State, suffix="tsv"): | ||
def __init__(self, frame: DataFrame, *, index: bool = False, **kwargs: Any): | ||
with BytesIO() as buffer: | ||
frame.to_csv(buffer, sep="\t", index=index) | ||
super().__init__(buffer.getvalue(), **kwargs) | ||
|
||
def load(self) -> DataFrame: | ||
return read_csv(self.buffer, sep="\t") |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pandas as pd | ||
|
||
from iokit import Csv, Tsv | ||
|
||
|
||
def _test_teble_data() -> list[dict[str, str | int]]: | ||
return [ | ||
{"name": "Alice", "age": 24}, | ||
{"name": "Bob", "age": 25}, | ||
{"name": "Charlie", "age": 26}, | ||
] | ||
|
||
|
||
def test_csv() -> None: | ||
frame = pd.DataFrame(_test_teble_data()) | ||
assert frame.equals(Csv(frame).load()) | ||
assert Csv(frame, index=True).size > Csv(frame).size | ||
assert Csv(frame).load().equals(Csv(frame, index=True).load()[["name", "age"]]) | ||
assert len([1 for ch in Csv(frame).data if ch == b","[0]]) == 4 | ||
|
||
|
||
def test_tsv() -> None: | ||
frame = pd.DataFrame(_test_teble_data()) | ||
assert frame.equals(Tsv(frame).load()) | ||
assert Tsv(frame, index=True).size > Tsv(frame).size | ||
assert Tsv(frame).load().equals(Tsv(frame, index=True).load()[["name", "age"]]) | ||
assert len([1 for ch in Tsv(frame).data if ch == b"\t"[0]]) == 4 |