blob: f0e14aa53afc5c66706a13c0a0c672d0102b9a97 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# ymlstash
A simple ORM-like utility for operating on local YAML files via Python dataclasses.
## Install
Package is published on PyPI - https://pypi.org/project/ymlstash/
Install from pip or your favorite package manager:
```bash
$ pip install ymlstash
```
## Usage
Define a dataclass:
```python
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class User:
name: str
age: int
active: bool
key: ClassVar[str] = "name"
```
Note the special `key` field which is used to denote that `name` should be used as the primary key field. If an object has `name: "foo"`, it will be saved as `foo.yml` in the stash root directory.
Instantiate a new object:
```python
user = User(name="yuval", age=42, active=True)
```
Save it to file:
```python
from ymlstash import YmlStash
stash = YmlStash(User, "path/to/db")
stash.save(user)
```
This will create a `yuval.yml` file in the stash root directory.
When saving to file, a `key` field must be present on the dataclass, otherwise an explicit `key` must be passed:
```python
stash.save(obj, key="custom-key")
```
Load from file:
```python
user = stash.load("yuval")
```
List all keys existing in stash:
```python
keys = stash.list_keys()
```
Delete a key:
```python
stash.delete("foo")
```
Check for key existance:
```python
stash.exists("foo")
```
Drop all files (careful, this deletes everything):
```python
stash.drop()
```
## License
[MIT](LICENSE)
|