refactor: Replace string concatenation with joins

Same as previous commits. "".join() is better that "string" + "another_string" + "another_string_2", because strings are immutable.
Also, changed for loops with list comprehension. This is much faster and would improve performance.

Signed-off-by: nothingbutlucas <69118979+nothingbutlucas@users.noreply.github.com>
This commit is contained in:
nothingbutlucas 2024-04-24 15:37:10 -03:00
parent c032a2e438
commit acd893a6e6
No known key found for this signature in database
GPG Key ID: 31DE05DB3B0D9074
1 changed files with 24 additions and 28 deletions

View File

@ -28,7 +28,7 @@ import argparse
import shlex
import time
import sys
import tty
# import tty # Commented this because commented lines below use it
import os
from RNS._version import __version__
@ -39,9 +39,10 @@ reticulum = None
allow_all = False
allowed_identity_hashes = []
def prepare_identity(identity_path):
global identity
if identity_path == None:
if identity_path is None:
identity_path = RNS.Reticulum.identitypath+"/"+APP_NAME
if os.path.isfile(identity_path):
@ -52,6 +53,7 @@ def prepare_identity(identity_path):
identity = RNS.Identity()
identity.to_file(identity_path)
def listen(configdir, identitypath = None, verbosity = 0, quietness = 0, allowed = [], print_identity = False, disable_auth = None, disable_announce=False):
global identity, allow_all, allowed_identity_hashes, reticulum
@ -69,7 +71,7 @@ def listen(configdir, identitypath = None, verbosity = 0, quietness = 0, allowed
if disable_auth:
allow_all = True
else:
if allowed != None:
if allowed is not None:
for a in allowed:
try:
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
@ -297,6 +299,7 @@ def remote_execution_progress(request_receipt):
link = None
listener_destination = None
remote_exec_grace = 2.0
def execute(configdir, identitypath = None, verbosity = 0, quietness = 0, detailed = False, mirror = False, noid = False, destination = None, command = None, stdin = None, stdoutl = None, stderrl = None, timeout = RNS.Transport.PATH_REQUEST_TIMEOUT, result_timeout = None, interactive = False):
global identity, reticulum, link, listener_destination, remote_exec_grace
@ -548,7 +551,6 @@ def main():
parser.add_argument("--stdout", action='store', default=None, help="max size in bytes of returned stdout", type=int)
parser.add_argument("--stderr", action='store', default=None, help="max size in bytes of returned stderr", type=int)
parser.add_argument("--version", action="version", version="rnx {version}".format(version=__version__))
args = parser.parse_args()
if args.listen or args.print_identity:
@ -668,6 +670,7 @@ def size_str(num, suffix='B'):
return "%.2f%s%s" % (num, last_unit, suffix)
def pretty_time(time, verbose=False):
days = int(time // (24 * 3600))
time = time % (24 * 3600)
@ -684,31 +687,24 @@ def pretty_time(time, verbose=False):
components = []
if days > 0:
components.append(str(days)+" day"+sd if verbose else str(days)+"d")
components.append("".join([str(days), " day", sd if verbose else str(days), "d"]))
if hours > 0:
components.append(str(hours)+" hour"+sh if verbose else str(hours)+"h")
components.append("".join([str(hours), " hour", sh if verbose else str(hours), "h"]))
if minutes > 0:
components.append(str(minutes)+" minute"+sm if verbose else str(minutes)+"m")
components.append("".join([str(minutes), " minute", sm if verbose else str(minutes), "m"]))
if seconds > 0:
components.append(str(seconds)+" second"+ss if verbose else str(seconds)+"s")
components.append("".join([str(seconds), " second", ss if verbose else str(seconds), "s"]))
i = 0
tstr = ""
for c in components:
i += 1
if i == 1:
pass
elif i < len(components):
tstr += ", "
elif i == len(components):
tstr += " and "
if len(components) > 1:
time_string = "".join([", ".join(components[:-1]), " and ", components[-1]])
else:
time_string = components[0] if components else ""
tstr += c
return time_string
return tstr
if __name__ == "__main__":
main()