-
Notifications
You must be signed in to change notification settings - Fork 1
/
labTracksQuery.py
314 lines (254 loc) · 9 KB
/
labTracksQuery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 23 13:45:57 2019
@author: svc_ccg
"""
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 23 13:04:34 2019
@author: svc_ccg
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 11:29:39 2018
@author: svc_ccg
"""
import decimal # bleh
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import url
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session
from contextlib import contextmanager
ADAPTER = "sqlserver"
DATASERVER = "AILABTRACKS\SQLEXPRESS"
USERNAME = "limstracks"
PASSWORD = "m0usetr@ck"
DATABASE = "LabTracks_AIBS"
TABLE = "Animals"
def row_to_dict(row):
"""Convert sqlalchemy table row, typically returned via a query on a table
model, into a typical python dictionary. Each key/value pair has the
following format:
<row dictionary>[<column name>] = <column value for row>.
Args:
row (sqlalchemy row instance): Sqlalchemy row object resulting from
a table model query. Its name will be based on the name of the table
model.
Returns:
ret_dict (dict or None): Result of converting 'row' to a dictonary.
Returns None if 'row' is 'Nonetype'.
"""
if row is None:
return {}
else:
return {
column.name: getattr(row, column.name)
for column in row.__table__.columns
}
def table_model_factory(engine, table_name, class_name, primary_index):
"""Dynamically automap a sql table as a `sqlalchemy.schema.Table`. This
doesn't need to exist, but it reduces a lot of boilerplate sometimes...
Parameters
----------
engine : `sqlalchemy.engine.Engine`
`engine` used to interface with database connection/behavior
table_name : string
name of the table to automap
class_name : string
name of the `Table` schema class instance generated
primary_index : string
name of the column that serves as the "primary key" in the database,
this is required by sqlalchemy regardless of whether or not it is
required by your specific sql dialect/version/etc
Returns
-------
`sqlalchemy.schema.Table`
schema class instance representing the target table in an ORM-like way
Notes
-----
- The reference to the `declarative_base` is maintained solely by its
relationship with the returned table, so if the returned schema dies,
the 'declarative_base' should also die...
- This isn't the most efficient way to map multiple tables from a single
`engine`, but is slightly helpful for automapping a single table and
the performance/control loss isn't that bad
"""
DatabaseBase = declarative_base()
DatabaseBase.metadata.reflect(engine, only=[table_name])
primary_key = getattr(DatabaseBase.metadata.tables[table_name].c,
primary_index)
class_dict = {
"__table__": DatabaseBase.metadata.tables[table_name],
"__mapper_args__": {
"primary_key": (primary_key, ) # must have a primary_key for mapping!
}
}
class_bases = (DatabaseBase, )
return type(class_name, class_bases, class_dict)
@contextmanager
def get_session(sessionmaker_factory):
"""Safe-ish way to handle a session within a context. Implements rollback
on failure, commit on success, and close on exit.
Parameters
----------
sessionmaker_factory : `sqlalchemy.orm.session.sessionmaker`
`sessionmaker` instance used to generate sessions
Yields
------
`sqlalchemy.orm.session.Session`
sqlalchemy session used to manage ORM operations
Notes
-----
- Technically this function yields an
`sqlalchemy.orm.scoping.scoped_session` instance but I think that it is
probably the same as returning a `Session` from our perspective
"""
session = scoped_session(sessionmaker_factory)
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
# light wrappers to cleanup session in between usage
def _query_session(session, TableModel, filters=(), conditions=(), options=()):
"""Helper query an sqlalchemy orm session. Not really that necessary...
Parameters
----------
session : `sqlalchemy.orm.session.Session`
sqlalchemy session used to manage ORM operations
TableModel : `sqlalchemy.schema.Table`
ORM of the table
filters : iterable of `sqlalchemy.sql.elements.ColumnElement`, optional
query filters
conditions : iterable of `sqlalchemy.sql.elements.ColumnElement`, optional
query "ORDER_BY" conditions
Returns
-------
`sqlalchemy.orm.query.Query`
`Query` instance representing the entries queried
Notes
-----
As of 04/07/2017, this doesn't attempt to call the `order_by`
method if `conditions` is empty. This fixed some
query compatability issues with `mouse_info.orm.update`
"""
results = session.query(TableModel) \
.filter(*filters)
if conditions:
results = results.order_by(*conditions)
if options:
results = results.options(*options)
return results
def query(
sessionmaker_factory,
TableModel,
filters=(),
conditions=(),
options=(),
adapter=row_to_dict
):
"""Create an sqlalchemy `Session` instance and query it in a somewhat
"safe-ish" way.
Parameters
----------
sessionmaker_factory : `sqlalchemy.orm.session.sessionmaker`
`sessionmaker` instance used to generate sessions
filters : iterable of `sqlalchemy.sql.elements.ColumnElement`, default=()
query filters
conditions : iterable of `sqlalchemy.sql.elements.ColumnElement`, default=()
query "ORDER_BY" conditions
options : iterable of `sqlalchemy.orm.Load` interface options, default=()
loader options supplied to the `options` method of the
`sqlalchemy.orm.query.Query` instance
adapter : callable, default=`mouse_info.sql.utils.row_to_dict`
function used to convert query results into dictionaries independent
of the original `sqlalchemy.orm.query.Query` instance
Returns
-------
list of dictionaries
with each dictionary representing a row returned by the query
Notes
-----
- returning dictionaries is costly on both space and time but this function
isn't built for speed or memory efficiency
- we could have returned the direct result of a `sqlalchemy.orm.query.Query`
but im dumb and flushing in `get_session` so...that won't work...>.>
"""
with get_session(sessionmaker_factory) as session:
q = _query_session(session, TableModel, filters, conditions, options)
return [
adapter(row)
for row in q.all()
]
def __fix_id(mouse_id):
"""Attempts to "fix" mouse_id if its supplied as a string.
Parameters
----------
mouse_id : integer or string
supposed id of the mouse
Returns
-------
int
mouse id in the expected datatype/format
"""
if isinstance(mouse_id, str):
return int( mouse_id.lower().lstrip('m') )
else:
return mouse_id
def __convert_entry_dict(entry_dict):
"""Converts certain objects to more "friendly" object? like
'decimal.Decimal' to 'float'. CAUTION: modifies the original dictionary
passed to it.
Notes
-----
- Modifies the dictionary supplied
"""
for key, value in entry_dict.items():
if isinstance(value, decimal.Decimal):
entry_dict[key] = float(value)
def get_labtracks_animals_entry(mouse_id):
"""Fetches entry from labtracks 'Animals' table at 'ID' 'mouse_id'. Will
attempt to "fix" the id if it gets a string value for 'mouse_id'.
Parameters
----------
mouse_id : integer or string
'ID' to use to query the 'Animals' table.
Returns
-------
dictionary
Dictionary representation of the column returned by the query. Is in
the format:
<column name>: <column value>
Notes
-----
- string mouse_id values are expected as an integer string or `M` prepended
integer string
"""
connection_url = url.URL(
drivername="mssql+pymssql",
username=USERNAME,
password=PASSWORD,
database=DATABASE,
host=DATASERVER
)
_engine = create_engine(connection_url, convert_unicode=True, echo=False)
LabTracks_AIBSSession = sessionmaker(bind=_engine) # awkward name lol
AnimalsModel = table_model_factory(
_engine, "Animals", "AnimalsModel", "ID"
)
filters = [
AnimalsModel.ID == __fix_id(mouse_id),
]
conditions = []
try:
entry_dict = query(
LabTracks_AIBSSession, AnimalsModel, filters, conditions
)[0] # there should only be one
except IndexError:
entry_dict = {}
__convert_entry_dict(entry_dict)
return entry_dict