Skip to main content

Moonlight is a lightweight JSON-database for Python.

Project description

Moonlight

Moonlight Logo

Moonlight is a lightweight JSON-database for Python.

Installing database library

pip install Moonlight

Class

Moonlight - database management class

Has the following set of methods:

0. async push()
1. async all()
2. async get()
3. async update()
5. async delete()
6. async drop()

Creating database

You just need to create an instance of the Moonlight-class, passing the path to the JSON file as an argument.

For example,

from Moonlight import Moonlight

database: Moonlight = Moonlight('../databases/database.json')

Arguments

  • filename - path to database .json-file
  • primary_key - primary key name (default: 'id')
  • show_messages - tuple of messages that will be output during operation (default: ('warning', 'error'))
    • 'success'
    • 'warning'
    • 'error'

Methods

Method examples will be given using the database variable we set.

Quick Methods

push()

Adds an object with the given fields to the database

Arguments:

  • data_to_push (dict[str, any]) - the key-value dictionary to be added to the database

Returns id (int).

identifier: int = await database.push(
    {
        'name'       : 'Bertram Gilfoyle',
        'job'        : 'Pied Piper Inc.',
        'occupation' : 'Vice President Of Architecture'
    }
)

print(identifier) 
# output >> 22104564398807 
#           ^^^^^^^^^^^^^^
# ID is 14-digit integer

all()

Get all objects from the database

Returns all_objects (list[dict[str, any]])

data: list[dict[str, any]] = await database.all()

print(data)
# output >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Vice President Of Architecture'
#   }
# ]

get()

Get object/s from the database by query

Arguments

  • query (dict[str, any]) - the key-value dictionary to find in database

Returns object/s (list[dict[str, any]]).

data: list[dict[str, any]] = await database.get({
    'job' : 'Pied Piper Inc.'
})

print(data)
# output >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Vice President Of Architecture'
#   }
# ]

update()

Update object in the database

Arguments

  • data_to_update (dict[str, any]) - the key-value dictionary to change in object in database (primary_key in data_to_update required!)

Returns id (int).


await database.update(
    {
        'id'         : 22104564398807, 
        'occupation' : 'Network engineer'
    }
)
# changed to >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Network engineer'
#   }
# ]

delete()

Remove object from the database

Arguments

  • id (14-digit int) - identifier of element

Returns object (dict[str, any])

await database.delete(22104564398807)

# database file changed to >> 
# {
#   "data": []
# }
#
# will returned >>
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Network engineer'
#   }

drop()

Removes all database data


await database.drop()

# database-file writes >>
# {
#   "data": []
# }

Author

     _      _  _               _ _   
  __| | ___| || |   ___  _   _| | |_ 
 / _` |/ _ \ || |_ / _ \| | | | | __|
| (_| |  __/__   _| (_) | |_| | | |_ 
 \__,_|\___|  |_|  \___/ \__,_|_|\__|

Thank you a lot!


How to reach me

Telegram Badge Gmail Badge

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

MoonlightDB-1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

MoonlightDB-1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file MoonlightDB-1.1.tar.gz.

File metadata

  • Download URL: MoonlightDB-1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for MoonlightDB-1.1.tar.gz
Algorithm Hash digest
SHA256 cf1f23b4532640f774ffa5ac135402fa4f896bf11f8ecf1cc124a9af7e441079
MD5 64dd0b86e4d3875a2de56f3698611631
BLAKE2b-256 e2e83ba172e662e370b868ad5182aca3f8cc3d665d396aab9b2dd43e549a7fd0

See more details on using hashes here.

File details

Details for the file MoonlightDB-1.1-py3-none-any.whl.

File metadata

  • Download URL: MoonlightDB-1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for MoonlightDB-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e1cf97fd2cc07cc7949554828a8d0713e51f2c3f05b4ba5ceb204f81117484b0
MD5 3c2de8c7baaa4ad1b599e6fac1c98533
BLAKE2b-256 b76709b8e85ac6c5271fb57122f189a35ce4c95efe407a0780718d775a1e1400

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