DEV Community

Cover image for Lightweight key value storage for Python
riyu
riyu

Posted on • Edited on

Lightweight key value storage for Python

Working with massive JSON data in Python is indeed frustrating. Let say you have around thousand of data stored on a JSON file and you wanted to search on a specific data. I know, thinking about it is kinda... Normally, you'll just end up doing a for loop.

for i in data.items():
    if i["name"] == "John Doe":
        print("Finally, found you!")
Enter fullscreen mode Exit fullscreen mode

But there's an easy way in doing that. Thanks to LemonDB. Just initialize a lemon database object and do built in operation.

from lemondb import LemonDB
db = LemonDB('test.json')

# You can use dict queries
db.find_one({'name': 'John Doe'})

# Or just built-in search queries
from lemondb import Query; query = Query()
db.find_one(query.name == 'John Doe')

# Lambda function is also supported
db.find_one(lambda x: x['name'] == 'John Doe')
Enter fullscreen mode Exit fullscreen mode

But that's it- No, there's more. In the previous release, LemonDB now support different type objects such as datetime, and more that the normal JSON could'nt properly serialize. Don't believe me? heres an example:

from lemondb import LemonDB
from datetime import datetime

db = LemonDB('test.json')
db.insert({'name': 'John Doe', 'created-at': datetime.now()})

# Filtering the item
print(db.find_one({'name': 'John Doe'})['created-at'])
# ...
Enter fullscreen mode Exit fullscreen mode

Frankly speaking, LemonDB is a document-oriented database, -- similar to mongodb or any key-value based database. In fact, lemondb was inspired by mongodb but more lightweight with around <= 1000 lines of code in total. -- a type of NoSQL database in which data is stored in binary document files. This type of database associates each document with a unique key that takes the form of a string, path or URI. Keys are used to locate and pull individual documents from the database.

More information can be found @ Github

Top comments (0)