an incredibly stupid, for fun, json "db"
Project description
This project is designed for recreational purposes, and I would advise against using it for critical database requirements unless it's intended for similar small-scale and experimental projects.
StupidDB
Why?
I often found myself re-implementing the same dumb db/lock for micro projects, so I decided to make it into a library I can just include as a dependency.
This is the dumbest PoC (not proof-of-concept) db I use for toy development. Not really actually a database, more of a python wrapper for a json file. Really great for things like doing a work secret santa api (all within a single file) 🎅 and... not much else.
Includes two amazingly stupid classes and some not well thought out functions for them:
-
stupiddb.StupidDBinsert
stupiddb.StupidDB().insert( {"name": "alice", "age": "21"} )
retrieve
result = stupiddb.StupidDB().retrieve( 0 # index of the record to retrieve ) print(result) # {"name": "alice", "age": "21"}
update
stupiddb.StupidDB().update( 0, # index of the record to update {"name": "alice", "age": "22"} )
remove
stupiddb.StupidDB().remove( 0 # index of the record to remove )
Bonus 🃏: this is a really dumb lock that uses a json file to store the lock state. It's not a real lock, but it's good enough for my purposes.
-
stupiddb.StupidLockis_locked
stupiddb.StupidLock().is_locked() # False
acquire_lock
stupiddb.StupidLock().acquire_lock() stupiddb.StupidLock().is_locked() # True
release_lock
stupiddb.StupidLock().release_lock() stupiddb.StupidLock().is_locked() # False
Command Reference
just format # runs black + isort
just lint # runs mypy + ruff
just test # runs tests
just coverage # prints coverage report (run after tests)
Example Usage
from stupiddb import StupidDB, StupidLock
db_path = "example_db.json"
lock_path = "example_lock.json"
def register_user(user: dict[str, str]) -> bool:
# initialize db
stupid_db = StupidDB(db_path)
# initialize lock
lock = StupidLock(lock_path)
try:
# acquire lock
lock.acquire_lock()
# insert user into db
stupid_db.insert(user)
# release lock
lock.release_lock()
except Exception as e:
# handle exception
# ...something went wrong, the user was not registered
return False
return True
alice = {"name": "alice", "age": "21"}
alice_is_registered = register_user(alice)
print(alice_is_registered) # True
Setup
Install Just
brew install just # if you want to use brew (recommended + easier)
Install Pyenv - (optional but recommended)
pyenv install 3.11.7
pyenv local 3.11.7
Install Poetry
Running the project
# after installing poetry run the following to install dev dependencies
poetry install --with dev --no-root
# if you're using vscode and want the venv path to python for IDE features
poetry run which python # copy the output of this command and paste it into your .vscode/settings.json {"python.defaultInterpreterPath": PATH}
just test # runs tests
FAQ
If any questions are ever asked, they will be answered here. 🙋
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stupiddb2-1.0.0.tar.gz.
File metadata
- Download URL: stupiddb2-1.0.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d478c0ac6eeffa0e50c33908df78bc3f5fcacec3bbabb83c2169243ffacdb41a
|
|
| MD5 |
9d8c6ae188bd4dec8c15d39264f4b6c3
|
|
| BLAKE2b-256 |
c8e8bb69538d76a55e1c3945ffd9afd1eb5a4276160af1f3775fcde18c92232a
|
File details
Details for the file stupiddb2-1.0.0-py3-none-any.whl.
File metadata
- Download URL: stupiddb2-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.7 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f75229907c08589f84c6a943829d80df1cb083318632c2b6a95503a644d494a6
|
|
| MD5 |
784dff8d3a6db908a99a01d06e8f0c46
|
|
| BLAKE2b-256 |
91de111b38fe04036175dda3d4c0a61b8a86deb5ddc51fffc85711e35129488f
|