Skip to content

Commit e95e73c

Browse files
committed
Chapter 6 revisions
1 parent d0cf6f3 commit e95e73c

File tree

6 files changed

+146
-122
lines changed

6 files changed

+146
-122
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from abq_data_entry.application import Application
2+
import os
3+
4+
print(os.getcwd())
5+
6+
app = Application()
7+
app.mainloop()

Chapter06/ABQ_Data_Entry/abq_data_entry/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def _on_save(self, *_):
4646
"Cannot save, error in fields: {}"
4747
.format(', '.join(errors.keys()))
4848
)
49-
return False
49+
return
5050

5151
data = self.recordform.get()
5252
self.model.save_record(data)
5353
self.records_saved += 1
5454
self.status.set(
55-
"{} records saved this session".format(self.records_saved)
55+
f"{self.records_saved} records saved this session"
5656
)
5757
self.recordform.reset()

Chapter06/ABQ_Data_Entry/abq_data_entry/models.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,48 @@ class CSVModel:
1010

1111
fields = {
1212
"Date": {'req': True, 'type': FT.iso_date_string},
13-
"Time": {'req': True, 'type': FT.string_list,
14-
'values': ['8:00', '12:00', '16:00', '20:00']},
13+
"Time": {
14+
'req': True, 'type': FT.string_list,
15+
'values': ['8:00', '12:00', '16:00', '20:00']
16+
},
1517
"Technician": {'req': True, 'type': FT.string},
16-
"Lab": {'req': True, 'type': FT.short_string_list,
17-
'values': ['A', 'B', 'C']},
18-
"Plot": {'req': True, 'type': FT.string_list,
19-
'values': [str(x) for x in range(1, 21)]},
18+
"Lab": {
19+
'req': True, 'type': FT.short_string_list,
20+
'values': ['A', 'B', 'C']
21+
},
22+
"Plot": {
23+
'req': True, 'type': FT.string_list,
24+
'values': [str(x) for x in range(1, 21)]
25+
},
2026
"Seed Sample": {'req': True, 'type': FT.string},
21-
"Humidity": {'req': True, 'type': FT.decimal,
22-
'min': 0.5, 'max': 52.0, 'inc': .01},
23-
"Light": {'req': True, 'type': FT.decimal,
24-
'min': 0, 'max': 100.0, 'inc': .01},
25-
"Temperature": {'req': True, 'type': FT.decimal,
26-
'min': 4, 'max': 40, 'inc': .01},
27+
"Humidity": {
28+
'req': True, 'type': FT.decimal,
29+
'min': 0.5, 'max': 52.0, 'inc': .01
30+
},
31+
"Light": {
32+
'req': True, 'type': FT.decimal,
33+
'min': 0, 'max': 100.0, 'inc': .01
34+
},
35+
"Temperature": {
36+
'req': True, 'type': FT.decimal,
37+
'min': 4, 'max': 40, 'inc': .01
38+
},
2739
"Equipment Fault": {'req': False, 'type': FT.boolean},
2840
"Plants": {'req': True, 'type': FT.integer, 'min': 0, 'max': 20},
2941
"Blossoms": {'req': True, 'type': FT.integer, 'min': 0, 'max': 1000},
3042
"Fruit": {'req': True, 'type': FT.integer, 'min': 0, 'max': 1000},
31-
"Min Height": {'req': True, 'type': FT.decimal,
32-
'min': 0, 'max': 1000, 'inc': .01},
33-
"Max Height": {'req': True, 'type': FT.decimal,
34-
'min': 0, 'max': 1000, 'inc': .01},
35-
"Med Height": {'req': True, 'type': FT.decimal,
36-
'min': 0, 'max': 1000, 'inc': .01},
43+
"Min Height": {
44+
'req': True, 'type': FT.decimal,
45+
'min': 0, 'max': 1000, 'inc': .01
46+
},
47+
"Max Height": {
48+
'req': True, 'type': FT.decimal,
49+
'min': 0, 'max': 1000, 'inc': .01
50+
},
51+
"Med Height": {
52+
'req': True, 'type': FT.decimal,
53+
'min': 0, 'max': 1000, 'inc': .01
54+
},
3755
"Notes": {'req': False, 'type': FT.long_string}
3856
}
3957

@@ -60,7 +78,7 @@ def save_record(self, data):
6078
"""Save a dict of data to the CSV file"""
6179
newfile = not self.file.exists()
6280

63-
with open(self.file, 'a') as fh:
81+
with open(self.file, 'a', newline='') as fh:
6482
csvwriter = csv.DictWriter(fh, fieldnames=self.fields.keys())
6583
if newfile:
6684
csvwriter.writeheader()

