Skip to content

Commit d241b31

Browse files
committed
refactor: move method into util module
1 parent c104ecf commit d241b31

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/json_as_db/Database.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,16 @@
44
import shortuuid
55

66
from datetime import datetime
7-
from typing import Any, Union, List, Callable, Tuple
8-
7+
from typing import Any, Union, List, Callable
98

109
from ._constants import package_name
11-
from ._utils import override_dict
12-
from .Condition import Condition
10+
from ._utils import override_dict, from_maybe_list, return_maybe
1311

1412
__all__ = [
1513
'Database'
1614
]
1715

1816

19-
def _from_maybe_list(value: Union[Any, List[Any]]) -> Tuple[type, List[Any]]:
20-
return_type = type(value)
21-
if not isinstance(value, list):
22-
value = [value]
23-
return (return_type, value)
24-
25-
26-
def _return_maybe(_type: type, _values: List[Any]) -> Union[Any, List[Any]]:
27-
if _type is list:
28-
return _values
29-
else:
30-
return _values[0]
31-
32-
3317
class Database(dict):
3418
__data__ = 'data'
3519
__version__ = '1.0.0'
@@ -116,9 +100,9 @@ def _update_timestamp(self) -> None:
116100
})
117101

118102
def get(self, key: Union[str, List[str]], default=None) -> Union[Any, List[Any]]:
119-
_type, _keys = _from_maybe_list(key)
103+
_type, _keys = from_maybe_list(key)
120104
values = [self.data.get(k, default) for k in _keys]
121-
return _return_maybe(_type, values)
105+
return return_maybe(_type, values)
122106

123107
def update(self, mapping: Union[dict, tuple] = (), **kwargs) -> None:
124108
"""Note that this method overrides database itself.
@@ -142,8 +126,8 @@ def modify(
142126
Returns:
143127
Any | List[Any]: Modified value(s)
144128
"""
145-
type_id, ids = _from_maybe_list(id)
146-
type_value, values = _from_maybe_list(value)
129+
type_id, ids = from_maybe_list(id)
130+
type_value, values = from_maybe_list(value)
147131
if len(ids) != len(values):
148132
raise ValueError(
149133
'Can not match ids and values. please check type and length of them')
@@ -153,7 +137,7 @@ def modify(
153137
target[_id] = _value
154138
self.data.update(target)
155139
self._update_timestamp()
156-
return _return_maybe(type_id, values)
140+
return return_maybe(type_id, values)
157141

158142
def add(self, item: Union[Any, List[Any]]) -> Union[str, List[str]]:
159143
"""
@@ -163,7 +147,7 @@ def add(self, item: Union[Any, List[Any]]) -> Union[str, List[str]]:
163147
Returns:
164148
Union[str, List[str]]: Automatically generated ID of added item
165149
"""
166-
_type, _items = _from_maybe_list(item)
150+
_type, _items = from_maybe_list(item)
167151

168152
ids = []
169153
for i in _items:
@@ -172,7 +156,7 @@ def add(self, item: Union[Any, List[Any]]) -> Union[str, List[str]]:
172156
ids.append(uid)
173157

174158
self._update_timestamp()
175-
return _return_maybe(_type, ids)
159+
return return_maybe(_type, ids)
176160

177161
def remove(self, key: Union[str, List[str]]) -> Union[Any, List[Any]]:
178162
"""
@@ -182,10 +166,10 @@ def remove(self, key: Union[str, List[str]]) -> Union[Any, List[Any]]:
182166
Returns:
183167
Union[Any, List[Any]]: removed items
184168
"""
185-
_type, _keys = _from_maybe_list(key)
169+
_type, _keys = from_maybe_list(key)
186170
popped = [self.data.pop(key) for key in _keys]
187171
self._update_timestamp()
188-
return _return_maybe(_type, popped)
172+
return return_maybe(_type, popped)
189173

190174
def all(self) -> List[Any]:
191175
"""Provide all items in database.
@@ -228,10 +212,10 @@ def has(self, key: Union[str, List[str]]) -> Union[bool, List[bool]]:
228212
Returns:
229213
Union[bool, List[bool]]: boolean or array of boolean.
230214
"""
231-
_type, _keys = _from_maybe_list(key)
215+
_type, _keys = from_maybe_list(key)
232216
key_set = set(self.data.keys())
233217
values = [k in key_set for k in _keys]
234-
return _return_maybe(_type, values)
218+
return return_maybe(_type, values)
235219

236220
def count(self) -> int:
237221
"""

src/json_as_db/_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import copy
22

3+
from typing import Any, Union, List, Tuple
4+
35

46
def override_dict(__dict: dict, __target: dict) -> dict:
57
"""override base object if value is unset
@@ -18,3 +20,17 @@ def override_dict(__dict: dict, __target: dict) -> dict:
1820
new_dict = copy.deepcopy(__dict)
1921
new_dict.update(new_target)
2022
return new_dict
23+
24+
25+
def from_maybe_list(value: Union[Any, List[Any]]) -> Tuple[type, List[Any]]:
26+
return_type = type(value)
27+
if not isinstance(value, list):
28+
value = [value]
29+
return (return_type, value)
30+
31+
32+
def return_maybe(_type: type, _values: List[Any]) -> Union[Any, List[Any]]:
33+
if _type is list:
34+
return _values
35+
else:
36+
return _values[0]

0 commit comments

Comments
 (0)