Python dictionaries that you can easily save, restore and edit as files
Project description
Yobs: Python dictionaries that you can easily save, restore and edit as files
A Yob (yaml file object) is a python dictionary that you can easily save, restore and edit as a file.
Yobs also come with an optional simple but powerful file oriented database.
Yobs can be arbitrarily complex and may leverage the serialization/deserialization capabilities of PyYAML if needed.
While yobs are reasonably performant they are individual yaml files at heart.
Specialize yobs to serve up JSON or models with any behavior based upon their data that you fancy.
Not tested on Windows.
Use a lonely yob for configuration
In [1]: from yob import Yob
In [2]: y = Yob('/tmp/yobs/A')
# Use a context manager to automatically save any changes
In [3]: with y:
...: y['base_path'] = '/tmp/yobs'
...:
In [4]: y
Out[4]: {'base_path': '/tmp/yobs'}
# ...or explicitly save after changes
In [5]: y['max'] = 99
In [6]: y.save()
In [7]: y
Out[7]: {'base_path': '/tmp/yobs', 'max': 99}
# Use it like any other dictionary
In [8]: y['max']
Out [8]: 99
You can also edit the file directly - it is just yaml.
Or put convivial yobs together as a file based database
The database is created from a root folder. Sub folders are generated from the unique identifier used for each yob in the database to make listing and filtering easy.
In [1]: animal_init = {'age': 0}
In [2]: cats_and_dogs = {'dog': ['fido', 'rover', 'spud'],
...: 'cat': ['puss', 'ginger', 'oscar']} # using <type>/<name> as uids
In [3]: from yob_database import YobDatabase
In [4]: !mkdir /tmp/test_yobs
In [5]: db = YobDatabase('/tmp/test_yobs')
In [6]: for animal_type, animal_names in cats_and_dogs.items():
...: for name in animal_names:
...: uid = f"{animal_type}/{name}"
...: data = {**animal_init, 'type': animal_type, 'name': name}
...: db.create(uid, data)
...:
In [7]: with db.get('dog/fido') as fido:
...: fido['age'] = 10
...:
In [8]: db.list()
Out[8]:
[{'age': 0, 'name': 'oscar', 'type': 'cat'},
{'age': 0, 'name': 'puss', 'type': 'cat'},
{'age': 0, 'name': 'ginger', 'type': 'cat'},
{'age': 0, 'name': 'rover', 'type': 'dog'},
{'age': 0, 'name': 'spud', 'type': 'dog'},
{'age': 10, 'name': 'fido', 'type': 'dog'}]
In [9]: db.filter(lambda y: y['name'].endswith('r'))
Out[9]:
[{'age': 0, 'name': 'oscar', 'type': 'cat'},
{'age': 0, 'name': 'ginger', 'type': 'cat'},
{'age': 0, 'name': 'rover', 'type': 'dog'}]
In [10]: db.filter(lambda y: y['name'].endswith('r'), 'cat')
Out[10]:
[{'age': 0, 'name': 'oscar', 'type': 'cat'},
{'age': 0, 'name': 'ginger', 'type': 'cat'}]
In [11]: db.filter(lambda y: y['age'] > 5)
Out[11]: [{'age': 10, 'name': 'fido', 'type': 'dog'}]
The file structure is simple and easy to work with directly too:
(yob) test_yobs $ tree
.
├── cat
│ ├── ginger
│ ├── oscar
│ └── puss
└── dog
├── fido
├── rover
└── spud
2 directories, 6 files
(yob) test_yobs $ cat cat/ginger
age: 0
name: ginger
type: cat
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yob-1.0.0.tar.gz.
File metadata
- Download URL: yob-1.0.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eed87d6663fc9992022cd2ccf456cb092e5b387838ef0ea90d2a4f9e647f12e
|
|
| MD5 |
81e4137afa2543b551f5f843d289792d
|
|
| BLAKE2b-256 |
1ac062a075507088e166b6449c99c8410fa5518c7c2c44ff906d469e211a84bd
|
File details
Details for the file yob-1.0.0-py3-none-any.whl.
File metadata
- Download URL: yob-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
568c0f21f0658f53be1f9b8ec76bf97c361522af21a506eda931ca349c571bfd
|
|
| MD5 |
3c217b7a326bb2b0d4407691a293091d
|
|
| BLAKE2b-256 |
80c042bf96883993a9077e7468a3ce6ca279437b8c4a1051398230c0766584c8
|