33"""
44
55
6+ from functools import lru_cache
7+ import importlib
8+ from typing import Iterable
9+
610from sqlalchemy import inspect
11+ from sqlalchemyseed import errors
712
813
914def iter_ref_kwargs (kwargs : dict , ref_prefix : str ):
10- """Iterate kwargs with name prefix or references"""
15+ """
16+ Iterate kwargs with name prefix or references
17+ """
1118 for attr_name , value in kwargs .items ():
1219 if attr_name .startswith (ref_prefix ):
1320 # removed prefix
1421 yield attr_name [len (ref_prefix ):], value
1522
1623
24+ def iter_kwargs_with_prefix (kwargs : dict , prefix : str ):
25+ """
26+ Iterate kwargs(dict) that has the specified prefix.
27+ """
28+ for key , value in kwargs .items ():
29+ if str (key ).startswith (prefix ):
30+ yield key , value
31+
32+
33+ def iterate_json (json : dict , key_prefix : str ):
34+ """
35+ Iterate through json that has matching key prefix
36+ """
37+ for key , value in json .items ():
38+ has_prefix = str (key ).startswith (key_prefix )
39+
40+ if has_prefix :
41+ # removed prefix
42+ yield key [len (key_prefix ):], value
43+
44+
45+ def iterate_json_no_prefix (json : dict , key_prefix : str ):
46+ """
47+ Iterate through json that has no matching key prefix
48+ """
49+ for key , value in json .items ():
50+ has_prefix = str (key ).startswith (key_prefix )
51+ if not has_prefix :
52+ yield key , value
53+
54+
1755def iter_non_ref_kwargs (kwargs : dict , ref_prefix : str ):
1856 """Iterate kwargs, skipping item with name prefix or references"""
1957 for attr_name , value in kwargs .items ():
@@ -33,22 +71,44 @@ def is_supported_class(class_):
3371def generate_repr (instance : object ) -> str :
3472 """
3573 Generate repr of object instance
36-
37- Example:
38- ```
39- class Person(Base):
40- ...
41- def __repr__(self):
42- return generate_repr(self)
43- ```
44-
45- Output format:
46- ```
47- "<Person(id='1',name='John Doe')>"
48- ```
4974 """
5075 class_name = instance .__class__ .__name__
5176 insp = inspect (instance )
5277 attributes = {column .key : column .value for column in insp .attrs }
5378 str_attributes = "," .join (f"{ k } ='{ v } '" for k , v in attributes .items ())
5479 return f"<{ class_name } ({ str_attributes } )>"
80+
81+
82+ def find_item (json : Iterable , keys : list ):
83+ """
84+ Finds item of json from keys
85+ """
86+ return find_item (json [keys [0 ]], keys [1 :]) if keys else json
87+
88+
89+ # check if class is a sqlalchemy model
90+ def is_model (class_ ):
91+ """
92+ Check if class is a sqlalchemy model
93+ """
94+ insp = inspect (class_ , raiseerr = False )
95+ return insp is not None and insp .is_mapper
96+
97+
98+ # get sqlalchemy model class from path
99+ @lru_cache (maxsize = None )
100+ def get_model_class (path : str ):
101+ """
102+ Get sqlalchemy model class from path
103+ """
104+ try :
105+ module_name , class_name = path .rsplit ("." , 1 )
106+ module = importlib .import_module (module_name )
107+ except (ImportError , AttributeError ) as e :
108+ raise errors .InvalidModelPath (path = path , error = e )
109+
110+ class_ = getattr (module , class_name )
111+ if not is_model (class_ ):
112+ raise errors .UnsupportedClassError (path = path )
113+
114+ return class_
0 commit comments