Skip to content

Commit 21befa5

Browse files
committed
Added sql model change from ch12
1 parent 371b52d commit 21befa5

File tree

5 files changed

+30
-41
lines changed
  • Chapter12/ABQ_Data_Entry/abq_data_entry
  • Chapter13/ABQ_Data_Entry/abq_data_entry
  • Chapter14/ABQ_Data_Entry/abq_data_entry
  • Chapter15/ABQ_Data_Entry/abq_data_entry
  • Chapter16/ABQ_Data_Entry/abq_data_entry

5 files changed

+30
-41
lines changed

Chapter12/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,21 @@ def __init__(self, host, database, user, password):
8080
self.connection = pg.connect(host=host, database=database,
8181
user=user, password=password, cursor_factory=DictCursor)
8282

83-
techs = self.query("SELECT * FROM lab_techs ORDER BY name")
83+
techs = self.query("SELECT name FROM lab_techs ORDER BY name")
8484
labs = self.query("SELECT id FROM labs ORDER BY id")
8585
plots = self.query("SELECT DISTINCT plot FROM plots ORDER BY plot")
8686
self.fields['Technician']['values'] = [x['name'] for x in techs]
8787
self.fields['Lab']['values'] = [x['id'] for x in labs]
8888
self.fields['Plot']['values'] = [str(x['plot']) for x in plots]
8989

9090
def query(self, query, parameters=None):
91-
cursor = self.connection.cursor()
92-
try:
93-
cursor.execute(query, parameters)
94-
except (pg.Error) as e:
95-
self.connection.rollback()
96-
raise e
97-
else:
98-
self.connection.commit()
91+
with self.connection:
92+
with self.connection.cursor() as cursor:
93+
cursor.execute(query, parameters)
9994
# cursor.description is None when
10095
# no rows are returned
101-
if cursor.description is not None:
102-
return cursor.fetchall()
96+
if cursor.description is not None:
97+
return cursor.fetchall()
10398

10499
def get_all_records(self, all_dates=False):
105100
"""Return all records.

Chapter13/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from xml.etree import ElementTree
99
import requests
1010
import paramiko
11-
import ftplib as ftp
1211

1312
import psycopg2 as pg
1413
from psycopg2.extras import DictCursor
@@ -85,26 +84,21 @@ def __init__(self, host, database, user, password):
8584
self.connection = pg.connect(host=host, database=database,
8685
user=user, password=password, cursor_factory=DictCursor)
8786

88-
techs = self.query("SELECT * FROM lab_techs ORDER BY name")
87+
techs = self.query("SELECT name FROM lab_techs ORDER BY name")
8988
labs = self.query("SELECT id FROM labs ORDER BY id")
9089
plots = self.query("SELECT DISTINCT plot FROM plots ORDER BY plot")
9190
self.fields['Technician']['values'] = [x['name'] for x in techs]
9291
self.fields['Lab']['values'] = [x['id'] for x in labs]
9392
self.fields['Plot']['values'] = [str(x['plot']) for x in plots]
9493

9594
def query(self, query, parameters=None):
96-
cursor = self.connection.cursor()
97-
try:
98-
cursor.execute(query, parameters)
99-
except (pg.Error) as e:
100-
self.connection.rollback()
101-
raise e
102-
else:
103-
self.connection.commit()
95+
with self.connection:
96+
with self.connection.cursor() as cursor:
97+
cursor.execute(query, parameters)
10498
# cursor.description is None when
10599
# no rows are returned
106-
if cursor.description is not None:
107-
return cursor.fetchall()
100+
if cursor.description is not None:
101+
return cursor.fetchall()
108102

109103
def get_all_records(self, all_dates=False):
110104
"""Return all records.
@@ -131,7 +125,7 @@ def get_record(self, rowkey):
131125
query,
132126
{"date": date, "time": time, "lab": lab, "plot": plot}
133127
)
134-
return result[0] if result else {}
128+
return result[0] if result else dict()
135129

136130
def save_record(self, record, rowkey):
137131
"""Save a record to the database
@@ -172,7 +166,7 @@ def get_lab_check(self, date, time, lab):
172166
'lab_id = %(lab)s AND date = %(date)s AND time = %(time)s')
173167
results = self.query(
174168
query, {'date': date, 'time': time, 'lab': lab})
175-
return results[0] if results else {}
169+
return results[0] if results else dict()
176170

177171
def get_current_seed_sample(self, lab, plot):
178172
"""Get the seed sample currently planted in the given lab and plot"""
@@ -274,7 +268,7 @@ def get_all_records(self):
274268

275269
with open(self.file, 'r', encoding='utf-8') as fh:
276270
# Casting to list is necessary for unit tests to work
277-
csvreader = csv.DictReader(list(fh.readlines()))
271+
csvreader = csv.DictReader(fh)
278272
missing_fields = set(self.fields.keys()) - set(csvreader.fieldnames)
279273
if len(missing_fields) > 0:
280274
fields_string = ', '.join(missing_fields)

Chapter14/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ def __init__(self, host, database, user, password):
9696
self.fields['Plot']['values'] = [str(x['plot']) for x in plots]
9797

9898
def query(self, query, parameters=None):
99-
with self.connection.cursor() as cursor:
100-
cursor.execute(query, parameters)
101-
self.connection.commit()
99+
with self.connection:
100+
with self.connection.cursor() as cursor:
101+
cursor.execute(query, parameters)
102102
# cursor.description is None when
103103
# no rows are returned
104-
if cursor.description is not None:
105-
return cursor.fetchall()
104+
if cursor.description is not None:
105+
return cursor.fetchall()
106106

107107
def get_all_records(self, all_dates=False):
108108
"""Return all records.

Chapter15/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ def __init__(self, host, database, user, password):
9696
self.fields['Plot']['values'] = [str(x['plot']) for x in plots]
9797

9898
def query(self, query, parameters=None):
99-
with self.connection.cursor() as cursor:
100-
cursor.execute(query, parameters)
101-
self.connection.commit()
99+
with self.connection:
100+
with self.connection.cursor() as cursor:
101+
cursor.execute(query, parameters)
102102
# cursor.description is None when
103103
# no rows are returned
104-
if cursor.description is not None:
105-
return cursor.fetchall()
104+
if cursor.description is not None:
105+
return cursor.fetchall()
106106

107107
def get_all_records(self, all_dates=False):
108108
"""Return all records.

Chapter16/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ def __init__(self, host, database, user, password):
9696
self.fields['Plot']['values'] = [str(x['plot']) for x in plots]
9797

9898
def query(self, query, parameters=None):
99-
with self.connection.cursor() as cursor:
100-
cursor.execute(query, parameters)
101-
self.connection.commit()
99+
with self.connection:
100+
with self.connection.cursor() as cursor:
101+
cursor.execute(query, parameters)
102102
# cursor.description is None when
103103
# no rows are returned
104-
if cursor.description is not None:
105-
return cursor.fetchall()
104+
if cursor.description is not None:
105+
return cursor.fetchall()
106106

107107
def get_all_records(self, all_dates=False):
108108
"""Return all records.

0 commit comments

Comments
 (0)