11from collections .abc import Iterable , Mapping , MutableMapping , Sequence
22from pathlib import Path
3+ from typing import Any , Callable
4+
5+ import sqlalchemy
36from mimesis import Generic
47from mimesis .locales import Locale
5- import sqlalchemy
6- from typing import Any , Callable
78
8- from datafaker .base import FileUploader , ColumnPresence
9+ from datafaker .base import ColumnPresence , FileUploader
910from datafaker .make import FunctionCall , TableGeneratorInfo
10-
1111from datafaker .providers import (
1212 BytesProvider ,
1313 ColumnValueProvider ,
2020)
2121from datafaker .utils import get_vocabulary_table_names , import_file
2222
23- def make_generic ():
23+
24+ def make_generic () -> Generic :
2425 g = Generic (locale = Locale .EN_GB )
2526 g .add_providers (
2627 BytesProvider ,
@@ -38,7 +39,7 @@ def make_generic():
3839generic = make_generic ()
3940
4041
41- def reset_generic ():
42+ def reset_generic () -> None :
4243 """
4344 Reset all the generators.
4445
@@ -63,10 +64,7 @@ def _eval_structure(config: Any, context: Mapping) -> Any:
6364 except NameError as exc :
6465 raise exc
6566 if isinstance (config , Mapping ):
66- return {
67- k : _eval_structure (v , context )
68- for k , v in config .items ()
69- }
67+ return {k : _eval_structure (v , context ) for k , v in config .items ()}
7068 if isinstance (config , Sequence ):
7169 return [_eval_structure (v , context ) for v in config ]
7270 return config
@@ -96,10 +94,7 @@ def _get_object(class_name: str, context: Mapping) -> Any:
9694
9795
9896def _call_from_context (
99- callable_name : str ,
100- args : list [Any ],
101- kwargs : dict [str , Any ],
102- context : Mapping
97+ callable_name : str , args : list [Any ], kwargs : dict [str , Any ], context : Mapping
10398) -> Any :
10499 """
105100 Call a callable from the classes (or functions) in the context.
@@ -111,14 +106,8 @@ def _call_from_context(
111106 cls = _get_object (callable_name , context )
112107 if not isinstance (cls , Callable ):
113108 return None
114- arg_objs = [
115- _eval_structure (arg , context )
116- for arg in args
117- ]
118- kwarg_objs = {
119- k : _eval_structure (v , context )
120- for k , v in kwargs .items ()
121- }
109+ arg_objs = [_eval_structure (arg , context ) for arg in args ]
110+ kwarg_objs = {k : _eval_structure (v , context ) for k , v in kwargs .items ()}
122111 return cls (* arg_objs , ** kwarg_objs )
123112
124113
@@ -191,7 +180,6 @@ def _get_symbols_instantiation(symbols: dict[str, Any], objs: dict[str, Any]) ->
191180
192181
193182class TableGenerator :
194-
195183 def __init__ (
196184 self ,
197185 dst_db_conn : sqlalchemy .Connection ,
@@ -217,10 +205,9 @@ def __init__(
217205 for constraint in table_data .unique_constraints :
218206 expr = sqlalchemy .select (constraint .columns )
219207 query_result = dst_db_conn .execute (expr ).fetchall ()
220- self .existing_constraint_hashes [constraint .name ] = set ([
221- hash (tuple (result ))
222- for result in query_result
223- ])
208+ self .existing_constraint_hashes [constraint .name ] = set (
209+ [hash (tuple (result )) for result in query_result ]
210+ )
224211
225212 @property
226213 def num_rows_per_pass (self ):
@@ -242,13 +229,17 @@ def __call__(self, db_conn: sqlalchemy.Connection):
242229 columns_to_generate = set (self .table_data .nonnull_columns )
243230 # Which missingness patterns do we want?
244231 for choice in self .table_data .column_choices :
245- cols = _call_from_context (choice .function_name , choice .args , choice .kwargs , self .context )
232+ cols = _call_from_context (
233+ choice .function_name , choice .args , choice .kwargs , self .context
234+ )
246235 columns_to_generate .update (cols )
247236
248237 max_tries = self .max_unique_constraint_tries
249238 while columns_to_generate :
250239 if max_tries == 0 :
251- raise RuntimeError (f"Failed to satisfy unique constraints for table { self .table_data .table_name } after { self .max_unique_constraint_tries } attempts." )
240+ raise RuntimeError (
241+ f"Failed to satisfy unique constraints for table { self .table_data .table_name } after { self .max_unique_constraint_tries } attempts."
242+ )
252243 if max_tries is not None :
253244 max_tries -= 1
254245 for row_gen in self .table_data .row_gens :
@@ -264,15 +255,11 @@ def __call__(self, db_conn: sqlalchemy.Connection):
264255 result [variable_name ] = values [index ]
265256 columns_to_generate = set ()
266257 for constraint in self .table_data .unique_constraints :
267- cf_hash = hash (tuple (
268- result [col .name ] for col in constraint .columns
269- ))
258+ cf_hash = hash (tuple (result [col .name ] for col in constraint .columns ))
270259 if cf_hash in self .existing_constraint_hashes [constraint .name ]:
271260 columns_to_generate .update (c .name for c in constraint .columns )
272261 for constraint in self .table_data .unique_constraints :
273- cf_hash = hash (tuple (
274- result [col .name ] for col in constraint .columns
275- ))
262+ cf_hash = hash (tuple (result [col .name ] for col in constraint .columns ))
276263 self .existing_constraint_hashes [constraint .name ].add (cf_hash )
277264 return result
278265
0 commit comments