From d6e104a3be69857e01f09b8f08c3f1f2bce455b0 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Mar 2023 14:46:35 +0530 Subject: [PATCH 01/19] Added json file system --- filexdb/collection.py | 32 ++++++++++------ filexdb/database.py | 15 ++++++-- filexdb/fileio.py | 87 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 111 insertions(+), 23 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index 2a5225b..f0ee2bd 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -14,12 +14,12 @@ class Collection: - def __init__(self, col_name: str, binary_file: FileIO) -> None: + def __init__(self, col_name: str, file_handler: FileIO) -> None: self._col_name = col_name - self._binary_file = binary_file + self._file_handler = file_handler # Get the data of existing Database or empty database. - self._database = self._binary_file.read() + self._database = self._file_handler.read() self._cursor: int = 0 @@ -69,7 +69,7 @@ def insert(self, document: Mapping) -> str: # print(self._database) # Write current state of Database into the Database-file - self._binary_file.write(self._database) + self._file_handler.write(self._database) return _doc_id else: @@ -99,9 +99,8 @@ def insert_all(self, document_list: List[Mapping]) -> List[str]: return _doc_id - """ FIND_ONE - def __find_one(self, query: Mapping = None) -> Document | None: - " + def __find_one(self, query: Mapping = None) -> Document | None: # Not works, right nom + """ Finds a single ``Document`` of ``Collection``. If ``query`` is None then returns all the ``Documents`` of ``Collection``. @@ -110,7 +109,8 @@ def __find_one(self, query: Mapping = None) -> Document | None: :param query: Condition to search Document :return: Document - " + """ + # Default result _result = {} @@ -131,7 +131,6 @@ def __find_one(self, query: Mapping = None) -> Document | None: _result = _result[self._cursor] return _result - """ def find(self, query=None, limit=None) -> List[Document]: """ @@ -208,7 +207,7 @@ def find(self, query=None, limit=None) -> List[Document]: self._reset_cursor() # check if lower limit is valid or not - if _limit_start >= len(_result): + if _limit_start >= len(_result) and _limit_start != 0: raise ValueError(f"lower limit should be smaller than length of result") else: # Travers limited result @@ -248,7 +247,7 @@ def delete(self, query=None) -> List[str]: self._collection.remove(_doc) _doc_id.append(_doc["_id_"]) - self._binary_file.write(self._database) + self._file_handler.write(self._database) return _doc_id @@ -284,7 +283,7 @@ def update(self, document: Mapping, query=None) -> List[str]: _doc_id.append(_doc["_id_"]) # Write current state of Database - self._binary_file.write(self._database) + self._file_handler.write(self._database) return _doc_id @@ -300,6 +299,15 @@ def count(self, query=None, limit: tuple = None) -> int: return count + def rename(self, new_name: str) -> str: + pass + + def drop(self) -> str: + pass + + + + # ----------------------------------------------------------------# def _reset_cursor(self) -> None: """ Reset Cursor Pointer to 0th index diff --git a/filexdb/database.py b/filexdb/database.py index dabba1c..cb9cd0e 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -1,12 +1,12 @@ from typing import Dict, Type, List from .collection import Collection -from .fileio import BinaryFileIO +from .fileio import BinaryFileIO, JsonFileIO class FileXdb: - def __init__(self, db_name: str, data_dir: str = None): + def __init__(self, db_name: str, data_dir=None, mode="binary"): """ Creates a Databased in ``data_dir`` Directory named ``db_name``. @@ -23,7 +23,10 @@ def __init__(self, db_name: str, data_dir: str = None): self._data_dir = data_dir # Creating an instance of FileIO to Read Write Database-File. - self._binary_file = BinaryFileIO(self._db_name, self._data_dir) + if mode == "binary": + self._file_handler = BinaryFileIO(self._db_name, self._data_dir) + elif mode == "json": + self._file_handler = JsonFileIO(self._db_name, self._data_dir) def collection(self, col_name: str) -> Collection: """ @@ -35,9 +38,13 @@ def collection(self, col_name: str) -> Collection: :return: An instance of Collection Baseclass. """ # Initiating collection - collection = Collection(col_name, self._binary_file) + collection = Collection(col_name, self._file_handler) return collection def show_collections(self): pass + + + + diff --git a/filexdb/fileio.py b/filexdb/fileio.py index 2fb6cab..d8ada98 100644 --- a/filexdb/fileio.py +++ b/filexdb/fileio.py @@ -71,7 +71,7 @@ def write(self, data: dict): class BinaryFileIO(FileIO): - def __init__(self, db_name: str, data_dir: str = None): + def __init__(self, db_name: str, data_dir=None): """ Create a new instance. @@ -87,7 +87,7 @@ def __init__(self, db_name: str, data_dir: str = None): self._data_dir = data_dir - # Adding ``FileXdb`` specific file extention to the Database-file. + # Adding ``FileXdb`` specific file extension to the Database-file. self._db_name = f"{db_name}.fxdb" # Setting default Database-file path. @@ -132,7 +132,7 @@ def read(self) -> dict: raise IOError(f"Cannot read file.\n\t`{self._db_name}` is not a database") else: - # Returns an empty dict as + # Initiate an empty dict as database = {} return database @@ -167,12 +167,85 @@ def write(self, data: dict) -> None: class JsonFileIO(FileIO): - def __init__(self, db_name): + def __init__(self, db_name:str, data_dir=None): + super().__init__() - self.db_name = db_name + + self._data_dir = data_dir + + # Adding ``JSON`` file extension to the Database-file. + self._db_name = f"{db_name}.json" + + # Setting default Database-file path. + self._db_file_path = self._db_name + + # Checking if Data Directory is on root or not. + if self._data_dir is not None: + # Creating Database-file full path by joining data_dir & db_name. + self._db_file_path = os.path.join(self._data_dir, self._db_name) + + # Create the Database/File if it doesn't exist + create_db(self._db_name, self._data_dir) def read(self) -> dict: - pass + """ + Reads existing Database-file, either it is empty or non-empty. + + If empty returns an empty dict, else returns saved Data. + + :return: Database as a python Dictionary. + """ + database = None + + with open(self._db_file_path, "r") as file: + + # Get the file size by moving the cursor to the file end and reading its location. + file.seek(0, os.SEEK_END) + size = file.tell() + + # check if size of file is 0 + if size: + # Bring the cursor to the beginning of Database-file + file.seek(0) + + try: + # Load whole Database form Database-file + database = json.load(file) + + except io.UnsupportedOperation: + # Through an Unsupported Operation Error. + raise IOError(f"Cannot read file.\n\t`{self._db_name}` is not a database") + + else: + # Initiate an empty dict as + database = {} + + return database def write(self, data: dict): - pass + """ + Write the current state of entire Database to the Database-file. + + :param data: Dictionary object to write on Database. + :return: None. + """ + with open(self._db_file_path, "w") as file: + + # Move the cursor to the beginning of the file just in case. + file.seek(0) + + # Serialize the database state using the user-provided arguments + serialized = json.dumps(data, indent=4) + + # Write the serialized data to the file + try: + file.write(serialized) + except io.UnsupportedOperation: + raise IOError(f"Cannot write to the file.\n\t`{self._db_name}` is not a database") + + # Ensure the file has been written + file.flush() + os.fsync(file.fileno()) + + # Remove data that is behind the new cursor if the file has gotten shorter. + file.truncate() From 9d6464688a5569b2fc27e4db427efbc5b6765255 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Mar 2023 15:38:02 +0530 Subject: [PATCH 02/19] Added document counter --- filexdb/collection.py | 38 +++++++++++------------------------ filexdb/document.py | 46 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index f0ee2bd..5d04ce4 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -1,7 +1,7 @@ import json from typing import Mapping, List -from .document import Document +from .document import Document, JsonArray from .fileio import FileIO @@ -75,7 +75,7 @@ def insert(self, document: Mapping) -> str: else: raise ValueError(f"Document id `{_document.id}` is already exists") - def insert_all(self, document_list: List[Mapping]) -> List[str]: + def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]: """ Inserts a single ``Document`` into the ``Database``. @@ -97,7 +97,7 @@ def insert_all(self, document_list: List[Mapping]) -> List[str]: # insert every single document in Database & increment ``doc_count``. _doc_id.append(self.insert(document)) - return _doc_id + return JsonArray(_doc_id) def __find_one(self, query: Mapping = None) -> Document | None: # Not works, right nom """ @@ -132,7 +132,7 @@ def __find_one(self, query: Mapping = None) -> Document | None: # Not wo return _result - def find(self, query=None, limit=None) -> List[Document]: + def find(self, query=None, limit=None) -> JsonArray[Document]: """ Finds all ``Document`` of ``Collection``. @@ -188,7 +188,7 @@ def find(self, query=None, limit=None) -> List[Document]: else: _result = self._collection - return _result + return JsonArray(_result) elif query is not None and type(query) == type({}): if limit: @@ -224,9 +224,9 @@ def find(self, query=None, limit=None) -> List[Document]: self._reset_cursor() - return _result + return JsonArray(_result) - def delete(self, query=None) -> List[str]: + def delete(self, query=None) -> JsonArray[str]: """ Delete single or multiple Document when meet the Conditions or ``query``. @@ -249,9 +249,9 @@ def delete(self, query=None) -> List[str]: self._file_handler.write(self._database) - return _doc_id + return JsonArray(_doc_id) - def update(self, document: Mapping, query=None) -> List[str]: + def update(self, document: Mapping, query=None) -> JsonArray[str]: """ Fetch all the Documents mathc the conditions and update them. @@ -285,7 +285,7 @@ def update(self, document: Mapping, query=None) -> List[str]: # Write current state of Database self._file_handler.write(self._database) - return _doc_id + return JsonArray(_doc_id) def count(self, query=None, limit: tuple = None) -> int: """ @@ -315,7 +315,7 @@ def _reset_cursor(self) -> None: """ self._cursor = 0 - def _find_document_by_query(self, query: Mapping) -> List: + def _find_document_by_query(self, query: Mapping) -> JsonArray[Document]: """ Finds a single ``Document`` of ``Collection``. @@ -379,20 +379,6 @@ def _find_document_by_query(self, query: Mapping) -> List: else: return None - return result - - # ======================== # - def _doc_is_exists(self, doc_id: str) -> bool: - # Iterate over all Documents of Collection - for doc in self._collection: - if doc["_id_"] == doc_id: - return True + return JsonArray(result) - return False - def _find_document_by_id(self, doc_id) -> Document: - for doc in self._collection: - if doc["_id_"] == doc_id: - return doc - else: - return None diff --git a/filexdb/document.py b/filexdb/document.py index 435d5e3..2dce8da 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -27,13 +27,13 @@ def __init__(self, value: Mapping) -> None: super().__init__(self._doc) - def beautify(self) -> str: + def prettify(self) -> str: """ Beautify the ``JSON Object`` with new lines & proper indentation. Convert `JSON Object`` into `JSON String`` using ``json.dumps()``. - :return: JSON String + :return: JSON Object """ # Dumping JSON Object & adding indentation @@ -42,3 +42,45 @@ def beautify(self) -> str: return self._doc + +class JsonArray(list): + def __init__(self, _value: list) -> None: + self.value = _value + super().__init__(self.value) + + + def prettify(self) -> str: + """ + Beautify the ``JSON Array`` with new lines & proper indentation. + + Convert `JSON Array`` into `JSON Array`` using ``json.dumps()``. + + :return: JSON Array + """ + + # Dumping JSON Object & adding indentation + self.value = json.dumps(self.value, indent=4) + + return self.value + + def docs_count(self) -> int: + """ + Return amount of Document found. + + :return: (int) amount of Document found. + """ + count = len(self.value) + + return count + + + + + + + + + + + + From 74f1d6907c51be2b22dda01f23659d16e9471b73 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Mar 2023 15:47:01 +0530 Subject: [PATCH 03/19] remove ``count()`` --- filexdb/collection.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index 5d04ce4..9dc407c 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -287,18 +287,6 @@ def update(self, document: Mapping, query=None) -> JsonArray[str]: return JsonArray(_doc_id) - def count(self, query=None, limit: tuple = None) -> int: - """ - Return amount of Document found. - - :param query: Condition to search Document. - :param limit: If there is any limit. - :return: (int) amount of Document found. - """ - count = len(self.find(query=query, limit=limit)) - - return count - def rename(self, new_name: str) -> str: pass From 5f8d6bb5e39cbb7e265122737f40185611aa16a0 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 03:54:07 +0530 Subject: [PATCH 04/19] re added doc existence check --- filexdb/collection.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/filexdb/collection.py b/filexdb/collection.py index 9dc407c..67f5e95 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -369,4 +369,12 @@ def _find_document_by_query(self, query: Mapping) -> JsonArray[Document]: return JsonArray(result) + def _doc_is_exists(self, doc_id: str) -> bool: + # Iterate over all Documents of Collection + for doc in self._collection: + if doc["_id_"] == doc_id: + return True + + return False + From a0395e5c97a549c43c8bc8dd5d180fabaa8f8e10 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 03:56:42 +0530 Subject: [PATCH 05/19] changed count - count_item --- filexdb/document.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/filexdb/document.py b/filexdb/document.py index 2dce8da..6932db7 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -1,4 +1,4 @@ -from typing import Mapping +from typing import Mapping, List import uuid import json @@ -44,7 +44,7 @@ def prettify(self) -> str: class JsonArray(list): - def __init__(self, _value: list) -> None: + def __init__(self, _value:list) -> None: self.value = _value super().__init__(self.value) @@ -63,7 +63,7 @@ def prettify(self) -> str: return self.value - def docs_count(self) -> int: + def count_item(self) -> int: """ Return amount of Document found. From 67dd93beef9e4c5bd05d825b253cfdab70bc9890 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 03:57:26 +0530 Subject: [PATCH 06/19] Feature Added: Show Collection --- filexdb/database.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/filexdb/database.py b/filexdb/database.py index cb9cd0e..996e661 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -2,7 +2,7 @@ from .collection import Collection from .fileio import BinaryFileIO, JsonFileIO - +from .document import JsonArray class FileXdb: @@ -18,7 +18,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): :param db_name: Name of Database without file extension. :param data_dir: Where the Database will be stored. """ - + self._database = {} self._db_name = db_name self._data_dir = data_dir @@ -42,9 +42,16 @@ def collection(self, col_name: str) -> Collection: return collection - def show_collections(self): - pass + def show_collections(self) -> JsonArray: + """ + Shows all collections of database. + :return: List Collections. + """ + self._database = self._file_handler.read() + # Initiating empty result list + _result = JsonArray(list(self._database.keys())) + return _result From b645c8961c9ed7de63bf5bf20067ade87b129b22 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 06:19:08 +0530 Subject: [PATCH 07/19] Feature Added: JSON Export --- filexdb/database.py | 6 +-- filexdb/document.py | 13 ++++- filexdb/fileio.py | 127 +++++++++++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 35 deletions(-) diff --git a/filexdb/database.py b/filexdb/database.py index 996e661..87a4fbe 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -2,7 +2,8 @@ from .collection import Collection from .fileio import BinaryFileIO, JsonFileIO -from .document import JsonArray +from .document import JsonArray, Document + class FileXdb: @@ -18,7 +19,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): :param db_name: Name of Database without file extension. :param data_dir: Where the Database will be stored. """ - self._database = {} + self._database = Document({}) self._db_name = db_name self._data_dir = data_dir @@ -54,4 +55,3 @@ def show_collections(self) -> JsonArray: return _result - diff --git a/filexdb/document.py b/filexdb/document.py index 6932db7..aa86721 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -1,6 +1,7 @@ from typing import Mapping, List import uuid import json +from .fileio import Export def _get_id(): @@ -44,7 +45,7 @@ def prettify(self) -> str: class JsonArray(list): - def __init__(self, _value:list) -> None: + def __init__(self, _value: list) -> None: self.value = _value super().__init__(self.value) @@ -75,6 +76,16 @@ def count_item(self) -> int: + def export(self, _file_name, _file_dir=None, _mode="json"): + """ + + :param _file_name: + :param _file_dir: + :param _mode: + :return: + """ + + e = Export(self.value, _file_name, _file_dir, _mode) diff --git a/filexdb/fileio.py b/filexdb/fileio.py index d8ada98..688ad5a 100644 --- a/filexdb/fileio.py +++ b/filexdb/fileio.py @@ -6,8 +6,10 @@ __all__ = ("FileIO", "JsonFileIO", "BinaryFileIO") +from typing import Tuple -def create_db(db_name: str, data_dir: str = None): + +def create_file(db_name: str, data_dir: str = None): """ Create a file if it doesn't exist yet. @@ -30,8 +32,32 @@ def create_db(db_name: str, data_dir: str = None): # Create the file by opening it in "a" mode which creates the file if it # does not exist yet but does not modify its contents - with open(_db_path, 'ab'): - pass + if not os.path.exists(_db_path): + with open(_db_path, 'a'): + pass + + +def pre_process(ext: str, db_name: str, data_dir=None) -> tuple[str, str]: + """ + + :param ext: + :param db_name: + :param data_dir: + :return: + """ + + # Adding ``FileXdb`` specific file extension to the Database-file. + _file_name = f"{db_name}.{ext}" + + # Setting default Database-file path. + _file_full_path = _file_name + + # Checking if Data Directory is on root or not. + if data_dir is not None: + # Creating Database-file full path by joining data_dir & db_name. + _file_full_path = os.path.join(data_dir, _file_name) + + return _file_name, _file_full_path class FileIO(ABC): @@ -69,8 +95,20 @@ def write(self, data: dict): raise NotImplementedError('To be overridden!') + @abstractmethod + def get_export_path(self) -> str: + """ + Return Database path + + :return: TDatabase path. + """ + + raise NotImplementedError('To be overridden!') + + class BinaryFileIO(FileIO): + def __init__(self, db_name: str, data_dir=None): """ Create a new instance. @@ -85,22 +123,11 @@ def __init__(self, db_name: str, data_dir=None): super().__init__() - self._data_dir = data_dir - - # Adding ``FileXdb`` specific file extension to the Database-file. - self._db_name = f"{db_name}.fxdb" - - # Setting default Database-file path. - self._db_file_path = self._db_name - - # Checking if Data Directory is on root or not. - if self._data_dir is not None: - - # Creating Database-file full path by joining data_dir & db_name. - self._db_file_path = os.path.join(self._data_dir, self._db_name) + self._db_name, self._db_file_path = pre_process("fxdb", db_name, data_dir) # Create the Database/File if it doesn't exist - create_db(self._db_name, self._data_dir) + create_file(self._db_name, data_dir) + def read(self) -> dict: """ @@ -165,27 +192,19 @@ def write(self, data: dict) -> None: # Remove data that is behind the new cursor if the file has gotten shorter. file.truncate() + def get_export_path(self) -> str: + return self._db_file_path + class JsonFileIO(FileIO): - def __init__(self, db_name:str, data_dir=None): + def __init__(self, db_name: str, data_dir=None): super().__init__() - self._data_dir = data_dir - - # Adding ``JSON`` file extension to the Database-file. - self._db_name = f"{db_name}.json" - - # Setting default Database-file path. - self._db_file_path = self._db_name - - # Checking if Data Directory is on root or not. - if self._data_dir is not None: - # Creating Database-file full path by joining data_dir & db_name. - self._db_file_path = os.path.join(self._data_dir, self._db_name) + self._db_name, self._db_file_path = pre_process("json", db_name, data_dir) # Create the Database/File if it doesn't exist - create_db(self._db_name, self._data_dir) + create_file(self._db_name, data_dir) def read(self) -> dict: """ @@ -249,3 +268,49 @@ def write(self, data: dict): # Remove data that is behind the new cursor if the file has gotten shorter. file.truncate() + + def get_export_path(self) -> str: + return self._db_file_path + + +class Export: + def __init__(self, _data, _file_name=None, _file_dir=None, _mode="json") -> None: + """ + Exports data into different readable file. + + :param _file_name: Where to export. + :param _file_dir: Parent dir. + :param _data: Data to export. + :param _mode: Export to which mode. + """ + + # Caching arguments + self.data = _data + self.file_name = _file_name + self.file_dir = _file_dir + self.mode = _mode + + self._db_name, self._db_file_path = pre_process(self.mode, self.file_name, self.file_dir) + + # Create the Database/File if it doesn't exist + create_file(self._db_name, self.file_dir) + + # check mode + if self.mode == "json": + self.to_json(self.data, self._db_file_path) + else: + raise TypeError(f"`{self.mode}` is not a appropriate mode to export") + + + + def to_json(self, _data, _file_path) -> None: + """ + Exports data into JSON file. + + :param _data: Data to export. + :param _file_path: Where to export. + :return: None + """ + with open(_file_path, "w") as f: + json.dump(_data, fp=f, indent=4) + From ecf594c618498c7d1315b564d925fb6a796bf28b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 06:51:06 +0530 Subject: [PATCH 08/19] Feature Added: Show & Export Database --- filexdb/database.py | 22 ++++++++++++++++++++-- filexdb/document.py | 5 ++++- filexdb/fileio.py | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/filexdb/database.py b/filexdb/database.py index 87a4fbe..4e213f1 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -1,7 +1,7 @@ from typing import Dict, Type, List from .collection import Collection -from .fileio import BinaryFileIO, JsonFileIO +from .fileio import BinaryFileIO, JsonFileIO, Export from .document import JsonArray, Document @@ -19,7 +19,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): :param db_name: Name of Database without file extension. :param data_dir: Where the Database will be stored. """ - self._database = Document({}) + self._database = {} self._db_name = db_name self._data_dir = data_dir @@ -55,3 +55,21 @@ def show_collections(self) -> JsonArray: return _result + def show(self) -> Document: + """ + + :return: Database + """ + self._database = self._file_handler.read() + return Document(self._database, False) + + def export(self, _file_name, _file_dir=None, _mode="json"): + """ + + :param _file_name: + :param _file_dir: + :param _mode: + :return: + """ + + e = Export(self.show(), _file_name, _file_dir, _mode) \ No newline at end of file diff --git a/filexdb/document.py b/filexdb/document.py index aa86721..7b48b57 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -3,6 +3,7 @@ import json from .fileio import Export +__all__ = ("Document", "JsonArray") def _get_id(): _id = uuid.uuid1() @@ -10,7 +11,7 @@ def _get_id(): class Document(dict): - def __init__(self, value: Mapping) -> None: + def __init__(self, value: Mapping, gen_id: bool = True) -> None: self.id = None _id_obj = { @@ -20,6 +21,8 @@ def __init__(self, value: Mapping) -> None: if "_id_" in value.keys(): self._doc = value self.id = value["_id_"] + elif "_id_" not in value.keys() and not gen_id: + self._doc = value else: self._doc = _id_obj for k, v in value.items(): diff --git a/filexdb/fileio.py b/filexdb/fileio.py index 688ad5a..894617c 100644 --- a/filexdb/fileio.py +++ b/filexdb/fileio.py @@ -4,7 +4,7 @@ import io from abc import ABC, abstractmethod -__all__ = ("FileIO", "JsonFileIO", "BinaryFileIO") +__all__ = ("FileIO", "JsonFileIO", "BinaryFileIO", "Export") from typing import Tuple From 4b9cfd63cd07c34d592cd74b547758e65b074478 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 07:28:59 +0530 Subject: [PATCH 09/19] test count & export --- test/test_collection.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_collection.py b/test/test_collection.py index c185b60..ff83221 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -40,6 +40,24 @@ def test_find(): assert student_info.find(limit=(1, 10)) # Returns doc[1] to doc[9] of all Documents. +def test_count_item(): + _query_1 = {"name": "Sam"} + + assert student_info.find().count_item() + assert student_info.find(query=_query_1).count_item() + assert student_info.find(query=_query_1, limit=(1, 3)).count_item() + assert student_info.find(limit=(1, 10)).count_item() + + +def test_export(): + _query_1 = {"name": "Sam"} + + student_info.find().export("test-db-Sam-1", "test_data/export") + student_info.find(query=_query_1).export("test-db-Sam-2", "test_data/export") + student_info.find(query=_query_1, limit=(1, 3)).export("test-db-Sam-3", "test_data/export") + student_info.find(limit=(1, 10)).export("test-db-Sam-4", "test_data/export") + + def test_delete(): assert student_info.delete({"name": "Addy"}) assert student_info.delete({"name": "Sam", "roll": "CSE/17/19"}) From e4e6d92b35967f60f2fb9b1dc501d14ef56dbc81 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 07:29:24 +0530 Subject: [PATCH 10/19] Test Added: database --- test/test_database.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/test_database.py diff --git a/test/test_database.py b/test/test_database.py new file mode 100644 index 0000000..0fa4673 --- /dev/null +++ b/test/test_database.py @@ -0,0 +1,38 @@ +from filexdb import FileXdb + + +# Create an instance of Database +db = FileXdb("test_DB", "test_data/db") +db_2 = FileXdb("NewDb", "test_data/db") + +# Create a Collection +student_info = db.collection("student_info") +player_info = db.collection("student_info") + +student_info.insert_all([ + {"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05}, + {"name": "Roman", "roll": "ECE/80/13", "dept": "ECE", "skill": ["game design"], "spc": ["Blinder"]}, + {"name": "Sam"}]) + +player_info.insert({"name": "Rana", "sport": "Cricket"}) + + +def test_show_collections(): + assert db.show_collections() + db_2.show_collections() + + +def test_show(): + assert db.show() + db_2.show() + + # prettify json object + assert db.show().prettify() + assert db_2.show().prettify() + + +def test_export(): + db.export("test-db-exp", "test_data/export") + db_2.export("test-db-2-exp", "test_data/export") + + From e40ae61d557c8425d99b9295af5cb4f82d1eba53 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 07:34:25 +0530 Subject: [PATCH 11/19] minor fixing --- filexdb/document.py | 2 -- filexdb/fileio.py | 9 ++++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/filexdb/document.py b/filexdb/document.py index 7b48b57..5a89ce9 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -77,8 +77,6 @@ def count_item(self) -> int: return count - - def export(self, _file_name, _file_dir=None, _mode="json"): """ diff --git a/filexdb/fileio.py b/filexdb/fileio.py index 894617c..3735b2d 100644 --- a/filexdb/fileio.py +++ b/filexdb/fileio.py @@ -37,7 +37,7 @@ def create_file(db_name: str, data_dir: str = None): pass -def pre_process(ext: str, db_name: str, data_dir=None) -> tuple[str, str]: +def pre_process(ext: str, db_name: str, data_dir=None): """ :param ext: @@ -311,6 +311,9 @@ def to_json(self, _data, _file_path) -> None: :param _file_path: Where to export. :return: None """ - with open(_file_path, "w") as f: - json.dump(_data, fp=f, indent=4) + try: + with open(_file_path, "w") as f: + json.dump(_data, fp=f, indent=4) + except Exception as e: + print(e) From d59a78763dd6cc84379f19aefcd61f12c6e49c8e Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 07:37:32 +0530 Subject: [PATCH 12/19] Remove ``find_one()`` --- filexdb/collection.py | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index 67f5e95..15b18b3 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -99,39 +99,6 @@ def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]: return JsonArray(_doc_id) - def __find_one(self, query: Mapping = None) -> Document | None: # Not works, right nom - """ - Finds a single ``Document`` of ``Collection``. - - If ``query`` is None then returns all the ``Documents`` of ``Collection``. - - If ``query`` is not None then returns only the first occurrence. - - :param query: Condition to search Document - :return: Document - """ - - # Default result - _result = {} - - # Make sure the query implements the ``Mapping`` interface. - if not isinstance(query, Mapping | None): - raise ValueError('Document is not a Dictionary') - - - - # Check if has ``query`` or not - if query is None: - _result = self._collection[self._cursor] - self._reset_cursor() - else: - print(self._cursor) - _result = self._find_document_by_query(query) - self._reset_cursor() - _result = _result[self._cursor] - - return _result - def find(self, query=None, limit=None) -> JsonArray[Document]: """ Finds all ``Document`` of ``Collection``. From f05b5e79c4f54677f30c79975fadbf9cfca46f49 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 11 Mar 2023 15:23:22 +0530 Subject: [PATCH 13/19] Added minor documentation --- filexdb/database.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/filexdb/database.py b/filexdb/database.py index 4e213f1..07a960a 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -55,21 +55,23 @@ def show_collections(self) -> JsonArray: return _result - def show(self) -> Document: + def export(self, _file_name, _file_dir=None, _mode="json"): """ + Export data in to readable file. - :return: Database + :param _file_name: File name in which data will be exported. + :param _file_dir: Parent directory of export file. + :param _mode: In which file mode you want to export data. + :return: None. """ - self._database = self._file_handler.read() - return Document(self._database, False) - def export(self, _file_name, _file_dir=None, _mode="json"): - """ + e = Export(self._show(), _file_name, _file_dir, _mode) - :param _file_name: - :param _file_dir: - :param _mode: - :return: + def _show(self) -> Document: """ + Shows the hole Database. - e = Export(self.show(), _file_name, _file_dir, _mode) \ No newline at end of file + :return: Database + """ + self._database = self._file_handler.read() + return Document(self._database, False) From d95733b207310f39cce484c34212f34a3a577705 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 11 Mar 2023 17:42:53 +0530 Subject: [PATCH 14/19] Feature Added: Rename & Drop --- filexdb/collection.py | 65 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index 15b18b3..954cac4 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -19,7 +19,7 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None: self._file_handler = file_handler # Get the data of existing Database or empty database. - self._database = self._file_handler.read() + self._database = Document(self._file_handler.read(), False) self._cursor: int = 0 @@ -28,12 +28,12 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None: if self._col_name in self._database.keys(): # Get the existing Collection - self._collection: list = self._database[self._col_name] + self._collection: JsonArray = self._database[self._col_name] else: # Create new Collection - self._database[self._col_name] = [] - self._collection: list = self._database[self._col_name] + self._database[self._col_name] = JsonArray([]) + self._collection: JsonArray = self._database[self._col_name] def insert(self, document: Mapping) -> str: """ @@ -254,12 +254,61 @@ def update(self, document: Mapping, query=None) -> JsonArray[str]: return JsonArray(_doc_id) - def rename(self, new_name: str) -> str: - pass + def rename(self, new_name: str) -> int: + """ + This method used to change the name of collection. + Takes current name & new name to change name of the collection. + + :param new_name: New name for collection. + :return: Amount of affected collection. + """ + + # Initiating counter + count = 0 + + # Checking the collection is already exist or not + if new_name not in self._database.keys(): + + # Creating new collection and + # Putting old data into new collection + self._database[new_name] = self._collection + + # Writing Current database status into the file + self._file_handler.write(self._database) + + # Remove old collection + self.drop() + + # Increasing counter + count += 1 + + return count + + def drop(self) -> int: + """ + Deletes the selected collection from the database + + :return: Amount of affected collection + """ + + # Initiating counter + count = 0 + + # Getting database + _database = self._file_handler.read() + + # Check database has the collection or not + if self._col_name in _database.keys(): + # Removing collection from database + _database.pop(self._col_name) + + # Writing current status of database into the file system. + self._file_handler.write(_database) - def drop(self) -> str: - pass + # Increasing counter + count += 1 + return count # ----------------------------------------------------------------# From 75f658f72a3b2ad436b3a344a43eb216a3a426e7 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 11 Mar 2023 17:43:29 +0530 Subject: [PATCH 15/19] minor modification --- filexdb/database.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/filexdb/database.py b/filexdb/database.py index 07a960a..d2f9b71 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -19,7 +19,6 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): :param db_name: Name of Database without file extension. :param data_dir: Where the Database will be stored. """ - self._database = {} self._db_name = db_name self._data_dir = data_dir @@ -29,6 +28,10 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): elif mode == "json": self._file_handler = JsonFileIO(self._db_name, self._data_dir) + # Getting whole database. + self._database = self._show() + + def collection(self, col_name: str) -> Collection: """ Creates a brand-new Collection if the Collection is not exists. @@ -65,7 +68,8 @@ def export(self, _file_name, _file_dir=None, _mode="json"): :return: None. """ - e = Export(self._show(), _file_name, _file_dir, _mode) + e = Export(self._database, _file_name, _file_dir, _mode) + def _show(self) -> Document: """ From 47f361bf0d0fafb10e02eed6457317014104d64b Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 11 Mar 2023 23:48:25 +0530 Subject: [PATCH 16/19] Fixed multiple insertion error --- filexdb/collection.py | 63 ++++++++++++++++++++++++++++++----------- test/test_collection.py | 24 +++++++++++++--- test/test_database.py | 12 ++++---- 3 files changed, 73 insertions(+), 26 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index 954cac4..eb98d3a 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -19,21 +19,15 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None: self._file_handler = file_handler # Get the data of existing Database or empty database. - self._database = Document(self._file_handler.read(), False) + self._database = self._get_database() - self._cursor: int = 0 + # Initiating Collecting + self._collection = self._get_collection() - # Initiate a default Collection. - # Check the Collection is already exists or no. - if self._col_name in self._database.keys(): + # Cursor + self._cursor: int = 0 - # Get the existing Collection - self._collection: JsonArray = self._database[self._col_name] - else: - # Create new Collection - self._database[self._col_name] = JsonArray([]) - self._collection: JsonArray = self._database[self._col_name] def insert(self, document: Mapping) -> str: """ @@ -41,9 +35,10 @@ def insert(self, document: Mapping) -> str: Document should be JSON Object. - :param document: Document to insert into database - :return: None + :param document: Document to insert into the database. + :return: Document ID. """ + # Make sure the document implements the ``Mapping`` interface if not isinstance(document, Mapping): raise ValueError('Document is not a Dictionary') @@ -52,10 +47,14 @@ def insert(self, document: Mapping) -> str: if "_id_" in document.keys(): raise KeyError(f"You are not allowed to modify key `_id_`") + + # getting Database + _database = self._get_database() + # Create a Document _document = Document(document) - # id of Document + # ID of Document _doc_id: str = _document.id # check Document is already exist or not @@ -65,11 +64,11 @@ def insert(self, document: Mapping) -> str: self._collection.append(_document) # Add modified Collection to Database - self._database[self._col_name] = self._collection + _database[self._col_name] = self._collection # print(self._database) # Write current state of Database into the Database-file - self._file_handler.write(self._database) + self._file_handler.write(_database) return _doc_id else: @@ -312,6 +311,38 @@ def drop(self) -> int: # ----------------------------------------------------------------# + def _get_database(self) -> Document: + """ + Getting Database + + :return: Database + """ + # Get the data of existing Database or empty database. + database = Document(self._file_handler.read(), False) + + return database + + def _get_collection(self) -> JsonArray: + """ + Getting Collection + + :return: Collection + """ + # Initiate a default Collection. + # Check the Collection is already exists or no. + if self._col_name in self._database.keys(): + + # Get the existing Collection + _collection: JsonArray = self._database[self._col_name] + + else: + # Create new Collection + self._database[self._col_name] = JsonArray([]) + _collection: JsonArray = self._database[self._col_name] + + return _collection + + def _reset_cursor(self) -> None: """ Reset Cursor Pointer to 0th index diff --git a/test/test_collection.py b/test/test_collection.py index ff83221..34fbfab 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -6,12 +6,14 @@ # Create a Collection student_info = db.collection("student_info") +student_2 = db.collection("student_2") def test_insert(): assert student_info.insert({"name": "Sam", "roll": "CSE/17/19", "dept": "CSE"}) assert student_info.insert({"name": "Bob", "roll": "EE/01/18", "dept": "EE", "skill": ["python", "c++"]}) assert student_info.insert({"name": "Rana", "dept": "CSE"}) + assert student_2.insert({"name": "Rana", "dept": "CSE"}) def test_insert_all(): @@ -19,8 +21,12 @@ def test_insert_all(): {"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05}, {"name": "Roman", "roll": "ECE/80/13", "dept": "ECE", "skill": ["game design"], "spc": ["Blinder"]}, {"name": "Sam"} - ] - ) + ]) + assert student_2.insert_all([ + {"name": "Addy", "roll": "CSE/57/19", "dept": "CSE", "cgpa": 9.05}, + {"name": "Roman", "roll": "CSE/80/13", "dept": "CSE", "skill": ["game design"], "spc": ["Blinder"]}, + {"name": "Sam", "dept": "CSE"} + ]) def test_find(): @@ -36,7 +42,7 @@ def test_find(): assert student_info.find() # Returns all Documents. assert student_info.find(query=_query_2) # Returns all Documents matches the ``_query``. - assert student_info.find(query=_query_2, limit=(0, 30)) # Returns doc[1] to doc[2] matches the ``_query``. + assert student_info.find(query=_query_2, limit=(0, 30)) # Returns doc[1] to doc[2] matches the ``_query``. assert student_info.find(limit=(1, 10)) # Returns doc[1] to doc[9] of all Documents. @@ -64,7 +70,17 @@ def test_delete(): assert student_info.delete({"name": "Roman", "roll": "ECE/80/13", "dept": "ECE"}) -def update_document(): +def test_update_document(): assert student_info.update({"passed": True, "mobile": 123456789}, {"name": "Bob"}) assert student_info.update({"name": "The Sam", "skill": ["C++", "Python"]}, {"name": "Sam"}) assert student_info.update({"dept": "Computer Science & Engineering"}, {"dept": "CSE"}) + + +def test_rename(): + student_2.rename("student_info_cse") + + +def test_drop(): + student_info.drop() + + diff --git a/test/test_database.py b/test/test_database.py index 0fa4673..d3bee37 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -6,8 +6,8 @@ db_2 = FileXdb("NewDb", "test_data/db") # Create a Collection -student_info = db.collection("student_info") -player_info = db.collection("student_info") +student_info = db_2.collection("student_info") +player_info = db_2.collection("student_info") student_info.insert_all([ {"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05}, @@ -23,12 +23,12 @@ def test_show_collections(): def test_show(): - assert db.show() - db_2.show() + db._show() + db_2._show() # prettify json object - assert db.show().prettify() - assert db_2.show().prettify() + assert db._show().prettify() + assert db_2._show().prettify() def test_export(): From 7de41c856bb25a8aee0ecec3047d53ce57ee395c Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 20 Mar 2023 03:48:44 +0530 Subject: [PATCH 17/19] Added tox --- tox.ini | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tox.ini diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..1f49dd8 --- /dev/null +++ b/tox.ini @@ -0,0 +1,19 @@ +[tox] +envlist = py{37,38,39,310}-{pytest,mypy} + + +[testenv:py310-pytest] +description = Run pytest. +deps = + pytest +; {[env]deps} +commands = + pytest + + +[testenv:py310-mypy] +description = Run mypy +deps = + mypy +commands = + mypy --install-types --non-interactive {toxinidir}/filexdb \ No newline at end of file From df5f9bf31ca7d19c1328ec28cf1624d852a52a79 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 20 Mar 2023 03:49:23 +0530 Subject: [PATCH 18/19] Fixed type errors for all version --- filexdb/collection.py | 30 +++++++++++------------------- filexdb/database.py | 7 ++++--- filexdb/document.py | 10 +++++----- filexdb/fileio.py | 3 ++- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/filexdb/collection.py b/filexdb/collection.py index eb98d3a..decbc39 100644 --- a/filexdb/collection.py +++ b/filexdb/collection.py @@ -22,13 +22,11 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None: self._database = self._get_database() # Initiating Collecting - self._collection = self._get_collection() + self._collection: JsonArray = self._get_collection() # Cursor self._cursor: int = 0 - - def insert(self, document: Mapping) -> str: """ Inserts a single Document into the Database. @@ -47,7 +45,6 @@ def insert(self, document: Mapping) -> str: if "_id_" in document.keys(): raise KeyError(f"You are not allowed to modify key `_id_`") - # getting Database _database = self._get_database() @@ -55,10 +52,10 @@ def insert(self, document: Mapping) -> str: _document = Document(document) # ID of Document - _doc_id: str = _document.id + _doc_id: str = str(_document.id) # check Document is already exist or not - if not self._doc_is_exists(_document.id): + if not self._doc_is_exists(str(_document.id)): # Append the document into the Collection self._collection.append(_document) @@ -74,7 +71,7 @@ def insert(self, document: Mapping) -> str: else: raise ValueError(f"Document id `{_document.id}` is already exists") - def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]: + def insert_all(self, document_list: List[Mapping]) -> JsonArray: """ Inserts a single ``Document`` into the ``Database``. @@ -98,7 +95,7 @@ def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]: return JsonArray(_doc_id) - def find(self, query=None, limit=None) -> JsonArray[Document]: + def find(self, query=None, limit=None) -> JsonArray: """ Finds all ``Document`` of ``Collection``. @@ -125,7 +122,7 @@ def find(self, query=None, limit=None) -> JsonArray[Document]: raise ValueError('Document is not a Tuple') # if limit, Check everything ok - _limit_start = _limit_end = None + _limit_start = _limit_end = 0 if limit and type(limit) == type((1, 3)): if len(limit) == 2: @@ -192,7 +189,7 @@ def find(self, query=None, limit=None) -> JsonArray[Document]: return JsonArray(_result) - def delete(self, query=None) -> JsonArray[str]: + def delete(self, query=None) -> JsonArray: """ Delete single or multiple Document when meet the Conditions or ``query``. @@ -217,7 +214,7 @@ def delete(self, query=None) -> JsonArray[str]: return JsonArray(_doc_id) - def update(self, document: Mapping, query=None) -> JsonArray[str]: + def update(self, document: Mapping, query=None) -> JsonArray: """ Fetch all the Documents mathc the conditions and update them. @@ -267,7 +264,6 @@ def rename(self, new_name: str) -> int: # Checking the collection is already exist or not if new_name not in self._database.keys(): - # Creating new collection and # Putting old data into new collection self._database[new_name] = self._collection @@ -309,7 +305,6 @@ def drop(self) -> int: return count - # ----------------------------------------------------------------# def _get_database(self) -> Document: """ @@ -338,10 +333,9 @@ def _get_collection(self) -> JsonArray: else: # Create new Collection self._database[self._col_name] = JsonArray([]) - _collection: JsonArray = self._database[self._col_name] - - return _collection + _collection = self._database[self._col_name] + return JsonArray(_collection) def _reset_cursor(self) -> None: """ @@ -350,7 +344,7 @@ def _reset_cursor(self) -> None: """ self._cursor = 0 - def _find_document_by_query(self, query: Mapping) -> JsonArray[Document]: + def _find_document_by_query(self, query: Mapping) -> JsonArray | None: """ Finds a single ``Document`` of ``Collection``. @@ -423,5 +417,3 @@ def _doc_is_exists(self, doc_id: str) -> bool: return True return False - - diff --git a/filexdb/database.py b/filexdb/database.py index d2f9b71..30ea0b3 100644 --- a/filexdb/database.py +++ b/filexdb/database.py @@ -1,7 +1,7 @@ from typing import Dict, Type, List from .collection import Collection -from .fileio import BinaryFileIO, JsonFileIO, Export +from .fileio import FileIO, BinaryFileIO, JsonFileIO, Export from .document import JsonArray, Document @@ -21,6 +21,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"): """ self._db_name = db_name self._data_dir = data_dir + self._file_handler: FileIO # Creating an instance of FileIO to Read Write Database-File. if mode == "binary": @@ -51,7 +52,7 @@ def show_collections(self) -> JsonArray: Shows all collections of database. :return: List Collections. """ - self._database = self._file_handler.read() + self._database = Document(self._file_handler.read(), False) # Initiating empty result list _result = JsonArray(list(self._database.keys())) @@ -77,5 +78,5 @@ def _show(self) -> Document: :return: Database """ - self._database = self._file_handler.read() + self._database = Document(self._file_handler.read(), False) return Document(self._database, False) diff --git a/filexdb/document.py b/filexdb/document.py index 5a89ce9..cb8be5a 100644 --- a/filexdb/document.py +++ b/filexdb/document.py @@ -1,4 +1,4 @@ -from typing import Mapping, List +from typing import Mapping, List, Any import uuid import json from .fileio import Export @@ -41,9 +41,9 @@ def prettify(self) -> str: """ # Dumping JSON Object & adding indentation - self._doc = json.dumps(self._doc, indent=4) + _doc: str = json.dumps(self._doc, indent=4) - return self._doc + return _doc @@ -63,9 +63,9 @@ def prettify(self) -> str: """ # Dumping JSON Object & adding indentation - self.value = json.dumps(self.value, indent=4) + value: str = json.dumps(self.value, indent=4) - return self.value + return value def count_item(self) -> int: """ diff --git a/filexdb/fileio.py b/filexdb/fileio.py index 3735b2d..198f5cc 100644 --- a/filexdb/fileio.py +++ b/filexdb/fileio.py @@ -3,13 +3,14 @@ import os import io from abc import ABC, abstractmethod +# from filexdb.document import Document __all__ = ("FileIO", "JsonFileIO", "BinaryFileIO", "Export") from typing import Tuple -def create_file(db_name: str, data_dir: str = None): +def create_file(db_name: str, data_dir: str | None): """ Create a file if it doesn't exist yet. From c7b6d170ab094b3fba2654dc7ec4466d8406fd80 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 20 Mar 2023 05:17:44 +0530 Subject: [PATCH 19/19] 1.0.0 --- filexdb/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filexdb/__init__.py b/filexdb/__init__.py index faea474..044a0f5 100644 --- a/filexdb/__init__.py +++ b/filexdb/__init__.py @@ -2,5 +2,5 @@ from .database import FileXdb -__version__ = "0.1.0" +__version__ = "1.0.0"