Package: simplejsondb
Do you need a simple, small, and very easy to use local database? You have come
to the right place! withsimplejsondbyou can create a simple JSON database with
only one line of code, and we will take care of the rest!A basic exampleInstallationTheDatabaseobjectDatabaseconstructorDatabase.dataDatabase.save(**additional)Database.load()Database.pathDatabase.folderTheDatabaseFolderobjectDatabaseFolderconstructorExamplesA basic examplefromsimplejsondbimportDatabase# If database named 'translations' doesn't exist yet, creates a new empty dict databasetranslations=Database('translations.json',default=dict())# Now, we can treat the database instance as a dictionary!translations.data['Hello']='Hola'translations.data['Goodbye']='Adiֳ³s'print(translations.data.values())# dict_values(['Hola', 'Adiֳ³s'])# The database will automatically save the changes when the program exitsAfter running the code above for the first time, a file namedtranslations.jsonwill be automatically created under the current working directory. Then, we will
be able to use the database inside other scripts, and treat it as a dictionary:fromsimplejsondbimportDatabase# loads the previously saved translations databasetranslations=Database('translations.json')# Again, we treat the 'translations' instance as a dictionaryprint(f"Hello in Spanish is{translations.data['Hello']}!")# This will output: Hello in Spanish is Hola!# We can also use the built in dictionary methodsforenglish,spanishintranslations.data.items():print(f"{english}in Spanish is{spanish}!")# This will output:# Hello in Spanish is Hola!# Goodbye in Spanish is Adiֳ³s!InstallationThe simplest way to installsimplejsondbis by usingpip. Just run the following
command in your terminal:$(sudo)pipinstallsimplejsondbYou can also clone theGithub repo,
download the latest release fromGithubor directly fromPyPI. Then, unzip
the files (if they are zipped), and use the following command to install the package:$(sudo)pythonsetup.pyinstallIf the command above didn't work, make sure that you have thesetuptoolspackage
installed using:$(sudo)pipinstallsetuptoolsTheDatabaseobjectEach instance of theDatabaseobject is represented by a single json file in
the local storage.DatabaseconstructorThe constructor of theDatabaseinstance. Receives a path to aJSONfile, and
tries to load it. If the file doesn't exist, loads the default data that is passed
using thedefaultargument.Argumentspath (str):The path to the json database file.default:The default data that will be loaded if the file doesn't exist yet.Noneby default.save_at_exit (bool):IfTrue(default), will automatically dump the database
into storage when the program exits.Database.dataA property that stores the data in the database. Writing and reading data should
be done directly using this property.Database.save(**additional)By default, the database is loaded from the local storage when the instance is
created. It is then saved in the memory, until the program exits - and only then
the new and updated data is saved back in the local storage. By using theDatabase.save()method, you can save the database into the local storage before
the program exists, in any given point.Additional argumentsThesavemethod supports receiving additional keyword arguments that are directly
passed into thejson.dumpfunction. One useful argument can beindentthat is
a non-negative integer that sets the indention level of the dumped json file. For
more information, check out thejson module documentation.Database.load()Loads the data from thejsonfile into the instance. Overwrites previous data that was
saved under the.dataproperty.Database.pathA property that contains the path to the database JSON file.Database.folderA property that stores the absolute path to the directory where the database json
file lives in.TheDatabaseFolderobjectIn larger projects, you may want to separate your database into multiplejsonfiles under the same directory. for that, it is possible to use theDatabaseFolderobject, that lets you easily manage multiplejsonfiles, without dealing with
different instances of theDatabaseobject.DatabaseFolderconstructorThe constructor receives a path to a directory, and stores the it for later.Argumentsfolder (str):A path to a folder in the storage in which the database folder
will live.default_factory (callable):A function that receives a string that represents
the name of a database file, and returns the default value for the file.
The default value is the following lambda functionlambda _: Nonewhich will
returnNoneno matter what the name of the file is.save_at_exit (bool):IfTrue(default), will automatically dump the database
into storage when the program exits.ExamplesfromsimplejsondbimportDatabaseFolder# Create a database under the 'db' folder# The default value in the files will be a listdb=DatabaseFolder('db',default_factory=lambda_:list())# Append 'hello' in the 'db/file1.json'db['file1'].append('hello')# 'db/file2.json' will store a dictionarydb['file2']=dict()db['file2']['hello']='there!'print(db['file2'])# {'hello': 'there!'}