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.1.tar.gz (15.7 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.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stupiddb2-1.0.1.tar.gz
  • Upload date:
  • Size: 15.7 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.1.tar.gz
Algorithm Hash digest
SHA256 776404e2ea611c0e7fa1a4af2b8b1f01822402ad593a5addb4374b9752dbd041
MD5 4a6a84722cea9953138dccc35d4a3bfc
BLAKE2b-256 fd39f54cf2697899f3c2a91abab15045f368d9c42ca7728ff69d9626f9b73c58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stupiddb2-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f642bc8dc2dd93071e458ed3c829667dc7367bc9818b702d04c060e25d54964
MD5 9eba8e076ca7e94739675a4de607635c
BLAKE2b-256 02fdbaba646e0ed1f11db5ea1fc76e746c7326ae9a594abbf9ff44f1d5f444cc

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