From 58b9c87a08d0ee0ea99d42d9f5b4edff017ce864 Mon Sep 17 00:00:00 2001 From: Jedy Matt Tabasco Date: Fri, 27 Aug 2021 07:11:18 +0800 Subject: [PATCH] Refactor _future.seeder.Seeder, added word for TODO description, removed util --- TODO.md | 2 +- sqlalchemyseed/_future/seeder.py | 46 +++++++++++++++++++++++++------- sqlalchemyseed/util.py | 2 -- 3 files changed, 38 insertions(+), 12 deletions(-) delete mode 100644 sqlalchemyseed/util.py diff --git a/TODO.md b/TODO.md index b70c259..b2e35f5 100644 --- a/TODO.md +++ b/TODO.md @@ -7,7 +7,7 @@ - [ ] Customize prefix in seeder (default=`!`) - [x] Customize prefix in validator (default=`!`) - [ ] relationship entity no longer required `model` key since the program will search it for you, but can also be - overridden by providing a model data instead as it saves time + overridden by providing a model data instead as it saves performance time # In Progress diff --git a/sqlalchemyseed/_future/seeder.py b/sqlalchemyseed/_future/seeder.py index 0676667..0ad5a43 100644 --- a/sqlalchemyseed/_future/seeder.py +++ b/sqlalchemyseed/_future/seeder.py @@ -1,3 +1,27 @@ +""" +MIT License + +Copyright (c) 2021 Jedy Matt Tabasco + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + from typing import NamedTuple import sqlalchemy @@ -118,17 +142,21 @@ def _seed(self, entity, parent: Entity = None): kwargs = entity[self.__data_key] - if isinstance(kwargs, dict): - # instantiate object - instance = self._setup_instance(class_, kwargs, parent) - for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix): - self._pre_seed(entity=value, parent=Entity(instance, attr_name)) - - else: # source_data is list + # kwargs is list + if isinstance(kwargs, list): for kwargs_ in kwargs: instance = self._setup_instance(class_, kwargs_, parent) - for attr_name, value in iter_ref_attr(kwargs_, self.ref_prefix): - self._pre_seed(value, parent=Entity(instance, attr_name)) + self._seed_children(instance, kwargs_) + return + + # kwargs is dict + # instantiate object + instance = self._setup_instance(class_, kwargs, parent) + self._seed_children(instance, kwargs) + + def _seed_children(self, instance, kwargs): + for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix): + self._pre_seed(entity=value, parent=Entity(instance, attr_name)) def _setup_instance(self, class_, kwargs: dict, parent: Entity): instance = class_(**filter_kwargs(kwargs, class_, self.ref_prefix)) diff --git a/sqlalchemyseed/util.py b/sqlalchemyseed/util.py deleted file mode 100644 index 79bff60..0000000 --- a/sqlalchemyseed/util.py +++ /dev/null @@ -1,2 +0,0 @@ -def get_class_path(class_): - return "{}.{}".format(class_.__module__, class_.__name__)