-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbtools.py
More file actions
23 lines (16 loc) · 861 Bytes
/
dbtools.py
File metadata and controls
23 lines (16 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import inspect
def orm(cursor, dto_type):
# the following line retrieve the argument names of the constructor
# returned are a tuple of 7. args retrieve the args of the constructor.
args = inspect.getfullargspec(dto_type.__init__).args
# the first argument of the constructor will be 'self', it does not correspond
# to any database field, so we can ignore it.
args = args[1:]
# gets the names of the columns returned in the cursor
col_names = [column[0] for column in cursor.description]
# map them into the position of the corresponding constructor argument
col_mapping = [col_names.index(arg) for arg in args]
return [row_map(row, col_mapping, dto_type) for row in cursor.fetchall()]
def row_map(row, col_mapping, dto_type):
ctor_args = [row[idx] for idx in col_mapping]
return dto_type(*ctor_args)