Skip to content

Commit fe61f6d

Browse files
intial version created
1 parent 9d74f30 commit fe61f6d

14 files changed

+292
-39
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
**Are you using latest code**
10+
11+
**Describe the bug**
12+
A clear and concise description of what the bug is.
13+
14+
15+
**Expected behavior**
16+
A clear and concise description of what you expected to happen.
17+
18+
**Screenshots**
19+
If applicable, add screenshots to help explain your problem.
20+
21+
**Pythonversion**
22+
- Python version
23+
24+
**Additional context**
25+
Add any other context about the problem here.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: enhancement
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
name: Update Documentation
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: documentation, enhancement
6+
assignees: ''
7+
8+
---
9+
10+
**Short introduction what you want to change in README.md **
11+
example:- code change, outdated Readme

.github/PULL_REQUEST_TEMPLATE.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# Pull Request Template
3+
4+
**script name** -
5+
6+
**What have you Changed**
7+
8+
what you changed in the codebase.write here
9+
10+
11+
### Issue no.(must) - #
12+
13+
### Self Check(Tick After Making pull Request)
14+
15+
- [ ] This issue was assigned to me.
16+
- [x] One Change in one Pull Request
17+
- [x] My file is in proper folder
18+
- [x] I am following clean code and Documentation and my code is well linted with flake8.
19+
- [ ] I have added README.md and requirements.txt with my script
20+
21+
If issue was not assigned to you Please don't make a PR. It will marked as invalid.
22+
23+
Join Us on Discord:- https://discord.gg/JfbK3bS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# GitHub Action that uses close-issue auto-close empty issues after they are opened.
2+
# If the issue body text is empty the Action auto-closes it and sends a notification.
3+
# Otherwise if the issue body is not empty, it does nothing and the issue remains open.
4+
# https://github.com/marketplace/actions/close-issue
5+
6+
name: auto_close_empty_issues
7+
on:
8+
issues:
9+
types: [opened]
10+
jobs:
11+
check-issue-body-not-empty:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- if: github.event.issue.body == 0
15+
name: Close Issue
16+
uses: peter-evans/close-issue@v1
17+
with:
18+
comment: |
19+
Issue body must contain content.
20+
Auto-closing this issue.

.github/workflows/codeql-analysis.yml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: "CodeQL"
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
schedule:
8+
- cron: '0 9 * * 5'
9+
10+
jobs:
11+
analyze:
12+
name: Analyze
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
# Override automatic language detection by changing the below list
19+
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
20+
language: ['python']
21+
# Learn more...
22+
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection
23+
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v2
27+
28+
# Initializes the CodeQL tools for scanning.
29+
- name: Initialize CodeQL
30+
uses: github/codeql-action/init@v1
31+
with:
32+
languages: ${{ matrix.language }}
33+
# If you wish to specify custom queries, you can do so here or in a config file.
34+
# By default, queries listed here will override any specified in a config file.
35+
# Prefix the list here with "+" to use these queries and those in the config file.
36+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
37+
38+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
39+
# If this step fails, then you should remove it and run the build manually (see below)
40+
- name: Autobuild
41+
uses: github/codeql-action/autobuild@v1
42+
43+
# ℹ️ Command-line programs to run using the OS shell.
44+
# 📚 https://git.io/JvXDl
45+
46+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
47+
# and modify them (or add more) to build your code if your project
48+
# uses a compiled language
49+
50+
#- run: |
51+
# make bootstrap
52+
# make release
53+
54+
- name: Perform CodeQL Analysis
55+
uses: github/codeql-action/analyze@v1

.github/workflows/publish.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish package to PyPI
2+
on: push
3+
jobs:
4+
build-and-publish:
5+
name: Build and Publish to PyPI
6+
runs-on: ubuntu-latest
7+
if: startsWith(github.ref, 'refs/tags')
8+
steps:
9+
- uses: actions/checkout@v2
10+
- name: Use Python 3.8
11+
uses: actions/setup-python@v2
12+
with:
13+
python-version: 3.8
14+
- name: Run setup
15+
run: >-
16+
python setup.py sdist
17+
- name: Publish distribution to PyPI
18+
uses: pypa/gh-action-pypi-publish@master
19+
with:
20+
user: __token__
21+
password: ${{ secrets.pypi_password }}

.github/workflows/run-pytest.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Run Pytest
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.10"]
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Install dependencies
20+
run: |
21+
sudo apt-get update
22+
sudo apt install libespeak-dev
23+
python -m pip install --upgrade pip
24+
pip install flake8 pytest
25+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
26+
- name: Lint with flake8
27+
run: |
28+
# stop the build if there are Python syntax errors or undefined names
29+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
30+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
31+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=200 --statistics
32+
- name: Test with pytest
33+
run: |
34+
pytest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
*.json

