-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.py
229 lines (173 loc) · 8.39 KB
/
unit.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
"""
Unit Provides helper functions for managing Unit and their associations
"""
__author__ = "William DeShazer"
__version__ = "0.1.0"
__license__ = "MIT"
from collections import namedtuple
from warnings import warn
from typing import Optional, List, Tuple
from pandas import DataFrame, read_sql, Series
from psycopg2.sql import SQL, Identifier, Placeholder, Composed
from psycopg2 import OperationalError
from psycopg2.extras import NamedTupleCursor
from latex_data import LatexData
from time_logging import TimeLogger
from db_utils import my_connect
class NoRecordIDError(UserWarning):
"""UserWarning for EquationGroup"""
def generic_pull_data(table_name: str = None, my_conn: Optional[dict] = None,
t_log: Optional[TimeLogger] = None, verbose: bool = False) -> DataFrame:
"""Multi-index Extract DataFrame DB"""
table_id_name: str = table_name + '_id'
query = SQL('SELECT * FROM {table}').format(table=Identifier(table_name))
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
conn = my_conn['conn']
data_df = read_sql(query, con=conn, index_col=table_id_name)
return data_df
def generic_record_count(data_df: DataFrame) -> int:
"""Method to get total number of eqn_groups"""
return len(data_df)
def generic_new_record_db(table_name: str = None, data_df: Optional[DataFrame] = None, name: str = None,
new_record=None, latex: LatexData = None, notes: str = None, created_by: str = None,
my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None,
verbose: bool = None) -> DataFrame:
"""Insert New Record Into math_object"""
if new_record is None:
new_record = {}
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
conn = my_conn['conn']
db_params = my_conn['db_params']
next_id: int = generic_record_count(data_df) + 1
if name is None:
name = "{aTable} {ID:d}".format(ID=next_id, aTable=table_name)
if created_by is None:
created_by = db_params['user']
if latex is None:
latex = LatexData()
new_record.update(name=name, notes=notes, latex=latex.latex, image=latex.image, compiled_at=latex.compiled_at,
template_id=latex.template_id, created_by=created_by)
query = SQL('INSERT INTO {table} ({fields}) VALUES ({values})'
).format(table=Identifier(table_name),
fields=SQL(', ').join(map(Identifier, new_record.keys())),
values=SQL(', ').join(map(Placeholder, new_record.keys())))
if verbose:
print(query.as_string(conn))
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if verbose:
print('Adding new record to Table: {aTable}'.format(aTable=table_name))
try:
cur.execute(query, new_record)
except OperationalError as error:
print(error)
conn.commit()
cur.close()
updated_df = \
generic_pull_data(table_name=table_name, my_conn=my_conn, t_log=t_log, verbose=verbose)
return updated_df
def add_field(key: str = None, value=None):
"""Convenience Wrapper to Beautify If value is none"""
data = {}
if value is not None:
data = {key: value}
return data
UnitRecord = \
namedtuple('UnitRecord',
['Index', 'name', 'latex', 'notes', 'image', 'template_id', 'image_is_dirty', 'created_at',
'created_by', 'modified_at', 'modified_by', 'compiled_at', 'type_name'])
class Unit:
"""Class for managing and interfacing with Postgres Table eqn_group"""
def __init__(self, my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None, verbose: bool = False):
"""Constructor for eqn_group"""
self.table_name = 'unit'
self.all_records_df: Optional[DataFrame] = None
self.all_records: Optional[List[Tuple]] = None
self.my_conn = my_conn
self.pull_data(my_conn=my_conn, t_log=t_log, verbose=verbose)
def id_name(self):
"""Convenience method to return id_name"""
return self.table_name + '_id'
def pull_data(self, my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None, verbose: bool = False):
"""Method to pull data from database"""
if my_conn is None:
my_conn = self.my_conn
self.all_records_df = generic_pull_data(table_name=self.table_name, my_conn=my_conn,
t_log=t_log, verbose=verbose)
self.all_records = list(self.all_records_df.itertuples())
def new_record(self, name: str = None, latex: LatexData = None,
new_record: dict = None, notes: str = None, created_by: str = None,
my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None, verbose: bool = False
):
"""Insert a new_record Into """
table_name = self.table_name
if my_conn is None:
my_conn = self.my_conn
self.all_records_df = \
generic_new_record_db(
table_name=table_name, name=name, latex=latex, notes=notes, new_record=new_record,
my_conn=my_conn, t_log=t_log, data_df=self.all_records_df, created_by=created_by,
verbose=verbose
)
self.all_records = list(self.all_records_df.itertuples())
def update(self, an_id: int = None, where_key: str = None, name: str = None,
data=None, latex: LatexData = None, notes: str = None,
modified_by: str = None, created_by: str = None,
my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None, verbose: bool = False
):
"""Insert New Record Into grouped_physics_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 = {}
if my_conn is None:
my_conn = self.my_conn
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
conn = my_conn['conn']
db_params = my_conn['db_params']
data.update(add_field('name', name))
data.update(add_field('notes', notes))
if latex is not None:
data.update(latex=latex.latex)
data.update(image=latex.image)
data.update(template_id=latex.template_id)
data.update(compiled_at=latex.compiled_at)
data.update(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}"
query = SQL(sql).format(
table=Identifier(self.table_name),
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)
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)
conn.commit()
cur.close()
self.pull_data(my_conn=my_conn, t_log=t_log, verbose=verbose)
def record(self, an_id: int = None):
"""Returns individual record based on id"""
record_series: List = self.all_records_df.loc[an_id].to_list()
record_series.insert(0, an_id)
record = UnitRecord(*record_series)
return record