Skip to content

Commit 9d74f30

Browse files
initial commit
1 parent 4d33d49 commit 9d74f30

File tree

10 files changed

+233
-1
lines changed

10 files changed

+233
-1
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# python-db
2-
open source key-value database
2+
3+
Light weight database for python, with a simple API and a simple file format.
4+
it is not meant to be a full featured database, but rather a simple way to store data in a file.
5+
it's written in python, and is compatible with python 3.6 and above.
6+
7+
8+
## Features
9+
10+
* Simple API
11+
* Colorful output
12+
* Easy to use
13+
* CLI interface
14+
* Open source
15+
16+
## Documentation
17+
18+
The documentation is available at [not yet available]()
19+
20+
## Installation
21+
22+
You can install python-db using pip:
23+
24+
pip install python-db
25+
26+
or you can install it from source:
27+
28+
git clone
29+
cd python-db
30+
python setup.py install
31+
32+
## Usage
33+
34+
To use python-db, you need to import it:
35+
36+
from python_db import Database

db.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/conf.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import datetime
2+
3+
project = 'python-db'
4+
release = '0.0.1'
5+
templates_path = ['_templates']
6+
source_suffix = ".rst"
7+
master_doc = "index"
8+
year = datetime.now().year
9+
copyright = "{} Py-Contributors".format(year)
10+
extensions = ['sphinx.ext.autosectionlabel']
11+
releases_github_path = "Py-Contributors/python-db"
12+
autosectionlabel_prefix_document = True
13+
html_theme = 'sphinx_rtd_theme' # 'pydata_sphinx_theme' 'alabaster'
14+
html_static_path = ['_static']
15+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.venv']
16+
html_sidebars = { '**': ['globaltoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html'] }

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
.. rst-class:: hide-header
3+
4+
Welcome to python-db's documentation!
5+
=====================================
6+
7+
![Download badge](http://pepy.tech/badge/pickledb)

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.https://www.sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

setup.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="python-db",
8+
version="0.0.1",
9+
author="Deepak Raj",
10+
author_email="[email protected]",
11+
description="Simple python database",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/py-contributors/python-db",
15+
keywords="python database",
16+
packages=setuptools.find_packages(),
17+
classifiers=[
18+
"Development Status :: 3 - Alpha",
19+
"Programming Language :: Python :: 3",
20+
"License :: OSI Approved :: MIT License",
21+
"Operating System :: OS Independent",
22+
"Intended Audience :: End Users/Desktop",
23+
"Intended Audience :: Developers",
24+
],
25+
python_requires=">=3.4",
26+
entry_points={
27+
"console_scripts": [],
28+
},
29+
)

src/__init__.py

Whitespace-only changes.

src/main.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from ast import For
2+
import os
3+
import json
4+
5+
from colorama import Fore
6+
from colorama import Style
7+
8+
def print_it(color, text):
9+
if color == "red":
10+
print(Fore.RED + text + Style.RESET_ALL)
11+
elif color == "green":
12+
print(Fore.GREEN + text + Style.RESET_ALL)
13+
elif color == 'blue':
14+
print(Fore.BLUE + text + Style.RESET_ALL)
15+
16+
class Database(object):
17+
""" python database.py """
18+
def __init__(self, db_path):
19+
self.db_path = db_path
20+
self.db = self.load_db()
21+
22+
def load_db(self):
23+
if os.path.exists(self.db_path):
24+
with open(self.db_path, 'r') as f:
25+
return json.load(f)
26+
else:
27+
return {}
28+
29+
def save_db(self, data):
30+
with open(self.db_path, 'w+') as f:
31+
try:
32+
json.dump(data, f, indent=4)
33+
return True
34+
except Exception as e:
35+
return False
36+
37+
def set(self, key, value, overwrite=False):
38+
39+
if key not in self.db:
40+
print_it("green", "Key is not present in the database, adding in the database")
41+
self.db[key] = value
42+
self.save_db(self.db)
43+
44+
elif not overwrite and key in self.db:
45+
print_it("red", "Key already exists")
46+
47+
elif overwrite and key in self.db:
48+
print_it("blue", "Key already exists, overwriting the value")
49+
self.db[key] = value
50+
self.save_db(self.db)
51+
52+
def get(self, key):
53+
if key in self.db:
54+
return self.db[key]
55+
else:
56+
print_it("red", "Key not found")
57+
return False
58+
59+
def delete_db(self):
60+
os.remove(self.db_path)
61+
print_it("green", "Database deleted")
62+
63+
64+
def delete(self, key):
65+
if key in self.db:
66+
del self.db[key]
67+
self.save_db(self.db)
68+
print_it("green", "Key deleted")
69+
else:
70+
print_it("red", "Key not found")
71+
72+
73+
def list(self):
74+
return self.db
75+
76+
def clear(self):
77+
self.db = {}
78+
self.save_db(self.db)
79+
print_it("green", "Database cleared")
80+
81+
82+
83+
if __name__ == '__main__':
84+
db = Database('db.json')
85+
data = db.load_db()
86+
db.set('key', 'value', overwrite=True)
87+
out = db.list()
88+
print(out)
89+
db.clear()

0 commit comments

Comments
 (0)