A tiny flat file (JSON/YAML) dictionnaries database.
Project description
**TinyDictDb**
==============
What is it ?
------------
TinyDictDb is a very small flat file (JSON or YAML) database meant to store dictionaries.
**Exemple:**
.. code:: json
[
{"id":4242,"name":"foo","tags":["aa","bb"]},
{"id":4243,"name":"bar","tags":["bb","cc"]},
{"id":4244,"name":"fobar","tags":["dd"]}
]
.. code:: yaml
- id: 4242
name: foo
tags: [aa, bb]
- id: 4243
name: bar
tags: [bb, cc]
- id: 4244
name: fobar
tags: [dd]]
Installation
------------
pip install tinydictdb
Note: If you want to use yaml file, you have to install PyYaml by yourself.
Usage
-----
import tinydictdb as tddb
Create or open a database (a JSON/YAML file):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
db = tddb.TinyDictDb(“path”)
db = tddb.TinyDictDb(“path”, 'yaml')
Add entry/entries :
~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
b = [{“id”:4242,“name”:“foo”},{“id”:4243,“name”:“bar”}]
db.addEntries(a)
db.addEntries(b)
Find entry / entries:
~~~~~~~~~~~~~~~~~~~~~
Four kinds of research are implemented :
db.findEntries() # Will return the full db
db.findEntries(name=“foo”) # Will return all entries with “name” containing “foo”.
db.findEntries(tags=[“aa”]) # Will return all entries with tag “aa”.
db.findEntries(key=function) # Will return all entries for which function(entry[“key”]) return true.
It’s even possible to use it inline with a lambda.
db.findEntries(id=(lambda x: True if x < 4243 else False))
Delete entry / entries:
~~~~~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
b = [{“id”:4242,“name”:“foo”},{“id”:4243,“name”:“bar”}]
db.deleteEntries(a)
db.deleteEntries(b)
db.deleteEntries(db.findEntries(name=“foo”))
Verify if an entry / count the number of its occurences:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
db.isPresent(a) #Will return the number of occurence of a in the db.
Edit entries:
~~~~~~~~~~~~~
editEntries will take a subset of entries and a function to apply to
them.
.. code :: python
def fct(in):
in["id"] += 1
return in
db.editEntries(db.findEntries(), fct) # will increment the id’s of all the db.
Get informations about the db:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print(db)
TinyDictDb instance stored in /tmp/plop, containing 4 entries in yaml format.
==============
What is it ?
------------
TinyDictDb is a very small flat file (JSON or YAML) database meant to store dictionaries.
**Exemple:**
.. code:: json
[
{"id":4242,"name":"foo","tags":["aa","bb"]},
{"id":4243,"name":"bar","tags":["bb","cc"]},
{"id":4244,"name":"fobar","tags":["dd"]}
]
.. code:: yaml
- id: 4242
name: foo
tags: [aa, bb]
- id: 4243
name: bar
tags: [bb, cc]
- id: 4244
name: fobar
tags: [dd]]
Installation
------------
pip install tinydictdb
Note: If you want to use yaml file, you have to install PyYaml by yourself.
Usage
-----
import tinydictdb as tddb
Create or open a database (a JSON/YAML file):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
db = tddb.TinyDictDb(“path”)
db = tddb.TinyDictDb(“path”, 'yaml')
Add entry/entries :
~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
b = [{“id”:4242,“name”:“foo”},{“id”:4243,“name”:“bar”}]
db.addEntries(a)
db.addEntries(b)
Find entry / entries:
~~~~~~~~~~~~~~~~~~~~~
Four kinds of research are implemented :
db.findEntries() # Will return the full db
db.findEntries(name=“foo”) # Will return all entries with “name” containing “foo”.
db.findEntries(tags=[“aa”]) # Will return all entries with tag “aa”.
db.findEntries(key=function) # Will return all entries for which function(entry[“key”]) return true.
It’s even possible to use it inline with a lambda.
db.findEntries(id=(lambda x: True if x < 4243 else False))
Delete entry / entries:
~~~~~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
b = [{“id”:4242,“name”:“foo”},{“id”:4243,“name”:“bar”}]
db.deleteEntries(a)
db.deleteEntries(b)
db.deleteEntries(db.findEntries(name=“foo”))
Verify if an entry / count the number of its occurences:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a = {“id”:4242,“name”:“foo”}
db.isPresent(a) #Will return the number of occurence of a in the db.
Edit entries:
~~~~~~~~~~~~~
editEntries will take a subset of entries and a function to apply to
them.
.. code :: python
def fct(in):
in["id"] += 1
return in
db.editEntries(db.findEntries(), fct) # will increment the id’s of all the db.
Get informations about the db:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print(db)
TinyDictDb instance stored in /tmp/plop, containing 4 entries in yaml format.
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
tinydictdb-0.0.2.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for tinydictdb-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b5ef3c43f849c1dfec0ff5f3e509cec6b89689c37fdce7d11c1fe0f8bd3a363 |
|
MD5 | 34d9d980e5f6e9d797bc218d838d854b |
|
BLAKE2b-256 | 4f45050976f767d6bc0b542c3d4f3af2c8a23f78175f96c1da2a652beaf37f3e |