Chapter06/ABQ_Data_Entry/abq_data_entry/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ def __init__(self, parent, model, *args, **kwargs):
5656
field_spec=fields['Time'],
5757
var=self._vars['Time'],
5858
).grid(row=0, column=1)
59+
w.LabelInput(
60+
r_info, "Technician",
61+
field_spec=fields['Technician'],
62+
var=self._vars['Technician'],
63+
).grid(row=0, column=2)
64+
# line 2
5965
w.LabelInput(
6066
r_info, "Lab",
6167
field_spec=fields['Lab'],
6268
var=self._vars['Lab'],
63-
).grid(row=0, column=2)
64-
# line 2
69+
).grid(row=1, column=0)
6570
w.LabelInput(
6671
r_info, "Plot",
6772
field_spec=fields['Plot'],
6873
var=self._vars['Plot'],
69-
).grid(row=1, column=0)
70-
w.LabelInput(
71-
r_info, "Technician",
72-
field_spec=fields['Technician'],
73-
var=self._vars['Technician'],
7474
).grid(row=1, column=1)
7575
w.LabelInput(
7676
r_info, "Seed Sample",

Chapter06/ABQ_Data_Entry/abq_data_entry/widgets.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _set_focus_update_var(self, event):
190190
if self.focus_update_var and not self.error.get():
191191
self.focus_update_var.set(value)
192192

193-
def _set_minimum(self, *args):
193+
def _set_minimum(self, *_):
194194
current = self.get()
195195
try:
196196
new_min = self.min_var.get()
@@ -203,7 +203,7 @@ def _set_minimum(self, *args):
203203
self.variable.set(current)
204204
self.trigger_focusout_validation()
205205

206-
def _set_maximum(self, *args):
206+
def _set_maximum(self, *_):
207207
current = self.get()
208208
try:
209209
new_max = self.max_var.get()
@@ -219,13 +219,13 @@ def _set_maximum(self, *args):
219219
def _key_validate(
220220
self, char, index, current, proposed, action, **kwargs
221221
):
222+
if action == '0':
223+
return True
222224
valid = True
223225
min_val = self.cget('from')
224226
max_val = self.cget('to')
225227
no_negative = min_val >= 0
226228
no_decimal = self.precision >= 0
227-
if action == '0':
228-
return True
229229

230230
# First, filter out obviously invalid keystrokes
231231
if any([
@@ -260,16 +260,17 @@ def _focusout_validate(self, **kwargs):
260260
max_val = self.cget('to')
261261

262262
try:
263-
value = Decimal(value)
263+
d_value = Decimal(value)
264264
except InvalidOperation:
265-
self.error.set('Invalid number string: {}'.format(value))
265+
self.error.set(f'Invalid number string: {value}')
266266
return False
267267

268-
if value < min_val:
269-
self.error.set('Value is too low (min {})'.format(min_val))
268+
if d_value < min_val:
269+
self.error.set(f'Value is too low (min {min_val})')
270+
valid = False
271+
if d_value > max_val:
272+
self.error.set(f'Value is too high (max {max_val})')
270273
valid = False
271-
if value > max_val:
272-
self.error.set('Value is too high (max {})'.format(max_val))
273274

274275
return valid
275276

@@ -279,36 +280,23 @@ class BoundText(tk.Text):
279280
def __init__(self, *args, textvariable=None, **kwargs):
280281
super().__init__(*args, **kwargs)
281282
self._variable = textvariable
282-
self._modifying = False
283283
if self._variable:
284284
# insert any default value
285285
self.insert('1.0', self._variable.get())
286286
self._variable.trace_add('write', self._set_content)
287287
self.bind('<<Modified>>', self._set_var)
288288

289-
def _clear_modified_flag(self):
290-
# This also triggers a '<<Modified>>' Event
291-
self.tk.call(self._w, 'edit', 'modified', 0)
292-
293289
def _set_var(self, *_):
294290
"""Set the variable to the text contents"""
295-
if self._modifying:
296-
return
297-
self._modifying = True
298-
# remove trailing newline from content
299-
content = self.get('1.0', tk.END)[:-1]
300-
self._variable.set(content)
301-
self._clear_modified_flag()
302-
self._modifying = False
291+
if self.edit_modified():
292+
content = self.get('1.0', 'end-1chars')
293+
self._variable.set(content)
294+
self.edit_modified(False)
303295

304296
def _set_content(self, *_):
305297
"""Set the text contents to the variable"""
306-
if self._modifying:
307-
return
308-
self._modifying = True
309298
self.delete('1.0', tk.END)
310299
self.insert('1.0', self._variable.get())
311-
self._modifying = False
312300

313301

314302
###########################

0 commit comments

Comments
 (0)