Skip to content

Commit

Permalink
feat: v0.1.3 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
rilshok committed Jul 7, 2024
2 parents 88956f2 + d549a18 commit f685bcb
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Tests

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "*" ]

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

IOKit is a Python library that offers a suite of utilities for managing a wide range of input/output operations. Central to its design is the concept of a `State`, where each state signifies a unit of data that can be loaded, saved, or transformed. Each state corresponds to a valid file state, represented as a bytes-like object.

IOKit abstracts and unifies serialization and deserialization operations from various libraries into a single, cohesive interface. This allows for direct manipulation of the file's state in memory, eliminating the need for disk interaction. Consequently, it facilitates the (de)serialization of data in multiple formats, such as `json`, `txt`, `tar`, `gzip`, among others. This abstraction not only simplifies data handling but also enhances efficiency by reducing disk I/O operations.
IOKit abstracts and unifies serialization and deserialization operations from various libraries into a single, cohesive interface. This allows for direct manipulation of the file's state in memory, eliminating the need for disk interaction. Consequently, it facilitates the (de)serialization of data in multiple formats, such as `json`, `yaml`, `txt`, `tar`, `gzip`, among others. This abstraction not only simplifies data handling but also enhances efficiency by reducing disk I/O operations.

## Installation

Expand Down Expand Up @@ -48,6 +48,22 @@ single.json (16B)
{'key': 'value'}
```

### YAML

```python
from iokit import Yaml

data = {"key": "value"}
state = Yaml(data, name="single")
print(state)
print(state.load())
```

```plain-text
single.yaml (11B)
{'key': 'value'}
```

### GZip Compression

```python
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies = [
"humanize>=4.9.0",
"typing-extensions>=4.8.0",
"pytz>=2024.1",
"PyYAML>=6.0.1",
]

[tool.setuptools.dynamic]
Expand Down
5 changes: 3 additions & 2 deletions src/iokit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"Jsonl",
"Tar",
"Txt",
"Yaml",
"State",
"filter_states",
"find_state",
"load_file",
"save_file",
"save_temp",
]
__version__ = "0.1.2"
__version__ = "0.1.3"

from .extensions import Dat, Gzip, Json, Jsonl, Tar, Txt
from .extensions import Dat, Gzip, Json, Jsonl, Tar, Txt, Yaml
from .state import State, filter_states, find_state
from .storage import load_file, save_file, save_temp
2 changes: 2 additions & 0 deletions src/iokit/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Jsonl",
"Tar",
"Txt",
"Yaml",
]

from .dat import Dat
Expand All @@ -13,3 +14,4 @@
from .jsonl import Jsonl
from .tar import Tar
from .txt import Txt
from .yaml import Yaml
18 changes: 18 additions & 0 deletions src/iokit/extensions/yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
__all__ = [
"Yaml",
]

from typing import Any

import yaml

from iokit.state import State


class Yaml(State, suffix="yaml"):
def __init__(self, data: Any, **kwargs: Any):
data = yaml.safe_dump(data).encode("utf-8")
super().__init__(data=data, **kwargs)

def load(self) -> Any:
return yaml.safe_load(self.data)
40 changes: 40 additions & 0 deletions tests/test_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from iokit.extensions.yaml import Yaml


def test_yaml_empty() -> None:
state = Yaml([], name="empty")
assert state.name == "empty.yaml"
assert state.size == 3
assert state.data.getvalue() == b"[]\n"
assert not state.load()


def test_yaml_single() -> None:
state = Yaml({"key": "value"}, name="single")
assert state.name == "single.yaml"
assert state.load() == {"key": "value"}


def test_yaml_multiple() -> None:
state = Yaml({"first": 1, "second": 2}, name="multiple")
assert state.name == "multiple.yaml"
assert state.load() == {"first": 1, "second": 2}
assert state.size == 19


def test_yaml_different() -> None:
data = {
"list": [1, 2, 3],
"tuple": (4, 5, 6),
"dict": {"a": 1, "b": 2},
"str": "hello",
"int": 42,
}
state = Yaml(data, name="different")
assert state.name == "different.yaml"
loaded = state.load()
assert all(v1 == v2 for v1, v2 in zip(loaded["list"], [1, 2, 3]))
assert all(v1 == v2 for v1, v2 in zip(loaded["tuple"], (4, 5, 6)))
assert loaded["dict"] == {"a": 1, "b": 2}
assert loaded["str"] == "hello"
assert loaded["int"] == 42

0 comments on commit f685bcb

Please sign in to comment.