Lightweight json-database library for Python
Project description
Sunshine
Lightweight json-database library for Python
Installing database library
pip install SunshineDB
Class
Sunshine - database management class.
Has the following set of methods:
0. push()
1. all()
2. get()
3. update()
4. contains()
5. delete()
6. drop()
7. backup()
Creating database
You just need to create an instance of the Sunshine-class, passing the path to the json-file as an argument.
For example,
from SunshineDB import Sunshine
database: Sunshine = Sunshine('../databases/database.json')
Methods
Method examples will be given using the database variable we set.
push()
Adds an object with the given fields to the database.
Requires one argument:
- data_to_push (dictionary[string, any]) - the key-value dictionary to be added to the database.
Returns ID.
identifier: int = database.push(
{
'name' : 'Bertram Gilfoyle',
'job' : 'Pied Piper Inc.',
'occupation' : 'Vice President Of Architecture'
}
)
print(identifier)
# output >> 22104564398807
# ^^^^^^^^^^^^^^
# ID is 14-digit integer
all()
Returns all database objects.
data: list[dict[str, any]] = database.all()
print(data)
# output >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Vice President Of Architecture'
# }
# ]
get()
Returns a database object using a query, or returns the number of given elements in the first one up to the number specified in the count argument.
Requires two arguments:
- query (dictionary[string, any]) - a key-value dictionary that will be used to select elements,
- count (integer) - the number of requested elements.
You cannot use both arguments together.
data: list[dict[str, any]] = database.get(
query = {
'job' : 'Pied Piper Inc.'
}
)
print(data)
# output >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Vice President Of Architecture'
# }
# ]
# And the same will be displayed if you call the get-method like this
data: list[dict[str, any]] = database.get(count = 1)
update()
Updates a database object with an ID.
Requires two arguments:
- id (14-digit integer) - numeric identifier,
- data_to_update (dictionary[string, any]) - the key-value dictionary that will be updated in the database object.
database.update(
22104564398807,
{
'occupation' : 'Network engineer'
}
)
# changed to >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Network engineer'
# }
# ]
contains()
Checks by query, if an element is contained in the database.
Requires two arguments:
- key (string),
- value (any).
These arguments will be searched in the database.
Returns boolean.
data: bool = database.contains('name', 'Bertram Gilfoyle')
print(data)
# output >> True
# ^^^^
# contains-method returns boolean
data: bool = database.contains('name', 'Dinesh Chugtai')
print(data)
# output >> False
delete()
Removes the object with the given ID from the database.
Requires one argument:
- id (14-digit integer) - numeric identifier,
database.delete(22104564398807)
# database-file >>
# {
# "data": []
# }
drop()
Removes all objects from the database.
database.drop()
# database-file >>
# {
# "data": []
# }
backup()
Creates a database backup at the given path.
Requires one argument:
- path (string) - path to the folder where the backup-file will be saved.
database.backup('../databases/backups/')
Author
_ _ _ _ _
__| | ___| || | ___ _ _| | |_
/ _` |/ _ \ || |_ / _ \| | | | | __|
| (_| | __/__ _| (_) | |_| | | |_
\__,_|\___| |_| \___/ \__,_|_|\__|
Thank you a lot!
How to reach me
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
Built Distribution
File details
Details for the file SunshineDB-1.5.tar.gz
.
File metadata
- Download URL: SunshineDB-1.5.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9e7369844f7bd49f136d6358caecb7a53f0a6116b44baa8cbe882f327f8e8c7 |
|
MD5 | 2c96dbadb08db256c3f4f9130658afee |
|
BLAKE2b-256 | ed1f6db4541760f99a691070ae09bac65eb62d5e426ce640e9939171b90872e0 |
File details
Details for the file SunshineDB-1.5-py3-none-any.whl
.
File metadata
- Download URL: SunshineDB-1.5-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57184584882ca0fbc6a4aec98c05ef8fce04d901769a57954dbd7462d83f9b11 |
|
MD5 | 067de59ef2b5152c412048b1b65b9a69 |
|
BLAKE2b-256 | 985acfa9cdbfc879d74367cc3e0cd3d0bcf0289f30587fc1363b72ed5a121382 |