From 31a5ad66bdf5fee8150867b12f9aaaa6e2618c39 Mon Sep 17 00:00:00 2001 From: taylor <tayjaybabee@gmail.com> Date: Wed, 15 Apr 2020 05:30:12 -0500 Subject: [PATCH 1/2] Refactor to match PEP 8 - Separated operators from variables - switched camel case to duck case - simplified if var == True expressions --- covid_info.py | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/covid_info.py b/covid_info.py index 69fbe90..58c94ee 100755 --- a/covid_info.py +++ b/covid_info.py @@ -69,47 +69,47 @@ def fetch_data(): # API URL url = 'https://covidtracking.com/api/v1/states/current.json' - readfromURL=False + should_read = False data = [] - - yamlimportfilename = 'data.yaml' - if Path(yamlimportfilename).exists(): - currentTime = datetime.now() - yamlimportfilehandle = open(yamlimportfilename,'rt') - data = yaml.safe_load(yamlimportfilehandle.read()) - yamlimportfilehandle.close() - # readfromURL=False + + yaml_dbname = 'data.yaml' + if Path(yaml_dbname).exists(): + current_time = datetime.now() + yaml_db_filepath = open(yaml_dbname, 'rt') + data = yaml.safe_load(yaml_db_filepath.read()) + yaml_db_filepath.close() + # should_read=False import_timestamp = latestTimeStamp(data) - age_seconds = (currentTime - import_timestamp).total_seconds() + age_seconds = (current_time - import_timestamp).total_seconds() if age_seconds > 21600: - readfromURL = True # file too old + should_read = True # file too old else: - readfromURL = False # file recent; don't hit up the web server this time + should_read = False # file recent; don't hit up the web server this time else: - readfromURL = True # file not found; retrieve data from web server and save it to the file - - if readfromURL == True: + should_read = True # file not found; retrieve data from web server and save it to the file + + if should_read: # Get web results or print connection exception try: res = requests.get(url) - readfromURL=True + should_read = True # Extract data from page data = res.json() except requests.exceptions.ConnectionError as e: print('Unable to reach data source. Please check your internet connection and try again!') - readfromURL=False + should_read = False exit(6) # Sort data by total tested positive s_data = sorted(data, key=lambda d: d["positive"], reverse=True) - if readfromURL == True: - yamlexportfilename = 'data.yaml' - yamlfilehandle = open(yamlexportfilename, 'wt') - yamlfilehandle.write(yaml.dump(s_data)) - yamlfilehandle.close() + if should_read: + db_out_filename = 'data.yaml' + db_out = open(db_out_filename, 'wt') + db_out.write(yaml.dump(s_data)) + db_out.close() # Set up accumulators for important stats delivered after sorted results total_infected = 0 @@ -178,9 +178,11 @@ if parser.gui: Qt.theme(app_theme) + menu_def = [['Application', 'Settings']] + layout = [ - [Qt.Menu(main_menu.definition, tearoff=False, pad=(200, 1))], - [Qt.MultilineOutput(autoscroll=True, key='output')], + [Qt.Menu(menu_def, tearoff=False, pad=(200, 1))], + [Qt.MultilineOutput(autoscroll=True, key='output', do_not_clear=False)], [Qt.Button('Refresh', enable_events=True, key='refresh_bttn'), Qt.Button('Inspect', enable_events=True, key='inspect_bttn', visible=False), Qt.CloseButton('Close', key='close_bttn')] -- GitLab From c20a13c25fcef935881ab09aeaccec9d884e7c1d Mon Sep 17 00:00:00 2001 From: taylor <tayjaybabee@gmail.com> Date: Wed, 15 Apr 2020 05:30:12 -0500 Subject: [PATCH 2/2] Refactor to match PEP 8 - Separated operators from variables - switched camel case to duck case - simplified if var == True expressions --- covid_info.py | 50 ++++++++++++++++++++------------------- lib/analyze_timestamps.py | 15 ++++++------ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/covid_info.py b/covid_info.py index 69fbe90..58c94ee 100755 --- a/covid_info.py +++ b/covid_info.py @@ -69,47 +69,47 @@ def fetch_data(): # API URL url = 'https://covidtracking.com/api/v1/states/current.json' - readfromURL=False + should_read = False data = [] - - yamlimportfilename = 'data.yaml' - if Path(yamlimportfilename).exists(): - currentTime = datetime.now() - yamlimportfilehandle = open(yamlimportfilename,'rt') - data = yaml.safe_load(yamlimportfilehandle.read()) - yamlimportfilehandle.close() - # readfromURL=False + + yaml_dbname = 'data.yaml' + if Path(yaml_dbname).exists(): + current_time = datetime.now() + yaml_db_filepath = open(yaml_dbname, 'rt') + data = yaml.safe_load(yaml_db_filepath.read()) + yaml_db_filepath.close() + # should_read=False import_timestamp = latestTimeStamp(data) - age_seconds = (currentTime - import_timestamp).total_seconds() + age_seconds = (current_time - import_timestamp).total_seconds() if age_seconds > 21600: - readfromURL = True # file too old + should_read = True # file too old else: - readfromURL = False # file recent; don't hit up the web server this time + should_read = False # file recent; don't hit up the web server this time else: - readfromURL = True # file not found; retrieve data from web server and save it to the file - - if readfromURL == True: + should_read = True # file not found; retrieve data from web server and save it to the file + + if should_read: # Get web results or print connection exception try: res = requests.get(url) - readfromURL=True + should_read = True # Extract data from page data = res.json() except requests.exceptions.ConnectionError as e: print('Unable to reach data source. Please check your internet connection and try again!') - readfromURL=False + should_read = False exit(6) # Sort data by total tested positive s_data = sorted(data, key=lambda d: d["positive"], reverse=True) - if readfromURL == True: - yamlexportfilename = 'data.yaml' - yamlfilehandle = open(yamlexportfilename, 'wt') - yamlfilehandle.write(yaml.dump(s_data)) - yamlfilehandle.close() + if should_read: + db_out_filename = 'data.yaml' + db_out = open(db_out_filename, 'wt') + db_out.write(yaml.dump(s_data)) + db_out.close() # Set up accumulators for important stats delivered after sorted results total_infected = 0 @@ -178,9 +178,11 @@ if parser.gui: Qt.theme(app_theme) + menu_def = [['Application', 'Settings']] + layout = [ - [Qt.Menu(main_menu.definition, tearoff=False, pad=(200, 1))], - [Qt.MultilineOutput(autoscroll=True, key='output')], + [Qt.Menu(menu_def, tearoff=False, pad=(200, 1))], + [Qt.MultilineOutput(autoscroll=True, key='output', do_not_clear=False)], [Qt.Button('Refresh', enable_events=True, key='refresh_bttn'), Qt.Button('Inspect', enable_events=True, key='inspect_bttn', visible=False), Qt.CloseButton('Close', key='close_bttn')] diff --git a/lib/analyze_timestamps.py b/lib/analyze_timestamps.py index 83037c3..1b0c655 100644 --- a/lib/analyze_timestamps.py +++ b/lib/analyze_timestamps.py @@ -1,11 +1,12 @@ from datetime import datetime -def latestTimeStamp(data): + +def latest_timestamp(data): time_string_format = '%Y-%m-%dT%H:%M:%SZ' - newesttimestamp = datetime.strptime('2020-01-01T00:00:00Z', time_string_format) + _new_time = datetime.strptime('2020-01-01T00:00:00Z', time_string_format) for state in data: - state_timestamp = datetime.strptime(state['dateChecked'], time_string_format) # example line from .yaml file: dateChecked: '2020-04-14T21:31:00Z' - if state_timestamp > newesttimestamp: - newesttimestamp = state_timestamp - return newesttimestamp - + state_timestamp = datetime.strptime(state['dateChecked'], + time_string_format) # example line from .yaml file: dateChecked: '2020-04-14T21:31:00Z' + if state_timestamp > _new_time: + _new_time = state_timestamp + return _new_time -- GitLab