Skip to main content

License: LGPL v3 Generic badge

LunaDB

LunaDB is a document oriented file based database written in Python. It's designed on similar scenarios as TinyDB, but with a better scaling with more documents and (hopefully) less memory usage.

Introduction

LunaDB reads only one document at a time and doesn't save the whole database in one giant JSON. Instead it saves every document as an own JSON and seperates them by line breaks.
Main features from LunaDB:

  • Document based: Like TinyDB, you can store documents as dict
  • Written in pure Python: LunaDB doesn't require any other dependency
  • Even more tiny: LunaDB has less than 500 lines of code, without unittests
    Important: LunaDB stores the data in multiple files located in one folder!

Supported Python Versions

LunaDB supports only Python 3, there is no Python 2 support and there won't be any. As it is using os.replace() it needs at least Python 3.3 to run. Newer versions should be no problem. I use this module on Windows 10 and Debian Strech, it should run on any OS, if not feel free to open an issue.

Example Usage

from LunaDB import LunaDB
db = LunaDB("relative/path/to/database")

# If you are not giving an specific field, which will be unique, an automatic id_field will be used
character = db.table("character")
city = db.table("city", id_field="name")

# If strict is True it will throw an error if there is duplicated entry. Otherwise it will just skip the entry.
character.insert({"name": "Tristan", "age": 21}, strict = True)
character.insert({"name": "Isolt", "age": 19}, strict = False)

# To insert multiple entries at once, use insert_multiple
city.insert_multiple([{"name":"London"}, {"name": "Manchester"}])

Searching for entries

Searching works with filter functions. The search method takes a function which returns True when the entry should be in the response. This filter function can be defined seperatly or with the lambda statement.

res = character.search(lambda entry: entry["age"] == 21)
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
res = character.search(lambda entry: entry["age"] < 22)
# res = [{"name": "Tristan", "age": 21, "_id": 0}, {"Isolt", "age":19, "_id":1}]
# The "_id" field is the auto_increment identifier
res = city.search(lambda entry: entry["name"] == "London")
# res = [{"name": "London"}]

Update

The update uses the id field to identify the document and will replace it.

res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
res["age"] = 22
character.update(res, auto_clean = True)
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 22, "_id": 0}]

The auto_clean parameter is present in every method that modifies or deletes existing data. Without auto_clean LunaDB does not delete the data from the database, in fact it is just disabling them. Only if the database is cleaned afterwards, the disabled entries will get deleted. If auto_clean is False you need to clean the table with the table.clean() method manually if you want to save space. If auto_clean is True, the table will automatically cleared when the disabled lines exceed the auto_clean_buffer, which is per default 5MB per table. auto_clean_buffer can be set with the creation of the database object. Per default auto_clean is True.

Delete

The delete works the same way as search, every row which returns at the filter function True will be deleted. The auto_clean is the same as with update.

res = character.search(lambda entry: entry["name"] == "Tristan")
# res = [{"name":"Tristan", "age": 21, "_id": 0}]
character.delete(lambda entry: entry["name"] == "Tristan", auto_clean = True)
res = character.search(lambda entry: entry["name"] == "Tristan")
# res = []

Additional concepts

Insert or update

row = {"name":"Tristan", "age": 17}
character.upsert(row)

Insert or skip

row = {"name":"Tristan", "age": 17}
# With strict = False, duplicated entries won't throw an exception
character.insert(row, strict = False)

Additional documentation

Look at the module reference.

Metadata

Release files for LunaDB 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for LunaDB 0.5.2
File Size Uploaded Provenance Actions
LunaDB-0.5.2.tar.gz 5.0 kB Without provenance Details for LunaDB-0.5.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for LunaDB 0.5.2
File Interpreter ABI Platform Actions
LunaDB-0.5.2-py3-none-any.whl Python 3 none any Details for LunaDB-0.5.2-py3-none-any.whl

Total release size: 13.8 kB

Release files / LunaDB-0.5.2.tar.gz

Download URL LunaDB-0.5.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8b80ebddd6cf0c5d546545ba6d7fe8ba07918d926ffb16b34cab10d30a85a3c5
BLAKE2b-256 checksum
How to use checksums
e0f8e8e1096e5add662a7ce4914957bdfe31b8de7894f5fe897f78f63ca2a757
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

Release files / LunaDB-0.5.2-py3-none-any.whl

Download URL LunaDB-0.5.2-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c54d188e14211b6639ff463bc2545025d09f2f17a8783bf85fdc428c266f8a62
BLAKE2b-256 checksum
How to use checksums
0390b4d24784b77951338394a85ad336cf2f6c94185b6a066f3cdbefc76a54ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

Security for LunaDB 0.5.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page