From fdbcf92bd629735d7b3b4611a986cdd55f6e3fae Mon Sep 17 00:00:00 2001 From: Bdoner Date: Wed, 22 Feb 2023 10:50:36 +0100 Subject: [PATCH] satisfy flake8 linter --- scripts/server.py | 28 ++++++++++++++++++---------- scripts/utils/notifications.py | 15 +++++++++------ tests/test_apprise_notifications.py | 17 +++++++++-------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/scripts/server.py b/scripts/server.py index d29904a..ecbf5b2 100755 --- a/scripts/server.py +++ b/scripts/server.py @@ -55,10 +55,11 @@ with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f: try: model = str(str(str([i for i in this_run if i.startswith('MODEL')]).split('=')[1]).split('\\')[0]) sf_thresh = str(str(str([i for i in this_run if i.startswith('SF_THRESH')]).split('=')[1]).split('\\')[0]) - except Exception as e: + except Exception: model = "BirdNET_6K_GLOBAL_MODEL" sf_thresh = 0.03 + def loadModel(): global INPUT_LAYER_INDEX @@ -95,6 +96,7 @@ def loadModel(): return myinterpreter + def loadMetaModel(): global M_INTERPRETER @@ -115,15 +117,16 @@ def loadMetaModel(): print("loaded META model") + def predictFilter(lat, lon, week): global M_INTERPRETER # Does interpreter exist? try: - if M_INTERPRETER == None: + if M_INTERPRETER is None: loadMetaModel() - except Exception as e: + except Exception: loadMetaModel() # Prepare mdata as sample @@ -135,6 +138,7 @@ def predictFilter(lat, lon, week): return M_INTERPRETER.get_tensor(M_OUTPUT_LAYER_INDEX)[0] + def explore(lat, lon, week): # Make filter prediction @@ -151,15 +155,17 @@ def explore(lat, lon, week): return l_filter + def predictSpeciesList(lat, lon, week): l_filter = explore(lat, lon, week) for s in l_filter: if s[0] >= float(sf_thresh): - #if there's a custom user-made include list, we only want to use the species in that - if(len(INCLUDE_LIST) == 0): + # if there's a custom user-made include list, we only want to use the species in that + if (len(INCLUDE_LIST) == 0): PREDICTED_SPECIES_LIST.append(s[1]) + def loadCustomSpeciesList(path): slist = [] @@ -272,8 +278,7 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,): if model == "BirdNET_GLOBAL_3K_V2.2_Model_FP16": if len(PREDICTED_SPECIES_LIST) == 0 or len(INCLUDE_LIST) != 0: - predictSpeciesList(lat,lon,week) - + predictSpeciesList(lat, lon, week) # Convert and prepare metadata mdata = convertMetadata(np.array([lat, lon, week])) @@ -320,7 +325,9 @@ def writeResultsToFile(detections, min_conf, path): rfile.write('Start (s);End (s);Scientific name;Common name;Confidence\n') for d in detections: for entry in detections[d]: - if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0) ): + if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) + and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) + and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0)): rfile.write(d + ';' + entry[0].replace('_', ';').split("/")[0] + ';' + str(entry[1]) + '\n') rcnt += 1 print('DONE! WROTE', rcnt, 'RESULTS.') @@ -442,7 +449,8 @@ def handle_client(conn, addr): species_apprised_this_run = [] for entry in detections[d]: if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) - and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0) ): + and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) + and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0)): # Write to text file. rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';').split("/")[0] + ';' + str(entry[1]) + ";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';' @@ -561,7 +569,7 @@ def handle_client(conn, addr): post_algorithm = "\"algorithm\": " + "\"2p2\"" + "," else: post_algorithm = "\"algorithm\": " + "\"alpha\"" + "," - + post_confidence = "\"confidence\": " + str(entry[1]) post_end = " }" diff --git a/scripts/utils/notifications.py b/scripts/utils/notifications.py index 958044e..e063cd2 100644 --- a/scripts/utils/notifications.py +++ b/scripts/utils/notifications.py @@ -24,6 +24,7 @@ config = apprise.AppriseConfig() config.add(APPRISE_CONFIG) apobj.add(config) + def notify(body, title, attached=""): if attached != "": apobj.notify( @@ -38,7 +39,9 @@ def notify(body, title, attached=""): ) -def sendAppriseNotifications(species, confidence, confidencepct, path, date, time, week, latitude, longitude, cutoff, sens, overlap, settings_dict, db_path=DB_PATH): +def sendAppriseNotifications(species, confidence, confidencepct, path, + date, time, week, latitude, longitude, cutoff, + sens, overlap, settings_dict, db_path=DB_PATH): # print(sendAppriseNotifications) # print(settings_dict) if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0: @@ -57,7 +60,7 @@ def sendAppriseNotifications(species, confidence, confidencepct, path, date, tim print("APPRISE NOTIFICATION EXCEPTION: "+str(e)) return - #TODO: this all needs to be changed, we changed the caddy default to allow direct IP access, so birdnetpi.local shouldn't be relied on anymore + # TODO: this all needs to be changed, we changed the caddy default to allow direct IP access, so birdnetpi.local shouldn't be relied on anymore try: websiteurl = settings_dict.get('BIRDNETPI_URL') if len(websiteurl) == 0: @@ -69,10 +72,11 @@ def sendAppriseNotifications(species, confidence, confidencepct, path, date, tim image_url = "" if len(settings_dict.get('FLICKR_API_KEY')) > 0 and "$flickrimage" in body: - if not comName in flickr_images: + if comName not in flickr_images: try: # TODO: Make this work with non-english comnames. Implement the "// convert sci name to English name" logic from overview.php here - url = 'https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key='+str(settings_dict.get('FLICKR_API_KEY'))+'&text='+str(comName)+' bird&sort=relevance&per_page=5&media=photos&format=json&license=2%2C3%2C4%2C5%2C6%2C9&nojsoncallback=1' + url = 'https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key='+str(settings_dict.get('FLICKR_API_KEY'))+'&text='+str( + comName)+' bird&sort=relevance&per_page=5&media=photos&format=json&license=2%2C3%2C4%2C5%2C6%2C9&nojsoncallback=1' resp = requests.get(url=url) resp.encoding = "utf-8" data = resp.json()["photos"]["photo"][0] @@ -80,12 +84,11 @@ def sendAppriseNotifications(species, confidence, confidencepct, path, date, tim image_url = 'https://farm'+str(data["farm"])+'.static.flickr.com/'+str(data["server"])+'/'+str(data["id"])+'_'+str(data["secret"])+'_n.jpg' flickr_images[comName] = image_url except Exception as e: - print("FLICKR API ERROR: "+str(e)) + print("FLICKR API ERROR: "+str(e)) image_url = "" else: image_url = flickr_images[comName] - if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1": notify_body = body.replace("$sciname", sciName)\ .replace("$comname", comName)\ diff --git a/tests/test_apprise_notifications.py b/tests/test_apprise_notifications.py index 0dd666d..15a81ad 100644 --- a/tests/test_apprise_notifications.py +++ b/tests/test_apprise_notifications.py @@ -65,7 +65,7 @@ def test_notifications(mocker): "test.db") # No active apprise notifcations configured. Confirm no notifications. - assert(notify_call.call_count == 0) # No notification should be sent. + assert (notify_call.call_count == 0) # No notification should be sent. # Add daily notification. notify_call.reset_mock() @@ -85,8 +85,8 @@ def test_notifications(mocker): settings_dict, "test.db") - assert(notify_call.call_count == 1) - assert( + assert (notify_call.call_count == 1) + assert ( notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)" ) @@ -108,12 +108,13 @@ def test_notifications(mocker): settings_dict, "test.db") - assert(notify_call.call_count == 2) - assert( + assert (notify_call.call_count == 2) + assert ( notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)" ) - assert( - notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times in last 7d)" + assert ( + notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence \ + of 91 (only seen 1 times in last 7d)" ) # Add each species notification. @@ -134,4 +135,4 @@ def test_notifications(mocker): settings_dict, "test.db") - assert(notify_call.call_count == 3) + assert (notify_call.call_count == 3)