Skip to content

Commit

Permalink
feat: csv and tsv support (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
rilshok authored Nov 29, 2024
2 parents 45b8e10 + 2f81fc4 commit 58242ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
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

0 comments on commit 58242ef

Please sign in to comment.