Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fl1yd committed Sep 9, 2021
0 parents commit 0ba2333
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Fl1yd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
LightDB
=======


What is this?
-------------

LightDB is a lightweight JSON Database for Python
that allows you to **quickly** and **easily** write data to a file


Installing
----------
pip3 install lightdb


How to use
----------

from lightdb import LightDB
db = LightDB("/path/to/file.json") # or a non-existent file, it will be created automatically

# `SET` method:
data = {
"key1": "value1",
"key2": ["value2", "value3", ...],
...
}
db.set("key3", data)
data = ["value4", "value5"]
db.set("key4", data)


# or `GET`:
print(db.get("key3"))
# {"key1": "value1", "key2": ["value2", "value3", ...]}
print(db.get("key4"))
# ["value4", "value5"]
109 changes: 109 additions & 0 deletions lightdb/LightDB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import json


class LightDB(object):
"""
Light Database
~~~~~~~~~~~~~~
`SET` method usage:
>>> from lightdb import LightDB
>>> db = LightDB("/path/to/file.json") # or a non-existent file, it will be created automatically
>>> data = {
"key1": "value1",
"key2": ["value2", "value3", ...],
...
}
>>> db.set("key3", data)
True
>>> data = ["value4", "value5"]
>>> db.set("key4", data)
True
... or `GET`:
>>> db.get("key3")
{"key1": "value1", "key2": ["value2", "value3", ...]}
>>> db.get("key4")
["value4", "value5"]
"""

def __init__(self, location: str):
self.location = location
self.db = self.load()

def load(self):
return (
json.load(open(self.location, "r"))
if os.path.exists(self.location)
else {}
)

def save(self):
json.dump(
self.db, open(self.location, "w+"),
ensure_ascii = False, indent = 4
)
return True

def set(self, key, value):
"""
LightDB `SET` method
Usage:
>>> data = {
"key1": "value1",
"key2": ["value2", "value3"]
}
>>> db.set("key3", data)
True
:params: The keyname and value you want to set
:return: True
"""

self.db[key] = value
return self.save()

def get(self, key, default = None):
"""
LightDB `GET` method
Usage:
>>> db.get("key3")
{"key1": "value1", "key2": ["value2", "value3"]}
>>> r = db.get("key3")
>>> r["key2"].remove("value3")
>>> r
{"key1": "value1", "key2": ["value2"]}
:params:
key - The keyname of the item you want to return the value from
default - A value to return if the specified key does not exist. Default value `None`
:return: The value of the item with the specified key
"""

return self.db.get(key, default)

def pop(self, key):
"""
LightDB `POP` method
Usage:
>>> db.pop("key1")
{"key1": "value1", "key2": ["value2", "value3"]}
:params: The keyname of the item you want to remove
:return: The value of the removed item
"""

db = self.db.pop(key)
self.save()
return db

def reset(self):
self.db = {}
self.save()
return True
13 changes: 13 additions & 0 deletions lightdb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
See LightDB docstring:
from LightDB import LightDB
---------------------
:copyright: (c) 2021 by Fl1yd.
:license: MIT, see LICENSE for more details.
"""

from .LightDB import LightDB

__version__ = "1.0"

0 comments on commit 0ba2333

Please sign in to comment.