Skip to main content

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:

  • stupiddb2.StupidDB

    • insert
    stupiddb2.StupidDB().insert(
        {"name": "alice", "age": "21"}
    )
    
    • retrieve
    result = stupiddb2.StupidDB().retrieve(
        0  # index of the record to retrieve
    )
    print(result)  # {"name": "alice", "age": "21"}
    
    • update
    stupiddb2.StupidDB().update(
        0,  # index of the record to update
        {"name": "alice", "age": "22"}
    )
    
    • remove
    stupiddb2.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.

  • stupiddb2.StupidLock

    • is_locked
    stupiddb2.StupidLock().is_locked()  # False
    
    • acquire_lock
    stupiddb2.StupidLock().acquire_lock()
    stupiddb2.StupidLock().is_locked()  # True
    
    • release_lock
    stupiddb2.StupidLock().release_lock()
    stupiddb2.StupidLock().is_locked()  # False
    

Installing

pip install stupiddb2  # pip
poetry add stupiddb2  # poetry

Example Usage

from stupiddb2 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

Development

Command Reference

just format  # runs black + isort
just lint  # runs mypy + ruff
just test  # runs tests
just coverage  # prints coverage report (run after tests)

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

Q: Why is this called stupiddb2 and not stupiddb?

stupiddb was taken on pypi 😭

If any questions are ever asked, they will be answered here. 🙋

License

GPL-3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stupiddb2-1.0.2.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stupiddb2-1.0.2-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file stupiddb2-1.0.2.tar.gz.

File metadata

  • Download URL: stupiddb2-1.0.2.tar.gz
  • Upload date:
  • Size: 15.8 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

Hashes for stupiddb2-1.0.2.tar.gz
Algorithm Hash digest
SHA256 4d73dacd68ce4f85d1e2b5a14f6d6a4a25c894eb3bc10f4bfeb2341f96c60191
MD5 e9d69a61f6dbead86b7dae6af03fab19
BLAKE2b-256 d090d9fb1e1138dc0731ea67b843ea9baf9d7d73945e4f154d8c065bbbe0388e

See more details on using hashes here.

File details

Details for the file stupiddb2-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: stupiddb2-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 28.7 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

Hashes for stupiddb2-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 689144c02bc69d22460b6f3c1f38562cfbe08d98c9d545506dc261c2894eb2af
MD5 b972d7ad82d94f909522f8a7fc88192c
BLAKE2b-256 981f2049bb8106076c28479da70311a061655beebe153fdf997fc799b5de22e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page