Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: csv and tsv support #33

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = [
"cryptography>=41.0.7",
"numpy>=1.21.1",
"soundfile>=0.12.1",
"pandas>=1.5.3",
]

[tool.setuptools.dynamic]
Expand Down
4 changes: 4 additions & 0 deletions src/iokit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__version__ = "0.2.0"
__all__ = [
"Csv",
"Dat",
"decrypt",
"download_file",
Expand All @@ -21,6 +22,7 @@
"SecretState",
"State",
"Tar",
"Tsv",
"Txt",
"Waveform",
"Wav",
Expand All @@ -29,6 +31,7 @@
]

from .extensions import (
Csv,
Dat,
Enc,
Env,
Expand All @@ -41,6 +44,7 @@
Ogg,
SecretState,
Tar,
Tsv,
Txt,
Wav,
Waveform,
Expand Down
3 changes: 3 additions & 0 deletions src/iokit/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = [
"Csv",
"Dat",
"Enc",
"Env",
Expand All @@ -11,6 +12,7 @@
"Ogg",
"SecretState",
"Tar",
"Tsv",
"Txt",
"Wav",
"Waveform",
Expand All @@ -28,6 +30,7 @@
from .json import Json
from .jsonl import Jsonl
from .npy import Npy
from .table import Csv, Tsv
from .tar import Tar
from .txt import Txt
from .yaml import Yaml
Expand Down
28 changes: 28 additions & 0 deletions src/iokit/extensions/table.py
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")
27 changes: 27 additions & 0 deletions tests/test_table.py
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