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.1.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for tinydictdb-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 392837dd694641bfcbb408c3ec0b1342806f6735af2ea2c34ddacd2646256575 |
|
MD5 | 2a388e923bfa434ffabcb4a224af040f |
|
BLAKE2b-256 | 22cb0e9e86e4a5798aa651187d2b44f11957a93df8e28436e122ebe68e38ff3b |