db.json

-1
This file was deleted.

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
colorama
1+
colorama
2+
pytest
3+
flake8

src/main.py

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from ast import For
21
import os
32
import json
3+
import re
44

5-
from colorama import Fore
6-
from colorama import Style
5+
from colorama import Fore, Style
76

87
def print_it(color, text):
98
if color == "red":
@@ -15,75 +14,76 @@ def print_it(color, text):
1514

1615
class Database(object):
1716
""" python database.py """
18-
def __init__(self, db_path):
17+
def __init__(self, db_path, overwrite_db=False):
1918
self.db_path = db_path
20-
self.db = self.load_db()
19+
self.db = self.__init_db(overwrite_db)
2120

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:
21+
def __load_db_from_file(self):
22+
with open(self.db_path, 'r') as f:
23+
return json.load(f)
24+
25+
def __init_db(self, overwrite_db):
26+
if not overwrite_db and os.path.exists(self.db_path):
27+
print_it("green", "Loading database from file")
28+
return self.__load_db_from_file()
29+
30+
elif overwrite_db and os.path.exists(self.db_path):
31+
print_it("blue", "Overwriting database")
32+
self.clear()
33+
return self.db
34+
35+
elif not os.path.exists(self.db_path):
36+
print_it("green", "Creating new database")
2737
return {}
2838

29-
def save_db(self, data):
30-
with open(self.db_path, 'w+') as f:
31-
try:
39+
def __dump_db(self, data):
40+
try:
41+
with open(self.db_path, 'w+') as f:
3242
json.dump(data, f, indent=4)
3343
return True
34-
except Exception as e:
35-
return False
44+
except Exception as e:
45+
return False
3646

3747
def set(self, key, value, overwrite=False):
38-
3948
if key not in self.db:
4049
print_it("green", "Key is not present in the database, adding in the database")
50+
4151
self.db[key] = value
42-
self.save_db(self.db)
52+
self.__dump_db(self.db)
53+
return True
4354

4455
elif not overwrite and key in self.db:
4556
print_it("red", "Key already exists")
57+
return False
4658

4759
elif overwrite and key in self.db:
4860
print_it("blue", "Key already exists, overwriting the value")
4961
self.db[key] = value
50-
self.save_db(self.db)
62+
self.__dump_db(self.db)
63+
return True
5164

5265
def get(self, key):
5366
if key in self.db:
5467
return self.db[key]
5568
else:
5669
print_it("red", "Key not found")
5770
return False
58-
59-
def delete_db(self):
60-
os.remove(self.db_path)
61-
print_it("green", "Database deleted")
62-
6371

6472
def delete(self, key):
6573
if key in self.db:
6674
del self.db[key]
67-
self.save_db(self.db)
75+
self.__dump_db(self.db)
6876
print_it("green", "Key deleted")
6977
else:
7078
print_it("red", "Key not found")
71-
72-
73-
def list(self):
79+
80+
def get_db(self):
81+
print(self.db)
7482
return self.db
7583

7684
def clear(self):
7785
self.db = {}
78-
self.save_db(self.db)
86+
self.__dump_db(self.db)
7987
print_it("green", "Database cleared")
80-
81-
88+
return True
8289

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()

src/tests/test_cases.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
sys.path.append('.')
3+
import unittest
4+
5+
from src.main import Database
6+
7+
db = Database("test_db.json", overwrite_db=True)
8+
9+
class DatabaseTest(unittest.TestCase):
10+
11+
def test_operation_one(self):
12+
""" if key does not exist, it should be added """
13+
self.assertTrue(db.set("name", "john"))
14+
self.assertTrue(db.set("age", 25))
15+
self.assertTrue(db.set("age", 30, overwrite=True))
16+
self.assertEqual(db.get_db(), {'name': 'john', 'age': 30})
17+
self.assertTrue(db.clear(), True)
18+
19+
def test_operation_two(self):
20+
""" if key already exists, it should not be added """
21+
self.assertTrue(db.set("name", "john"))
22+
self.assertFalse(db.set("name", "john"))
23+
self.assertTrue(db.clear(), True)
24+
25+
def test_operation_three(self):
26+
""" if key already exists, it should be overwritten """
27+
self.assertTrue(db.set("name", "john"))
28+
self.assertTrue(db.set("name", "john", overwrite=True))
29+
self.assertTrue(db.clear(), True)
30+
31+
32+
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)