Giter VIP home page Giter VIP logo

python_json_database_manager's Introduction

python-json-database-manager

The json file database manager used in many of my projects.

A thread safe json database context manager. The class creates separate locks for each .json file in the same directory as the .py file. This way if one file is edited other files do not have to wait. There are 23 test in test_database_manager.py.

This class can be used with the with statement.

Here is some example code:

from database_manager import Database as db

db.create('test')

with db('test') as users:
    users["quinten"] = {"status": "cool"}
    users["foo"] = "bar"

The context manager will automatically fetch the data that is in the .json file and make sure this code runs in a lock specific to the file in the enter and will in the exit write the modified dictionary back to the file. If any unhanded errors happen in the with no data is written back to the file.

You can initiate the class with a target name. The class checks if this file exists. If it does then the with statement will write the dictionary to that file.

You can also change the target name by modifying self.name. There is an automatic check that will assert if this new name exists.

Static methods

There are also a couple static methods for when you do not want to run a database command in a with. All of these static methods will all acquire the lock for the file automatically.

  • info() -> Gives some info about the Database like paths and current locks.
  • create_backup(filenames: list[str]) -> Makes a backup of all .json files in the backup folder. The default backup location is json_db_backups. Give "all_of_them" as input to back up all files. This is the default.
  • read(name) -> Will return the data in name.json
  • write(name,data) -> Will write data to name.json
  • add(name, data key) -> Will add/replace data under key in name.json. A shorthand for read + write.
  • append(name, data) -> Will append data to a list named name in name.json
  • reset_all(default_data) -> Will write default_data to all databases. You can implement your own exceptions here.
  • translate(name, key) -> Will return the value for key in name.json. Usefully for 1 layer dicts.
  • create(name, data) -> Will create a new name.json in the db dir, write data to it and will add a new lock. Do not create json files without this method as the database does only index json files on startup.

There are also non-static versions of these methods that end in s. These don't take a name but instead look at self.name to know which file it should interact with. .reads and .writes don't use a lock.

Finally, a __contains__ is implemented . So you can use the in keyword with an instance. A lock is used when not in a context manager and no lock is used if you are in a context manager.

python_json_database_manager's People

Contributors

tintin10q avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

python_json_database_manager's Issues

In operator broken with database in with block

__contains__ causes deathlock when used in a with block. Could be set maybe by setting a flag if it is locked and then not requesting it again in __contains __ if it is locked or we just don't overwrite in that might also work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.