Skip to content

Commit

Permalink
chore: formatted by flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Jun 13, 2024
1 parent 37570de commit 58a1c23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ All notable changes to this project will be documented in this file. The format
- Support for drop database added
- Support for `select` , `insert`, `update` and `delete` queries added

## [Initial Release]
## [Initial Release] Version 0.0.1

- Initial release with basic database features++
- Initial documentation++
Expand Down
2 changes: 1 addition & 1 deletion quickdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from quickdb.main import QuickDB

__all__ = ["QuickDB"]
__all__ = ["QuickDB"]
50 changes: 25 additions & 25 deletions quickdb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def __init__(self, log_path):
self.log_path = log_path
self.logger = logging.getLogger(__name__)

logging.basicConfig(filename=self.log_path,
logging.basicConfig(filename=self.log_path,
level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(lineno)d:%(message)s',
format='%(asctime)s:%(levelname)s:%(lineno)d:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filemode='a')
self.logger.info(f"Logger initialized with log path {self.log_path}")


class QuickDB(Logger):
""" python database.py """
def __init__(self, db_path="db.json", db_drop_path="quick_db_output",
def __init__(self, db_path="db.json", db_drop_path="quick_db_output",
overwrite_db=False, debug=True, db_log_path="dedug.log"):
super().__init__(db_log_path)
""" Initialize the database """
Expand All @@ -33,7 +33,7 @@ def __init__(self, db_path="db.json", db_drop_path="quick_db_output",
self.database = self.db['database']
self.db_drop_path = db_drop_path
self.db_log_path = db_log_path

def __load_db_from_file(self):
""" Load the database from the file """
with open(self.db_path, 'r') as f:
Expand All @@ -45,7 +45,7 @@ def __init_db(self):
if not self.overwrite_db and os.path.exists(self.db_path):
self.logger.info(f"Loading database from {self.db_path}")
return self.__load_db_from_file()

if self.overwrite_db and os.path.exists(self.db_path):
self.logger.info(f"Overwriting database from {self.db_path}")
self.clear()
Expand All @@ -62,12 +62,12 @@ def dump_db(self):
return True
except json.JSONDecodeError:
return False

def dumb_table(self, table_name, output_format='json'):
""" Dump the table to the file """
if table_name not in self.db['database']:
return False

os.makedirs(self.db_drop_path, exist_ok=True)

table_data = self.db['database'][table_name]
Expand Down Expand Up @@ -95,16 +95,16 @@ def dumb_table(self, table_name, output_format='json'):

if output_format == 'print':
print(table_data)
return True
return True

def create_table(self, table_name, columns_list, primary_key=None):
""" Create the schema of the database """

if primary_key not in columns_list:
raise ValueError(f"Primary key {primary_key} not in columns list")

primary_key_index = columns_list.index(primary_key)

self.schema[table_name] = {
"columns_list": columns_list,
"primary_key": primary_key,
Expand All @@ -119,30 +119,30 @@ def create_table(self, table_name, columns_list, primary_key=None):
def get_table_names(self):
""" Get all the tables in the database"""
return list(self.schema.keys())

def get_table(self, table_name):
""" Get the table schema """
return self.schema[table_name]

def get_table_columns(self, table_name):
""" Get the columns of the table """
return self.schema[table_name]['columns_list']

def get_table_primary_key(self, table_name):
""" Get the primary key of the table """
return self.schema[table_name]['primary_key']

def insert_into(self, table_name, value, overwrite=False, dump_db=False):
""" Set the key-value pair in the database """
primary_value = value[self.schema[table_name]['primary_key_index']]
columns_list = self.schema[table_name]['columns_list']

if primary_value in self.db['database'][table_name] and not overwrite:
return False

if len(value) != len(columns_list):
return False

self.db["database"][table_name][primary_value] = dict(zip(columns_list, value))
self.logger.info(f"Inserted {value} into {table_name}")

Expand All @@ -160,7 +160,7 @@ def clear(self):
self.dump_db()
self.logger.info("Database cleared")
return True

def drop_table(self, table_name):
""" Drop the table from the database """
if table_name in self.db['database']:
Expand All @@ -170,7 +170,7 @@ def drop_table(self, table_name):
self.logger.info(f"Table {table_name} dropped")
return True
return False

def delete(self, table_name, primary_value):
""" Delete the key-value pair in the database """
if primary_value in self.db['database'][table_name]:
Expand All @@ -179,7 +179,7 @@ def delete(self, table_name, primary_value):
self.logger.info(f"Deleted {primary_value} from {table_name}")
return True
return False

def update(self, table_name, primary_value, value):
""" Update the key-value pair in the database """
if primary_value in self.db['database'][table_name]:
Expand All @@ -188,26 +188,26 @@ def update(self, table_name, primary_value, value):
self.logger.info(f"Updated {primary_value} in {table_name}")
return True
return False

def search(self, table_name, primary_value):
""" Search the key-value pair in the database """
if primary_value in self.db['database'][table_name]:
return self.db['database'][table_name][primary_value]
return False

def where(self, table_name, column_name, value):
""" Search the key-value pair in the database """
result = []
for key, val in self.db['database'][table_name].items():
if val[column_name] == value:
result.append(val)
return result

def __repr__(self):
return f"QuickDB(db_path={self.db_path}, db={self.db})"

def __str__(self):
return f"QuickDB(db_path={self.db_path}, db={self.db})"

def __getitem__(self, key):
return self.db["database"][key] if key in self.db["database"] else None
1 change: 0 additions & 1 deletion tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ class DatabaseTest(unittest.TestCase):
pass



if __name__ == '__main__':
unittest.main()

0 comments on commit 58a1c23

Please sign in to comment.