More efficient detection of empty lists

main
Bob Mottram 2025-04-12 20:40:40 +01:00
parent 28706dc0b0
commit c431556df2
9 changed files with 41 additions and 41 deletions

View File

@ -89,7 +89,7 @@ def is_editor(base_dir: str, nickname: str) -> bool:
except OSError:
print('EX: is_editor unable to read ' + editors_file)
if len(lines) == 0:
if not lines:
admin_name = get_config_param(base_dir, 'admin')
if admin_name:
if admin_name == nickname:
@ -120,7 +120,7 @@ def is_artist(base_dir: str, nickname: str) -> bool:
except OSError:
print('EX: is_artist unable to read ' + artists_file)
if len(lines) == 0:
if not lines:
admin_name = get_config_param(base_dir, 'admin')
if admin_name:
if admin_name == nickname:

View File

@ -2663,7 +2663,7 @@ def _restore_queue_items(base_dir: str, queue: []) -> None:
queue.append(os.path.join(queue_dir, qfile))
break
break
if len(queue) > 0:
if queue:
print('Restored ' + str(len(queue)) + ' inbox queue items')
@ -2705,13 +2705,13 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str,
print('Queue: Quota per day - Maximum posts for ' +
post_domain + ' reached (' +
str(domain_max_posts_per_day) + ')')
if len(queue) > 0:
if queue:
try:
os.remove(queue_filename)
except OSError:
print('EX: _inbox_quota_exceeded unable to delete 1 ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
return True
quotas_daily['domains'][post_domain] += 1
@ -2728,13 +2728,13 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str,
print('Queue: Quota per min - Maximum posts for ' +
post_domain + ' reached (' +
str(domain_max_posts_per_min) + ')')
if len(queue) > 0:
if queue:
try:
os.remove(queue_filename)
except OSError:
print('EX: _inbox_quota_exceeded unable to delete 2 ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
return True
quotas_per_min['domains'][post_domain] += 1
@ -2750,13 +2750,13 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str,
' Maximum posts for ' +
post_handle + ' reached (' +
str(account_max_posts_per_day) + ')')
if len(queue) > 0:
if queue:
try:
os.remove(queue_filename)
except OSError:
print('EX: _inbox_quota_exceeded unable to delete 3 ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
return True
quotas_daily['accounts'][post_handle] += 1
@ -2773,13 +2773,13 @@ def _inbox_quota_exceeded(queue: {}, queue_filename: str,
' Maximum posts for ' +
post_handle + ' reached (' +
str(account_max_posts_per_min) + ')')
if len(queue) > 0:
if queue:
try:
os.remove(queue_filename)
except OSError:
print('EX: _inbox_quota_exceeded unable to delete 4 ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
return True
quotas_per_min['accounts'][post_handle] += 1
@ -3280,7 +3280,7 @@ def run_inbox_queue(server,
curr_mitm_servers = server.mitm_servers.copy()
save_mitm_servers(base_dir, curr_mitm_servers)
if len(queue) == 0:
if not queue:
# restore any remaining queue items
queue_restore_ctr += 1
if queue_restore_ctr >= 30:
@ -3297,7 +3297,7 @@ def run_inbox_queue(server,
if not os.path.isfile(queue_filename):
print("Queue: queue item rejected because it has no file: " +
queue_filename)
if len(queue) > 0:
if queue:
queue.pop(0)
continue
@ -3313,7 +3313,7 @@ def run_inbox_queue(server,
print('Queue: run_inbox_queue failed to load inbox queue item ' +
queue_filename)
# Assume that the file is probably corrupt/unreadable
if len(queue) > 0:
if queue:
queue.pop(0)
# delete the queue file
if os.path.isfile(queue_filename):
@ -3461,7 +3461,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 2 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
continue
@ -3521,7 +3521,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 3 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
continue
else:
@ -3543,7 +3543,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 4 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', 'not_verify_signature',
@ -3580,7 +3580,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 5 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', '_receive_undo',
@ -3611,7 +3611,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 6 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
print('Queue: Follow activity for ' + key_id +
' removed from queue')
@ -3634,7 +3634,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 7 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', 'receive_accept_reject',
@ -3666,7 +3666,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 7 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', 'receive_quote_request',
@ -3700,7 +3700,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 8 unable to receive move ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', '_receive_move_activity',
@ -3742,7 +3742,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 8 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
fitness_performance(inbox_start_time, server.fitness,
'INBOX', '_receive_update_activity',
@ -3755,8 +3755,8 @@ def run_inbox_queue(server,
_inbox_post_recipients(base_dir, queue_json['post'],
domain, port, debug,
onion_domain, i2p_domain)
if len(recipients_dict.items()) == 0 and \
len(recipients_dict_followers.items()) == 0:
if not recipients_dict.items() and \
not recipients_dict_followers.items():
if debug:
print('Queue: no recipients were resolved ' +
'for post arriving in inbox')
@ -3766,7 +3766,7 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 9 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)
continue
fitness_performance(inbox_start_time, server.fitness,
@ -3879,5 +3879,5 @@ def run_inbox_queue(server,
except OSError:
print('EX: run_inbox_queue 10 unable to delete ' +
str(queue_filename))
if len(queue) > 0:
if queue:
queue.pop(0)

View File

@ -185,7 +185,7 @@ def is_moderator(base_dir: str, nickname: str) -> bool:
except OSError:
print('EX: is_moderator unable to read ' + moderators_file)
if len(lines) == 0:
if not lines:
admin_name = get_config_param(base_dir, 'admin')
if not admin_name:
return False
@ -2843,7 +2843,7 @@ def create_report_post(base_dir: str,
moderators_list.append(moderator_actor)
except OSError:
print('EX: create_report_post unable to read ' + moderators_file)
if len(moderators_list) == 0:
if not moderators_list:
# if there are no moderators then the admin becomes the moderator
admin_nickname = get_config_param(base_dir, 'admin')
if admin_nickname:
@ -3535,9 +3535,9 @@ def _add_followers_to_public_post(post_json_object: {}) -> None:
if isinstance(post_json_object['object'], str):
if not post_json_object.get('to'):
return
if len(post_json_object['to']) > 1:
if not post_json_object['to']:
return
if len(post_json_object['to']) == 0:
if len(post_json_object['to']) > 1:
return
if not post_json_object['to'][0].endswith('#Public'):
if not post_json_object['to'][0] == 'as:Public':
@ -3550,9 +3550,9 @@ def _add_followers_to_public_post(post_json_object: {}) -> None:
elif has_object_dict(post_json_object):
if not post_json_object['object'].get('to'):
return
if len(post_json_object['object']['to']) > 1:
if not post_json_object['object']['to']:
return
if len(post_json_object['object']['to']) == 0:
if len(post_json_object['object']['to']) > 1:
return
if not post_json_object['object']['to'][0].endswith('#Public'):
if not post_json_object['object']['to'][0] == 'as:Public':

View File

@ -283,13 +283,13 @@ def is_devops(base_dir: str, nickname: str) -> bool:
return True
return False
lines = []
lines: list[str] = []
try:
with open(devops_file, 'r', encoding='utf-8') as fp_mod:
lines = fp_mod.readlines()
except OSError:
print('EX: is_devops unable to read ' + devops_file)
if len(lines) == 0:
if not lines:
# if there is nothing in the file
admin_name = get_config_param(base_dir, 'admin')
if not admin_name:

View File

@ -78,7 +78,7 @@ def _load_dfc_ids(base_dir: str, system_language: str,
if not product_types.get('@graph'):
print('No @graph list within ontology')
return None
if len(product_types['@graph']) == 0:
if not product_types['@graph']:
print('@graph list has no contents')
return None
if not product_types['@graph'][0].get('rdfs:label'):

View File

@ -91,7 +91,7 @@ def remove_dormant_threads(base_dir: str, threads_list: [], debug: bool,
timeout_mins: int) -> None:
"""Removes threads whose execution has completed
"""
if len(threads_list) == 0:
if not threads_list:
return
timeout_secs = int(timeout_mins * 60)

View File

@ -1407,7 +1407,7 @@ def create_inbox_queue_dir(nickname: str, domain: str, base_dir: str) -> str:
def domain_permitted(domain: str, federation_list: []) -> bool:
"""Is the given domain permitted according to the federation list?
"""
if len(federation_list) == 0:
if not federation_list:
return True
domain = remove_domain_port(domain)
if domain in federation_list:

View File

@ -1804,7 +1804,7 @@ def _html_profile_posts(recent_posts_cache: {}, max_recent_posts: int,
authorized, 0, False, 0)
if not outbox_feed:
break
if len(outbox_feed['orderedItems']) == 0:
if not outbox_feed['orderedItems']:
break
shown_items: list[str] = []
for item in outbox_feed['orderedItems']:

View File

@ -678,7 +678,7 @@ def html_skills_search(actor: str, translate: {}, base_dir: str,
skillsearch + \
'</a></h1></center>'
if len(results) == 0:
if not results:
skill_search_form += \
'<center><h5>' + translate['No results'] + \
'</h5></center>'
@ -790,7 +790,7 @@ def html_history_search(translate: {}, base_dir: str,
'<center><h1><a href="' + actor + '/search">' + \
history_search_title + '</a></h1></center>'
if len(box_filenames) == 0:
if not box_filenames:
history_search_form += \
'<center><h5>' + translate['No results'] + \
'</h5></center>'