Skip to content

Commit d4ce978

Browse files
Development
- Reversed start max in Limit to be max start - Fixed annotations/typings in Data and Storage - Added exceptions and types to top level, and remove Limit (now in types.Limit)
1 parent b1d4eef commit d4ce978

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

record/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
__created__ = "2023-03-21"
1111

1212
# Limit imports
13-
__all__ = ['Data', 'Limit', 'Storage']
13+
__all__ = ['Data', 'exceptions', 'Storage', 'types']
1414

1515
# Local modules
16-
from . import data, exceptions, storage, types
17-
18-
# Re-Export just the classes/types
19-
Data = data.Data
20-
Limit = types.Limit
21-
RecordDuplicate = exceptions.RecordDuplicate
22-
Storage = storage.Storage
16+
from . import exceptions, types
17+
from .data import Data
18+
from .storage import Storage

record/data.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
Holds data associated with records as well as methods to store that data
55
"""
6+
from __future__ import annotations
67

78
__author__ = "Chris Nasr"
89
__copyright__ = "Ouroboros Coding Inc."
@@ -43,9 +44,6 @@ def __init__(self, storage: callable, value: dict = {}):
4344
# Init the list of fields that have changed
4445
self._changed = []
4546

46-
def __delattr__(self, __name: str) -> None:
47-
return super().__delattr__(__name)
48-
4947
def __contains__(self, key):
5048
"""Contains
5149
@@ -77,7 +75,7 @@ def __delattr__(self, name: str) -> None:
7775
except KeyError as e:
7876
raise AttributeError(*e.args)
7977

80-
def __delitem__(self, key):
78+
def __delitem__(self, key) -> None:
8179
"""Del(ete) Item
8280
8381
Overrides python magic method __delitem__ for deleting a key from a dict
@@ -150,7 +148,7 @@ def __setattr__(self, name: str, value: any) -> None:
150148
except KeyError as e:
151149
raise AttributeError(*e.args)
152150

153-
def __setitem__(self, key, value):
151+
def __setitem__(self, key, value) -> None:
154152
"""Set Item
155153
156154
Overrides python magic method __setitem__ for setting a key in a dict
@@ -181,7 +179,7 @@ def __repr__(self) -> str:
181179
str(self._value)
182180
)
183181

184-
def __str__(self):
182+
def __str__(self) -> str:
185183
"""String
186184
187185
Overrides python magic method __str__ to return a string representing
@@ -229,13 +227,13 @@ def changed(self, field: str) -> bool:
229227
# Call the instances changed and return that
230228
return self[field].changed()
231229

232-
def changes(self) -> list:
230+
def changes(self) -> list[str]:
233231
"""Changes
234232
235233
Returns the list of fields that have been changed
236234
237235
Returns:
238-
list
236+
str[]
239237
"""
240238
return list(self._changed.keys())
241239

@@ -253,15 +251,15 @@ def clean(self) -> None:
253251
self._value = self._storage.clean(self._value)
254252

255253
@property
256-
def errors(self) -> list:
254+
def errors(self) -> list[list[str]]:
257255
"""Errors
258256
259257
Read only property that returns the list of errors from the last failed
260258
valid call
261259
"""
262260
return copy(self._errors)
263261

264-
def field_get(self, field, default=None):
262+
def field_get(self, field: str, default = None):
265263
"""Field Get
266264
267265
Returns a specific field, if it's not found, returns the default
@@ -270,7 +268,7 @@ def field_get(self, field, default=None):
270268
271269
Arguments:
272270
field (str): The field to get
273-
default (mixed): Returned if the field doesn't exist
271+
default (any): Returned if the field doesn't exist
274272
275273
Returns:
276274
any
@@ -283,7 +281,7 @@ def field_get(self, field, default=None):
283281
# Return the field
284282
return self._value[field]
285283

286-
def field_remove(self, field: str):
284+
def field_remove(self, field: str) -> Data:
287285
"""Field Remove
288286
289287
Deletes a specific field from the record value
@@ -318,7 +316,7 @@ def field_remove(self, field: str):
318316
# Return self for chaining
319317
return self
320318

321-
def field_set(self, field, value):
319+
def field_set(self, field: str, value: any) -> Data:
322320
"""Field Set
323321
324322
Sets a specific field in a record

record/storage.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,30 @@ class Storage(Tree, abc.ABC):
3636
"""
3737

3838
@abc.abstractmethod
39-
def add(self, value: dict | list) -> str | list:
39+
def add(self, value: dict) -> str | list:
4040
"""Add
4141
42-
Adds one or more raw records to the storage system
42+
Adds one raw record to the storage system
4343
4444
Arguments:
45-
value (dict | dict[]): One or more dictionaries of fields to data
45+
value (dict): A dictionary of fields to data
4646
4747
Returns:
48-
The ID or IDs of the added records
48+
The ID of the added record
4949
"""
5050
pass
5151

5252
@abc.abstractmethod
53-
def create(self, value: dict | list = {}) -> Data | list:
53+
def create(self, value: dict = {}) -> Data | list:
5454
"""Create
5555
56-
Creates one or more new data objects associated with the Storage
57-
instance
56+
Creates a new data object associated with the Storage instance
5857
5958
Arguments:
60-
value (dict | list): The initial values to set for the record(s)
59+
value (dict): The initial values to set for the record
6160
6261
Returns:
63-
Data | Data[]
62+
Data
6463
"""
6564
pass
6665

@@ -94,13 +93,13 @@ def exists(self, _id: str):
9493

9594
@abc.abstractmethod
9695
def fetch(self,
97-
_id: str | list = None,
96+
_id: str | list[str] = None,
9897
filter: dict = None,
9998
limit: int | Limit = None,
100-
fields: list = None,
101-
raw: bool | list = False,
99+
fields: list[str] = None,
100+
raw: bool | list[str] = False,
102101
options: dict = None
103-
) -> list | dict | Data:
102+
) -> Data | list[Data] | dict | list[dict]:
104103
"""Fetch
105104
106105
Gets one, many, or all records from the storage system associated with
@@ -133,7 +132,7 @@ def install(self) -> bool:
133132
pass
134133

135134
@abc.abstractmethod
136-
def remove(self, _id: str | list = None, filter: dict = None):
135+
def remove(self, _id: str | list[str] = None, filter: dict = None):
137136
"""Remove
138137
139138
Removes one or more records from storage by ID or filter
@@ -159,7 +158,10 @@ def revision_add(cls, _id: str, changes: dict):
159158
pass
160159

161160
@classmethod
162-
def revision_generate(cls, old: dict | list, new: dict | list) -> dict | None:
161+
def revision_generate(cls,
162+
old: dict | list,
163+
new: dict | list
164+
) -> dict | None:
163165
"""Revision Generate
164166
165167
Generates the list of changes between two records

record/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# Python imports
1616
from collections import namedtuple
1717

18-
Limit = namedtuple('Limit', 'start max')
18+
Limit = namedtuple('Limit', 'max start')
1919
"""Limit
2020
21-
Used to denote the starting point, and max number, of records when fetching them
21+
Used to denote the max number, and starting point, of records when fetching them
2222
"""

0 commit comments

Comments
 (0)