Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/umihico/microdb
In-memory, Hash-mapping, Disk-writable, Thread-safe database.
https://github.com/umihico/microdb
database python
Last synced: about 2 months ago
JSON representation
In-memory, Hash-mapping, Disk-writable, Thread-safe database.
- Host: GitHub
- URL: https://github.com/umihico/microdb
- Owner: umihico
- License: mit
- Created: 2018-12-22T11:19:12.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-24T06:54:25.000Z (about 6 years ago)
- Last Synced: 2024-11-04T23:51:33.952Z (about 2 months ago)
- Topics: database, python
- Language: Python
- Size: 19.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# microdb
In-memory, Hash-mapping, Disk-writable, Thread-safe database.
[![PyPI version](https://badge.fury.io/py/microdb.svg)](https://badge.fury.io/py/microdb)Installation with pip
```
$ pip install microdb
```This database works as list of dictionaries.
```
>>> import microdb
>>> mdb = microdb.MicroDB("testdata/mdb.json", partition_keys=['job', 'name'])
>>> mdb.pprint_all()
[{'job': 'clean', 'name': 'Alice', 'status': 'undone'},
{'job': 'study', 'name': 'Alice', 'status': 'undone'},
{'job': 'clean', 'name': 'Bob', 'status': 'undone'},
{'job': 'study', 'name': 'Bob', 'status': 'undone'}]
>>> mdb.pprint_all_as_grid()
[['name', 'status', 'job'],
['Alice', 'undone', 'clean'],
['Alice', 'undone', 'study'],
['Bob', 'undone', 'clean'],
['Bob', 'undone', 'study']]
```
Only 'upsert' method add data, or overwrite if the "key" is already in use.
```
>>> mdb.upsert({'job': 'clean', 'name': 'Alice', 'status': 'finished'})
>>> mdb.upsert({'job': 'clean', 'name': 'Eve', 'status': 'undone'})
>>> mdb.pprint_all()
[{'job': 'clean', 'name': 'Alice', 'status': 'finished'}, # overwritten
{'job': 'study', 'name': 'Alice', 'status': 'undone'},
{'job': 'clean', 'name': 'Bob', 'status': 'undone'},
{'job': 'study', 'name': 'Bob', 'status': 'undone'},
{'job': 'clean', 'name': 'Eve', 'status': 'undone'}] # new
```
Key is tupled values of partition_keys. Use 'gen_key' method to see.
```
>>> # mdb = microdb.MicroDB("testdata/mdb.json", partition_keys=['job', 'name'])
>>> mdb.gen_key({'job': 'clean', 'name': 'Bob', 'what': 'ever'})
('clean', 'Bob')
```
'get' method and 'in' operator as same as dict
```
>>> {'job': 'clean', 'name': 'Bob', 'what': 'ever'} in mdb
True
>>> mdb.get({'job': 'study', 'name': 'Alice','whaat':'everr'})
{'status': 'undone', 'job': 'study', 'name': 'Alice'}
>>> mdb.get({'wrong': 'key'},'when_not_exist') # same as dict.get
'when_not_exist'
```
Write on disk when you want.
```
>>> mdb.save()>>> mdb.save_as_grid() # good to reduce amount when all keys are common
$ cat testdata/mdb.json
[['status', 'job', 'name'], ['undone', 'clean', 'Alice'], ['undone', 'study', 'Alice'], ['undone', 'clean', 'Bob'], ['undone', 'study', 'Bob']]>>> mdb.save('another_file_name.json') # you can write anywhere
$ cat another_file_name.json
[{'job': 'clean', 'name': 'Alice', 'status': 'undone'}, {'job': 'study', 'name': 'Alice', 'status': 'undone'}, {'job': 'clean', 'name': 'Bob', 'status': 'undone'}, {'job': 'study', 'name': 'Bob', 'status': 'undone'}]
```All operation is already thread-safe by threading.rlock().
In case if you need to block while operations in a row,
```
>>> with mdb.lock():
... bob_clean_status=mdb.get({'job': 'clean', 'name': 'Bob'})['status']
... if bob_clean_status != 'finished':
... mdb.upsert({'job': 'pay_penalty', 'name': 'Bob','status':'undone'})
```