refactor: Change loop for dictionary comprehension

This will improve code execution because nested loops are slower than list/dictionary comprehensions.

Signed-off-by: nothingbutlucas <69118979+nothingbutlucas@users.noreply.github.com>
This commit is contained in:
nothingbutlucas 2024-04-24 12:08:23 -03:00
parent 059c2381b5
commit 1f6a494e14
No known key found for this signature in database
GPG Key ID: 31DE05DB3B0D9074

View File

@ -48,6 +48,13 @@ def size_str(num, suffix='B'):
return "%.2f%s%s" % (num, last_unit, suffix) return "%.2f%s%s" % (num, last_unit, suffix)
def convert_bytes_to_hex(value_obj):
if isinstance(value_obj, bytes):
return RNS.hexrep(value_obj, delimit=False)
elif isinstance(value_obj, dict):
return {key: convert_bytes_to_hex(value) for key, value in value_obj.items()}
return value_obj
def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=False, astats=False, sorting=None, sort_reverse=False): def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=False, astats=False, sorting=None, sort_reverse=False):
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
@ -63,22 +70,13 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
if json: if json:
import json import json
for s in stats: for s, value in stats.items():
if isinstance(stats[s], bytes): stats[s] = convert_bytes_to_hex(value)
stats[s] = RNS.hexrep(stats[s], delimit=False)
if isinstance(stats[s], dict):
for i in stats[s]:
if isinstance(i, dict):
for k in i:
if isinstance(i[k], bytes):
i[k] = RNS.hexrep(i[k], delimit=False)
print(json.dumps(stats)) print(json.dumps(stats))
exit() exit()
interfaces = stats["interfaces"] interfaces = stats["interfaces"]
if sorting != None and isinstance(sorting, str): if sorting is not None and isinstance(sorting, str):
sorting = sorting.lower() sorting = sorting.lower()
if sorting == "rate" or sorting == "bitrate": if sorting == "rate" or sorting == "bitrate":
interfaces.sort(key=lambda i: i["bitrate"], reverse=not sort_reverse) interfaces.sort(key=lambda i: i["bitrate"], reverse=not sort_reverse)
@ -97,7 +95,6 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
if sorting == "held": if sorting == "held":
interfaces.sort(key=lambda i: i["held_announces"], reverse=not sort_reverse) interfaces.sort(key=lambda i: i["held_announces"], reverse=not sort_reverse)
for ifstat in interfaces: for ifstat in interfaces:
name = ifstat["name"] name = ifstat["name"]
@ -130,8 +127,7 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
else: else:
modestr = "Full" modestr = "Full"
if ifstat["clients"] is not None:
if ifstat["clients"] != None:
clients = ifstat["clients"] clients = ifstat["clients"]
if name.startswith("Shared Instance["): if name.startswith("Shared Instance["):
cnum = max(clients-1,0) cnum = max(clients-1,0)
@ -221,6 +217,7 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
print("") print("")
def main(): def main():
try: try:
parser = argparse.ArgumentParser(description="Reticulum Network Stack Status") parser = argparse.ArgumentParser(description="Reticulum Network Stack Status")
@ -294,6 +291,7 @@ def main():
print("") print("")
exit() exit()
def speed_str(num, suffix='bps'): def speed_str(num, suffix='bps'):
units = ['','k','M','G','T','P','E','Z'] units = ['','k','M','G','T','P','E','Z']
last_unit = 'Y' last_unit = 'Y'
@ -310,5 +308,6 @@ def speed_str(num, suffix='bps'):
return "%.2f %s%s" % (num, last_unit, suffix) return "%.2f %s%s" % (num, last_unit, suffix)
if __name__ == "__main__": if __name__ == "__main__":
main() main()