Skip to content

Commit f5a7004

Browse files
authored
Merge pull request #3 from the-sam963/new-feature
New feature
2 parents cc3dfa0 + df5f9bf commit f5a7004

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

filexdb/collection.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None:
2222
self._database = self._get_database()
2323

2424
# Initiating Collecting
25-
self._collection = self._get_collection()
25+
self._collection: JsonArray = self._get_collection()
2626

2727
# Cursor
2828
self._cursor: int = 0
2929

30-
31-
3230
def insert(self, document: Mapping) -> str:
3331
"""
3432
Inserts a single Document into the Database.
@@ -47,18 +45,17 @@ def insert(self, document: Mapping) -> str:
4745
if "_id_" in document.keys():
4846
raise KeyError(f"You are not allowed to modify key `_id_`")
4947

50-
5148
# getting Database
5249
_database = self._get_database()
5350

5451
# Create a Document
5552
_document = Document(document)
5653

5754
# ID of Document
58-
_doc_id: str = _document.id
55+
_doc_id: str = str(_document.id)
5956

6057
# check Document is already exist or not
61-
if not self._doc_is_exists(_document.id):
58+
if not self._doc_is_exists(str(_document.id)):
6259

6360
# Append the document into the Collection
6461
self._collection.append(_document)
@@ -74,7 +71,7 @@ def insert(self, document: Mapping) -> str:
7471
else:
7572
raise ValueError(f"Document id `{_document.id}` is already exists")
7673

77-
def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]:
74+
def insert_all(self, document_list: List[Mapping]) -> JsonArray:
7875
"""
7976
Inserts a single ``Document`` into the ``Database``.
8077
@@ -98,7 +95,7 @@ def insert_all(self, document_list: List[Mapping]) -> JsonArray[str]:
9895

9996
return JsonArray(_doc_id)
10097

101-
def find(self, query=None, limit=None) -> JsonArray[Document]:
98+
def find(self, query=None, limit=None) -> JsonArray:
10299
"""
103100
Finds all ``Document`` of ``Collection``.
104101
@@ -125,7 +122,7 @@ def find(self, query=None, limit=None) -> JsonArray[Document]:
125122
raise ValueError('Document is not a Tuple')
126123

127124
# if limit, Check everything ok
128-
_limit_start = _limit_end = None
125+
_limit_start = _limit_end = 0
129126

130127
if limit and type(limit) == type((1, 3)):
131128
if len(limit) == 2:
@@ -192,7 +189,7 @@ def find(self, query=None, limit=None) -> JsonArray[Document]:
192189

193190
return JsonArray(_result)
194191

195-
def delete(self, query=None) -> JsonArray[str]:
192+
def delete(self, query=None) -> JsonArray:
196193
"""
197194
Delete single or multiple Document when meet the Conditions or ``query``.
198195
@@ -217,7 +214,7 @@ def delete(self, query=None) -> JsonArray[str]:
217214

218215
return JsonArray(_doc_id)
219216

220-
def update(self, document: Mapping, query=None) -> JsonArray[str]:
217+
def update(self, document: Mapping, query=None) -> JsonArray:
221218
"""
222219
Fetch all the Documents mathc the conditions and update them.
223220
@@ -267,7 +264,6 @@ def rename(self, new_name: str) -> int:
267264

268265
# Checking the collection is already exist or not
269266
if new_name not in self._database.keys():
270-
271267
# Creating new collection and
272268
# Putting old data into new collection
273269
self._database[new_name] = self._collection
@@ -309,7 +305,6 @@ def drop(self) -> int:
309305

310306
return count
311307

312-
313308
# ----------------------------------------------------------------#
314309
def _get_database(self) -> Document:
315310
"""
@@ -338,10 +333,9 @@ def _get_collection(self) -> JsonArray:
338333
else:
339334
# Create new Collection
340335
self._database[self._col_name] = JsonArray([])
341-
_collection: JsonArray = self._database[self._col_name]
342-
343-
return _collection
336+
_collection = self._database[self._col_name]
344337

338+
return JsonArray(_collection)
345339

346340
def _reset_cursor(self) -> None:
347341
"""
@@ -350,7 +344,7 @@ def _reset_cursor(self) -> None:
350344
"""
351345
self._cursor = 0
352346

353-
def _find_document_by_query(self, query: Mapping) -> JsonArray[Document]:
347+
def _find_document_by_query(self, query: Mapping) -> JsonArray | None:
354348
"""
355349
Finds a single ``Document`` of ``Collection``.
356350
@@ -423,5 +417,3 @@ def _doc_is_exists(self, doc_id: str) -> bool:
423417
return True
424418

425419
return False
426-
427-

filexdb/database.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict, Type, List
22

33
from .collection import Collection
4-
from .fileio import BinaryFileIO, JsonFileIO, Export
4+
from .fileio import FileIO, BinaryFileIO, JsonFileIO, Export
55
from .document import JsonArray, Document
66

77

@@ -21,6 +21,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"):
2121
"""
2222
self._db_name = db_name
2323
self._data_dir = data_dir
24+
self._file_handler: FileIO
2425

2526
# Creating an instance of FileIO to Read Write Database-File.
2627
if mode == "binary":
@@ -51,7 +52,7 @@ def show_collections(self) -> JsonArray:
5152
Shows all collections of database.
5253
:return: List Collections.
5354
"""
54-
self._database = self._file_handler.read()
55+
self._database = Document(self._file_handler.read(), False)
5556

5657
# Initiating empty result list
5758
_result = JsonArray(list(self._database.keys()))
@@ -77,5 +78,5 @@ def _show(self) -> Document:
7778
7879
:return: Database
7980
"""
80-
self._database = self._file_handler.read()
81+
self._database = Document(self._file_handler.read(), False)
8182
return Document(self._database, False)

filexdb/document.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Mapping, List
1+
from typing import Mapping, List, Any
22
import uuid
33
import json
44
from .fileio import Export
@@ -41,9 +41,9 @@ def prettify(self) -> str:
4141
"""
4242

4343
# Dumping JSON Object & adding indentation
44-
self._doc = json.dumps(self._doc, indent=4)
44+
_doc: str = json.dumps(self._doc, indent=4)
4545

46-
return self._doc
46+
return _doc
4747

4848

4949

@@ -63,9 +63,9 @@ def prettify(self) -> str:
6363
"""
6464

6565
# Dumping JSON Object & adding indentation
66-
self.value = json.dumps(self.value, indent=4)
66+
value: str = json.dumps(self.value, indent=4)
6767

68-
return self.value
68+
return value
6969

7070
def count_item(self) -> int:
7171
"""

filexdb/fileio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import os
44
import io
55
from abc import ABC, abstractmethod
6+
# from filexdb.document import Document
67

78
__all__ = ("FileIO", "JsonFileIO", "BinaryFileIO", "Export")
89

910
from typing import Tuple
1011

1112

12-
def create_file(db_name: str, data_dir: str = None):
13+
def create_file(db_name: str, data_dir: str | None):
1314
"""
1415
Create a file if it doesn't exist yet.
1516

tox.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[tox]
2+
envlist = py{37,38,39,310}-{pytest,mypy}
3+
4+
5+
[testenv:py310-pytest]
6+
description = Run pytest.
7+
deps =
8+
pytest
9+
; {[env]deps}
10+
commands =
11+
pytest
12+
13+
14+
[testenv:py310-mypy]
15+
description = Run mypy
16+
deps =
17+
mypy
18+
commands =
19+
mypy --install-types --non-interactive {toxinidir}/filexdb

0 commit comments

Comments
 (0)