-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_object.py
564 lines (418 loc) · 19.3 KB
/
math_object.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
"""
math_object Provides helper functions for pulling the LaTeX show_template_manager from the equations database
update method: https://stackoverflow.com/questions/34774409/build-a-dynamic-update-query-in-psycopg2
https://www.psycopg.org/docs/sql.html#
Type Hinting Tuple: https://stackoverflow.com/questions/47533787/typehinting-tuples-in-python
Update borrowed from: https://stackoverflow.com/questions/34774409/build-a-dynamic-update-query-in-psycopg2
"""
__author__ = "William DeShazer"
__version__ = "0.1.0"
__license__ = "MIT"
from typing import Tuple
from collections import defaultdict
from warnings import warn
from psycopg2.sql import SQL, Identifier, Placeholder, Composed
from psycopg2 import connect, OperationalError, Binary
from psycopg2.extras import NamedTupleCursor
from latex_template import compile_pattern, template
from config import config
class RecordIDTypeError(UserWarning):
"""UserWarning for EquationGroup"""
class ImageWithoutTemplateIDError(UserWarning):
"""UserWarning for EquationGroup"""
class NoRecordIDError(UserWarning):
"""UserWarning for EquationGroup"""
class MathObject:
"""Base class for Equations, Variables, and Units"""
def __init__(self, table: str, parent_table: str, verbose: bool = True):
"""Constructor for MathObject"""
self.table = table
self.parent_table = parent_table # For equations it is eqn_group. For variables it is equations
self.records: dict = self.all_records(verbose=verbose)
self.last_inserted = None
self.id_name = self.table + "_id"
self.records_for_parent = None
def all_records(self, as_columns: bool = True, verbose: bool = False):
"""Returns all records for math_object"""
# sql = 'SELECT * FROM {aTable}'.format(aTable=self.table)
sql = "SELECT * FROM {tbl}"
query = SQL(sql).format(tbl=Identifier(self.table))
db_params = config()
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Extracting records from Table: {aTable}'.format(aTable=self.table))
try:
cur.execute(query)
except OperationalError as error:
print(error)
records = cur.fetchall()
cur.close()
conn.close()
if as_columns is True:
records = self.as_columns(records)
return records
def insert(self, parent_id: int = None, name: str = None,
data=None, latex: str = None, notes: str = None,
image: bytes = None, template_id: int = None, dimensions: int = 1,
insertion_order: int = None, created_by: str = None, unit_id: int = 1,
verbose: bool = None):
"""Insert New Record Into math_object"""
if data is None:
data = {}
db_params = config()
next_id: int = self.record_count_total(verbose=verbose) + 1
if name is None:
name = "{aTable} {ID:d}".format(ID=next_id, aTable=self.table)
if created_by is None:
created_by = db_params['user']
if latex is None:
latex = 'a^2 + b^2 = c^2'
the_template = template(version=template_id, verbose=verbose)
a_template = the_template.data
template_id = the_template.id
if image is None:
# If a_template is None the most recent show_template_manager is used
image = Binary(compile_pattern(pattern=latex, aTemplate=a_template, verbose=verbose))
data.update({
'name': name,
'latex': latex,
'image': image,
'notes': notes,
'dimensions': dimensions,
'unit_id': unit_id,
'template_id': template_id,
'created_by': created_by
})
sql = 'INSERT INTO {table} ({fields}) VALUES ({values}) RETURNING *'
keys = data.keys()
query = SQL(sql).format(table=Identifier(self.table),
fields=SQL(', ').join(map(Identifier, keys)),
values=SQL(', ').join(map(Placeholder, keys)))
conn = connect(**db_params)
if verbose:
print(query.as_string(conn))
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Adding new record to Table: {aTable}'.format(aTable=self.table))
try:
cur.execute(query, data)
except OperationalError as error:
print(error)
new_records = cur.fetchall()
conn.commit()
cur.close()
conn.close()
if parent_id is not None:
for record in new_records:
self.associate_parent(parent_id=parent_id, child_id=record.id, insertion_order=insertion_order)
self.last_inserted = self.as_columns(new_records)
self.append(new_records)
def update(self, an_id: id = None, where_key: str = None, name: str = None,
data=None, latex: str = None, notes: str = None, unit_id: int = None,
image: bytes = None, template_id: int = None, dimensions: int = None,
modified_by: str = None, created_by: str = None,
verbose: bool = None):
"""Insert New Record Into math_object"""
if where_key is None:
where_key = self.id_name
if an_id is None:
warn("No Record ID Specified", NoRecordIDError)
else:
if data is None:
data = {}
db_params = config()
data.update(self._add_field('name', name))
data.update(self._add_field('notes', notes))
data.update(self._process_latex(latex, image, template_id, verbose))
data.update(self._add_field('dimensions', dimensions))
data.update(self._add_field('unit_id', unit_id))
data.update(self._add_field('created_by', created_by))
# If there is no data, then skip. Of course one could still change modified by:
if len(data) > 0 or modified_by is not None:
# Always require a modified by and because one can change data without specifying a modifer,
# this is necessary. We don't check it before the previous if, because we don't want to create
# a modified_by if not data was set and no modified_by was set.
if modified_by is None:
modified_by = db_params['user']
data.update(modified_by=modified_by)
fields = data.keys()
sql = "UPDATE {table} SET {fields} WHERE {pkey} = {a_value} RETURNING *"
query = SQL(sql).format(table=Identifier(self.table),
fields=SQL(', ').join(
Composed([Identifier(k), SQL(' = '), Placeholder(k)]) for k in fields
),
pkey=Identifier(where_key),
a_value=Placeholder('where_key')
)
data.update(where_key=an_id)
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print(query.as_string(conn))
print(cur.mogrify(query, data))
try:
cur.execute(query, data)
except OperationalError as error:
print(error)
new_record = cur.fetchall()
conn.commit()
cur.close()
conn.close()
self.last_inserted = self.as_columns(new_record)
self.records = self.all_records()
@staticmethod
def _process_latex(latex, image, template_id, verbose: bool = False):
"""Helper function to process latex, image and template_id flags"""
data = {}
if latex is not None or image is not None or template_id is not None:
only_template_changed = template_id is not None and latex is None and image is None
only_latex_changed = latex is not None and image is None # regardless of template_id state
# always need template_id when image is manually changed
acceptable_image_change = image is not None and template_id is not None
recompile = only_latex_changed or only_template_changed
if recompile:
the_template = template(version=template_id, verbose=verbose)
a_template = the_template.data
template_id = the_template.id
image = Binary(compile_pattern(pattern=latex, aTemplate=a_template, verbose=verbose))
data.update(image=image, template_id=template_id)
if only_latex_changed:
data.update(latex=latex)
elif acceptable_image_change:
# Note if template_id is not specified
data.update(image=image, template_id=template_id)
if latex is not None:
data.update(latex=latex)
else:
warn('User Input Images must also specify show_template_manager used', ImageWithoutTemplateIDError)
return data
@staticmethod
def _add_field(key: str = None, value=None):
data = {}
if value is not None:
data = {key: value}
return data
def append(self, new_records):
"""Append new records to existing records. Because the data is stored as a dictionary instead of
NamedTuples, it requires a helper function to append new data"""
res = self.records
for data in new_records:
# noinspection PyProtectedMember
record: dict = data._asdict()
for key, value in record.items():
res[key].append(value)
self.records = res
# Simple return statement. More sophisticated ones will have to be purpose built
def values_for_fields(self, where_key: str = None, where_values: Tuple[int, ...] = None,
name: bool = True, as_column: bool = True, verbose: bool = False, **kwargs):
"""where_key could be 'id', 'name', etc.
For any other field, you just set the fieldname to True"""
if where_key is None:
where_key = self.id_name
fields = [self.id_name]
if name is True:
fields.append('name')
for a_key in kwargs:
if kwargs[a_key] is True:
fields.append(a_key)
db_params = config()
conn = connect(**db_params)
if where_values is None:
sql = "SELECT {fields} FROM {table}"
query = SQL(sql).format(table=Identifier(self.table),
fields=SQL(', ').join(map(Identifier, fields)))
if verbose:
print(query.as_string(conn))
else:
sql = "SELECT {fields} from {table} where {pkey} IN %s"
query = SQL(sql).format(table=Identifier(self.table),
fields=SQL(', ').join(map(Identifier, fields)),
pkey=Identifier(where_key)
)
if verbose:
print(query.as_string(conn))
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Extracting records from Table: {aTable}'.format(aTable=self.table))
try:
cur.execute(query, where_values)
except OperationalError as error:
print(error)
records = cur.fetchall()
cur.close()
conn.close()
if as_column is True:
records = self.as_columns(records)
return records
def record_count_total(self, verbose: bool = False) -> int:
"""Get the total record count for {table}""".format(table=self.table)
sql = "SELECT COUNT(*) from {table}"
query = SQL(sql).format(table=Identifier(self.table))
db_params = config()
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Getting Count of Records in table: {table}'.format(table=self.table))
cur.execute(query) # self.table))
record_count = cur.fetchone()
cur.close()
conn.close()
return record_count[0]
def record_count_for_parent(self, parent_id: Tuple[int, ...] = None,
verbose: bool = False):
"""Return record count for Parent Table"""
sql = ''
if isinstance(parent_id, tuple):
sql = "SELECT COUNT(*) from {table} WHERE {parent_key} IN %s"
elif isinstance(parent_id, int):
sql = "SELECT COUNT(*) from {table} WHERE {parent_key}= %s"
else:
warn("Unrecognized type for parent_id", RecordIDTypeError)
query = SQL(sql).format(table=Identifier(self.join_table()),
parent_key=Identifier(self.parent_key())
)
db_params = config()
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Getting Count of Records in table: {table} for Group ID: {gid}'.format(table=self.join_table(),
gid=parent_id))
print(query.as_string(conn))
if isinstance(parent_id, int):
cur.mogrify(query, parent_id)
elif isinstance(parent_id, tuple):
cur.mogrify(query, (parent_id, ))
try:
cur.execute(query, (parent_id,))
except TypeError:
cur.execute(query, parent_id) # self.table))
except OperationalError as error:
print(error)
record_count = cur.fetchone()
cur.close()
conn.close()
return record_count[0]
def associate_parent(self, parent_id: int = None, child_id: int = None,
insertion_order: int = None, inserted_by: str = None,
data: dict = None, verbose: bool = False):
"""Associate the parent and child tables using parent id. Insertion_order and inserted_by are optional"""
parent_key = self.parent_table + '_id'
self_key = self.table + '_id'
join_table = self.table + '_' + self.parent_table
db_params = config()
if data is None:
data = {}
if inserted_by is None:
inserted_by = db_params['user']
data.update(
{parent_key: parent_id, self_key: child_id},
insertion_order=insertion_order,
inserted_by=inserted_by
)
sql = 'INSERT INTO {table} ({fields}) VALUES ({values})'
keys = data.keys()
query = SQL(sql).format(table=Identifier(join_table),
fields=SQL(', ').join(map(Identifier, keys)),
values=SQL(', ').join(map(Placeholder, keys)))
conn = connect(**db_params)
if verbose:
print(query.as_string(conn))
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Adding new record to Table: {aTable}'.format(aTable=join_table))
try:
cur.execute(query, data)
except OperationalError as error:
print(error)
conn.commit()
cur.close()
conn.close()
def get_records_for_parent(self, parent_id: Tuple[int, ...] = None, as_columns: bool = True,
verbose: bool = False):
"""Get the {table} records for {parent}""".format(table=self.table, parent=self.parent_table)
db_params = config()
sql = "SELECT * FROM {child_table} c INNER JOIN {join_table} j " \
" USING({id_name}) WHERE j.{parent_id_name} = %s ORDER BY insertion_order;"
query = SQL(sql).format(
child_table=Identifier(self.table),
join_table=Identifier(self.join_table()),
id_name=Identifier(self.id_name),
parent_id_name=Identifier(self.parent_key())
)
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print(query.as_string(conn))
if isinstance(parent_id, int):
cur.mogrify(query, parent_id)
elif isinstance(parent_id, tuple):
cur.mogrify(query, (parent_id, ))
try:
cur.execute(query, (parent_id, ))
except TypeError:
cur.execute(query, parent_id)
except OperationalError as error:
print(error)
records = cur.fetchall()
cur.close()
conn.close()
if as_columns is True:
records = self.as_columns(records)
return records
def records_not_in_parent(self, parent_id: Tuple[int, ...], as_columns: bool = True, verbose: bool = False):
"""Get the {table} records for {parent}""".format(table=self.table, parent=self.parent_table)
db_params = config()
sql = ''
sql = "SELECT * FROM {child_table} WHERE {id_name} NOT IN" \
" (SELECT {id_name} FROM {join_table} WHERE {parent_id_name} = %s);"
# self_dict = dict(table='equation', join_table= lambda: 'equation_eqn_group',
# id_name='equation_id', parent_key = lambda : 'eqn_group_id')
# MyTuple = namedtuple('MyTuple', self_dict)
# self = MyTuple(**self_dict)
query = SQL(sql).format(
child_table=Identifier(self.table),
join_table=Identifier(self.join_table()),
id_name=Identifier(self.id_name),
parent_id_name=Identifier(self.parent_key())
)
conn = connect(**db_params)
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print(query.as_string(conn))
if isinstance(parent_id, int):
cur.mogrify(query, (parent_id, ))
elif isinstance(parent_id, tuple):
cur.mogrify(query, parent_id)
try:
cur.execute(query, (parent_id, ))
except TypeError:
cur.execute(query, parent_id)
except OperationalError as error:
print(error)
records = cur.fetchall()
if as_columns is True:
records = self.as_columns(records)
cur.close()
conn.close()
return records
def parent_key(self):
"""Parent ID String to be used as a Key in the table"""
return self.parent_table + '_id'
def self_key(self):
"""Self ID String to be used as a Key in the table"""
return self.table + '_id'
def join_table(self):
"""Join table which is a concatenation of the table and the parent table"""
return self.table + '_' + self.parent_table
# NamedTuple uses underscores to prevent name collision _make, _replace, _asdict, _fields
# noinspection PyProtectedMember
@staticmethod
def as_columns(records):
"""Helper function to turn NameTuple records into column-like dictionaries"""
res = defaultdict(list)
for data in records:
record = data._asdict()
for key, value in record.items():
res[key].append(value)
return res