Skip to content

Commit 58b9c87

Browse files
committed
Refactor _future.seeder.Seeder, added word for TODO description, removed util
1 parent 4d069f1 commit 58b9c87

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- [ ] Customize prefix in seeder (default=`!`)
88
- [x] Customize prefix in validator (default=`!`)
99
- [ ] relationship entity no longer required `model` key since the program will search it for you, but can also be
10-
overridden by providing a model data instead as it saves time
10+
overridden by providing a model data instead as it saves performance time
1111

1212
# In Progress
1313

sqlalchemyseed/_future/seeder.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
"""
2+
MIT License
3+
4+
Copyright (c) 2021 Jedy Matt Tabasco
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
125
from typing import NamedTuple
226

327
import sqlalchemy
@@ -118,17 +142,21 @@ def _seed(self, entity, parent: Entity = None):
118142

119143
kwargs = entity[self.__data_key]
120144

121-
if isinstance(kwargs, dict):
122-
# instantiate object
123-
instance = self._setup_instance(class_, kwargs, parent)
124-
for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix):
125-
self._pre_seed(entity=value, parent=Entity(instance, attr_name))
126-
127-
else: # source_data is list
145+
# kwargs is list
146+
if isinstance(kwargs, list):
128147
for kwargs_ in kwargs:
129148
instance = self._setup_instance(class_, kwargs_, parent)
130-
for attr_name, value in iter_ref_attr(kwargs_, self.ref_prefix):
131-
self._pre_seed(value, parent=Entity(instance, attr_name))
149+
self._seed_children(instance, kwargs_)
150+
return
151+
152+
# kwargs is dict
153+
# instantiate object
154+
instance = self._setup_instance(class_, kwargs, parent)
155+
self._seed_children(instance, kwargs)
156+
157+
def _seed_children(self, instance, kwargs):
158+
for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix):
159+
self._pre_seed(entity=value, parent=Entity(instance, attr_name))
132160

133161
def _setup_instance(self, class_, kwargs: dict, parent: Entity):
134162
instance = class_(**filter_kwargs(kwargs, class_, self.ref_prefix))

sqlalchemyseed/util.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)