Convert format strings strings to f-strings (#23241)

* Convert all text strings to f-strings

Reformats all the text from the old "%-formatted" and .format(...) format to the newer f-string format, as defined in PEP 498. This requires Python 3.6+.

Flynt 0.69 was used to reformat the strings. 120 f-strings were created in 51 files.

F-strings are in general more readable, concise and performant. See also: https://www.python.org/dev/peps/pep-0498/#rationale

* revert pyextra changes

* revert ublox.py

Co-authored-by: Willem Melching <willem.melching@gmail.com>
pull/23251/head
Ewout ter Hoeven 2021-12-16 14:58:17 +01:00 committed by GitHub
parent 4f1eb4278a
commit 55390d273f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 95 additions and 96 deletions

View File

@ -28,7 +28,7 @@ def ffi_wrap(name, c_code, c_header, tmpdir="/tmp/ccache", cflags="", libraries=
try: try:
mod = __import__(cache) mod = __import__(cache)
except Exception: except Exception:
print("cache miss {0}".format(cache)) print(f"cache miss {cache}")
compile_code(cache, c_code, c_header, tmpdir, cflags, libraries) compile_code(cache, c_code, c_header, tmpdir, cflags, libraries)
mod = __import__(cache) mod = __import__(cache)
finally: finally:

View File

@ -35,7 +35,7 @@ def get_tmpdir_on_same_filesystem(path):
if len(parts) > 1 and parts[1] == "scratch": if len(parts) > 1 and parts[1] == "scratch":
return "/scratch/tmp" return "/scratch/tmp"
elif len(parts) > 2 and parts[2] == "runner": elif len(parts) > 2 and parts[2] == "runner":
return "/{}/runner/tmp".format(parts[1]) return f"/{parts[1]}/runner/tmp"
return "/tmp" return "/tmp"

View File

@ -42,4 +42,4 @@ class Profiler():
print("%30s: %9.2f avg: %7.2f percent: %3.0f IGNORED" % (n, ms*1000.0, ms*1000.0/self.iter, ms/self.tot*100)) print("%30s: %9.2f avg: %7.2f percent: %3.0f IGNORED" % (n, ms*1000.0, ms*1000.0/self.iter, ms/self.tot*100))
else: else:
print("%30s: %9.2f avg: %7.2f percent: %3.0f" % (n, ms*1000.0, ms*1000.0/self.iter, ms/self.tot*100)) print("%30s: %9.2f avg: %7.2f percent: %3.0f" % (n, ms*1000.0, ms*1000.0/self.iter, ms/self.tot*100))
print("Iter clock: %2.6f TOTAL: %2.2f" % (self.tot/self.iter, self.tot)) print(f"Iter clock: {self.tot / self.iter:2.6f} TOTAL: {self.tot:2.2f}")

View File

@ -79,7 +79,7 @@ class Ratekeeper:
remaining = self._next_frame_time - sec_since_boot() remaining = self._next_frame_time - sec_since_boot()
self._next_frame_time += self._interval self._next_frame_time += self._interval
if self._print_delay_threshold is not None and remaining < -self._print_delay_threshold: if self._print_delay_threshold is not None and remaining < -self._print_delay_threshold:
print("%s lagging by %.2f ms" % (self._process_name, -remaining * 1000)) print(f"{self._process_name} lagging by {-remaining * 1000:.2f} ms")
lagged = True lagged = True
self._frame += 1 self._frame += 1
self._remaining = remaining self._remaining = remaining

View File

@ -8,7 +8,7 @@ from common.file_helpers import atomic_write_in_dir
class TestFileHelpers(unittest.TestCase): class TestFileHelpers(unittest.TestCase):
def run_atomic_write_func(self, atomic_write_func): def run_atomic_write_func(self, atomic_write_func):
path = "/tmp/tmp{}".format(uuid4()) path = f"/tmp/tmp{uuid4()}"
with atomic_write_func(path) as f: with atomic_write_func(path) as f:
f.write("test") f.write("test")

View File

@ -12,7 +12,7 @@ class Timeout:
""" """
def __init__(self, seconds, error_msg=None): def __init__(self, seconds, error_msg=None):
if error_msg is None: if error_msg is None:
error_msg = 'Timed out after {} seconds'.format(seconds) error_msg = f'Timed out after {seconds} seconds'
self.seconds = seconds self.seconds = seconds
self.error_msg = error_msg self.error_msg = error_msg

View File

@ -54,7 +54,7 @@ class TestCamerad(unittest.TestCase):
self.assertTrue(abs(dfid - 1) <= SKIP_FRAME_TOLERANCE, "%s frame id diff is %d" % (camera, dfid)) self.assertTrue(abs(dfid - 1) <= SKIP_FRAME_TOLERANCE, "%s frame id diff is %d" % (camera, dfid))
dts = ct - last_ts[camera] dts = ct - last_ts[camera]
self.assertTrue(abs(dts - (1000/CAMERAS[camera])) < LAG_FRAME_TOLERANCE, "%s frame t(ms) diff is %f" % (camera, dts)) self.assertTrue(abs(dts - (1000/CAMERAS[camera])) < LAG_FRAME_TOLERANCE, f"{camera} frame t(ms) diff is {dts:f}")
last_frame_id[camera] = sm[camera].frameId last_frame_id[camera] = sm[camera].frameId
last_ts[camera] = ct last_ts[camera] = ct

View File

@ -39,7 +39,7 @@ def get_one_can(logcan):
def load_interfaces(brand_names): def load_interfaces(brand_names):
ret = {} ret = {}
for brand_name in brand_names: for brand_name in brand_names:
path = ('selfdrive.car.%s' % brand_name) path = f'selfdrive.car.{brand_name}'
CarInterface = __import__(path + '.interface', fromlist=['CarInterface']).CarInterface CarInterface = __import__(path + '.interface', fromlist=['CarInterface']).CarInterface
if os.path.exists(BASEDIR + '/' + path.replace('.', '/') + '/carstate.py'): if os.path.exists(BASEDIR + '/' + path.replace('.', '/') + '/carstate.py'):
@ -65,7 +65,7 @@ def _get_interface_names():
for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]: for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]:
try: try:
brand_name = car_folder.split('/')[-1] brand_name = car_folder.split('/')[-1]
model_names = __import__('selfdrive.car.%s.values' % brand_name, fromlist=['CAR']).CAR model_names = __import__(f'selfdrive.car.{brand_name}.values', fromlist=['CAR']).CAR
model_names = [getattr(model_names, c) for c in model_names.__dict__.keys() if not c.startswith("__")] model_names = [getattr(model_names, c) for c in model_names.__dict__.keys() if not c.startswith("__")]
brand_names[brand_name] = model_names brand_names[brand_name] = model_names
except (ImportError, IOError): except (ImportError, IOError):

View File

@ -11,7 +11,7 @@ def get_attr_from_cars(attr, result=dict, combine_brands=True):
for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]: for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]:
try: try:
car_name = car_folder.split('/')[-1] car_name = car_folder.split('/')[-1]
values = __import__('selfdrive.car.%s.values' % car_name, fromlist=[attr]) values = __import__(f'selfdrive.car.{car_name}.values', fromlist=[attr])
if hasattr(values, attr): if hasattr(values, attr):
attr_values = getattr(values, attr) attr_values = getattr(values, attr)
else: else:

View File

@ -362,7 +362,7 @@ if __name__ == "__main__":
print("Getting vin...") print("Getting vin...")
addr, vin = get_vin(logcan, sendcan, 1, retry=10, debug=args.debug) addr, vin = get_vin(logcan, sendcan, 1, retry=10, debug=args.debug)
print(f"VIN: {vin}") print(f"VIN: {vin}")
print("Getting VIN took %.3f s" % (time.time() - t)) print(f"Getting VIN took {time.time() - t:.3f} s")
print() print()
t = time.time() t = time.time()
@ -379,4 +379,4 @@ if __name__ == "__main__":
print() print()
print("Possible matches:", candidates) print("Possible matches:", candidates)
print("Getting fw took %.3f s" % (time.time() - t)) print(f"Getting fw took {time.time() - t:.3f} s")

View File

@ -301,7 +301,7 @@ class CarInterface(CarInterfaceBase):
ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.6], [0.18]] # TODO: can probably use some tuning ret.lateralTuning.pid.kpV, ret.lateralTuning.pid.kiV = [[0.6], [0.18]] # TODO: can probably use some tuning
else: else:
raise ValueError("unsupported car %s" % candidate) raise ValueError(f"unsupported car {candidate}")
# These cars use alternate user brake msg (0x1BE) # These cars use alternate user brake msg (0x1BE)
if candidate in HONDA_BOSCH_ALT_BRAKE_SIGNAL: if candidate in HONDA_BOSCH_ALT_BRAKE_SIGNAL:

View File

@ -59,7 +59,7 @@ class TestCarInterfaces(unittest.TestCase):
car_interface.apply(CC) car_interface.apply(CC)
# Test radar interface # Test radar interface
RadarInterface = importlib.import_module('selfdrive.car.%s.radar_interface' % car_params.carName).RadarInterface RadarInterface = importlib.import_module(f'selfdrive.car.{car_params.carName}.radar_interface').RadarInterface
radar_interface = RadarInterface(car_params) radar_interface = RadarInterface(car_params)
assert radar_interface assert radar_interface

View File

@ -152,7 +152,7 @@ class CarInterface(CarInterfaceBase):
ret.wheelbase = 2.84 ret.wheelbase = 2.84
else: else:
raise ValueError("unsupported car %s" % candidate) raise ValueError(f"unsupported car {candidate}")
ret.rotationalInertia = scale_rot_inertia(ret.mass, ret.wheelbase) ret.rotationalInertia = scale_rot_inertia(ret.mass, ret.wheelbase)
ret.centerToFront = ret.wheelbase * 0.45 ret.centerToFront = ret.wheelbase * 0.45

View File

@ -377,8 +377,7 @@ class LongitudinalMpc():
if self.solution_status != 0: if self.solution_status != 0:
if t > self.last_cloudlog_t + 5.0: if t > self.last_cloudlog_t + 5.0:
self.last_cloudlog_t = t self.last_cloudlog_t = t
cloudlog.warning("Long mpc reset, solution_status: %s" % ( cloudlog.warning(f"Long mpc reset, solution_status: {self.solution_status}")
self.solution_status))
self.reset() self.reset()

View File

@ -146,7 +146,7 @@ class Cluster():
} }
def __str__(self): def __str__(self):
ret = "x: %4.1f y: %4.1f v: %4.1f a: %4.1f" % (self.dRel, self.yRel, self.vRel, self.aLeadK) ret = f"x: {self.dRel:4.1f} y: {self.yRel:4.1f} v: {self.vRel:4.1f} a: {self.aLeadK:4.1f}"
return ret return ret
def potential_low_speed_lead(self, v_ego): def potential_low_speed_lead(self, v_ego):

View File

@ -189,7 +189,7 @@ def radard_thread(sm=None, pm=None, can_sock=None):
# import the radar from the fingerprint # import the radar from the fingerprint
cloudlog.info("radard is importing %s", CP.carName) cloudlog.info("radard is importing %s", CP.carName)
RadarInterface = importlib.import_module('selfdrive.car.%s.radar_interface' % CP.carName).RadarInterface RadarInterface = importlib.import_module(f'selfdrive.car.{CP.carName}.radar_interface').RadarInterface
# *** setup messaging # *** setup messaging
if can_sock is None: if can_sock is None:

View File

@ -70,7 +70,7 @@ class TestAlerts(unittest.TestCase):
font = fonts[alert.alert_size][i] font = fonts[alert.alert_size][i]
w, _ = draw.textsize(txt, font) w, _ = draw.textsize(txt, font)
msg = "type: %s msg: %s" % (alert.alert_type, txt) msg = f"type: {alert.alert_type} msg: {txt}"
self.assertLessEqual(w, max_text_width, msg=msg) self.assertLessEqual(w, max_text_width, msg=msg)
def test_alert_sanity_check(self): def test_alert_sanity_check(self):

View File

@ -22,7 +22,7 @@ def can_printer(bus, max_msg, addr):
if sec_since_boot() - lp > 0.1: if sec_since_boot() - lp > 0.1:
dd = chr(27) + "[2J" dd = chr(27) + "[2J"
dd += "%5.2f\n" % (sec_since_boot() - start) dd += f"{sec_since_boot() - start:5.2f}\n"
for addr in sorted(msgs.keys()): for addr in sorted(msgs.keys()):
a = msgs[addr][-1].decode('ascii', 'backslashreplace') a = msgs[addr][-1].decode('ascii', 'backslashreplace')
x = binascii.hexlify(msgs[addr][-1]).decode('ascii') x = binascii.hexlify(msgs[addr][-1]).decode('ascii')

View File

@ -42,6 +42,6 @@ if __name__ == "__main__":
for name in socket_names: for name in socket_names:
dts = np.diff(rcv_times[name]) dts = np.diff(rcv_times[name])
mean = np.mean(dts) mean = np.mean(dts)
print("%s: Freq %.2f Hz, Min %.2f%%, Max %.2f%%, valid " % (name, 1.0 / mean, np.min(dts) / mean * 100, np.max(dts) / mean * 100), all(valids[name])) print(f"{name}: Freq {1.0 / mean:.2f} Hz, Min {np.min(dts) / mean * 100:.2f}%, Max {np.max(dts) / mean * 100:.2f}%, valid ", all(valids[name]))
prev_print = t prev_print = t

View File

@ -110,10 +110,10 @@ if __name__ == "__main__":
stat['avg'][name] = (stat['avg'][name] * (i - c) + avg * c) / (i) stat['avg'][name] = (stat['avg'][name] * (i - c) + avg * c) / (i)
stat['cpu_samples'][name] = [] stat['cpu_samples'][name] = []
msg = 'avg: {1:.2%}, min: {2:.2%}, max: {3:.2%} {0}'.format(os.path.basename(k), stat['avg']['total'], stat['min']['total'], stat['max']['total']) msg = f"avg: {stat['avg']['total']:.2%}, min: {stat['min']['total']:.2%}, max: {stat['max']['total']:.2%} {os.path.basename(k)}"
if args.detailed_times: if args.detailed_times:
for stat_type in ['avg', 'min', 'max']: for stat_type in ['avg', 'min', 'max']:
msg += '\n {}: {}'.format(stat_type, [name + ':' + str(round(stat[stat_type][name]*100, 2)) for name in cpu_time_names]) msg += f"\n {stat_type}: {[(name + ':' + str(round(stat[stat_type][name] * 100, 2))) for name in cpu_time_names]}"
l.append((os.path.basename(k), stat['avg']['total'], msg)) l.append((os.path.basename(k), stat['avg']['total'], msg))
l.sort(key=lambda x: -x[1]) l.sort(key=lambda x: -x[1])
for x in l: for x in l:

View File

@ -54,13 +54,13 @@ if __name__ == "__main__":
elif args.dump_json: elif args.dump_json:
print(json.dumps(evt.to_dict())) print(json.dumps(evt.to_dict()))
elif values: elif values:
print("logMonotime = {}".format(evt.logMonoTime)) print(f"logMonotime = {evt.logMonoTime}")
for value in values: for value in values:
if hasattr(evt, value[0]): if hasattr(evt, value[0]):
item = evt item = evt
for key in value: for key in value:
item = getattr(item, key) item = getattr(item, key)
print("{} = {}".format(".".join(value), item)) print(f"{'.'.join(value)} = {item}")
print("") print("")
else: else:
try: try:

View File

@ -27,5 +27,5 @@ while True:
fingerprint = ', '.join("%d: %d" % v for v in sorted(msgs.items())) fingerprint = ', '.join("%d: %d" % v for v in sorted(msgs.items()))
print("number of messages {0}:".format(len(msgs))) print(f"number of messages {len(msgs)}:")
print("fingerprint {0}".format(fingerprint)) print(f"fingerprint {fingerprint}")

View File

@ -45,7 +45,7 @@ if __name__ == '__main__':
capacity_average = average(capacity_average, capacity) capacity_average = average(capacity_average, capacity)
bat_temp_average = average(bat_temp_average, bat_temp) bat_temp_average = average(bat_temp_average, bat_temp)
print("%.2f volts %12.2f ma %12.2f mW %8.2f%% battery %8.1f degC" % (voltage, current, power, capacity, bat_temp)) print(f"{voltage:.2f} volts {current:12.2f} ma {power:12.2f} mW {capacity:8.2f}% battery {bat_temp:8.1f} degC")
time.sleep(0.1) time.sleep(0.1)
finally: finally:
stop_time = datetime.now() stop_time = datetime.now()
@ -55,8 +55,8 @@ if __name__ == '__main__':
power = power_average[0] power = power_average[0]
capacity = capacity_average[0] capacity = capacity_average[0]
bat_temp = bat_temp_average[0] bat_temp = bat_temp_average[0]
print("%.2f volts %12.2f ma %12.2f mW %8.2f%% battery %8.1f degC" % (voltage, current, power, capacity, bat_temp)) print(f"{voltage:.2f} volts {current:12.2f} ma {power:12.2f} mW {capacity:8.2f}% battery {bat_temp:8.1f} degC")
print(" {:.2f} Seconds {} samples".format((stop_time-start_time).total_seconds(), voltage_average[1])) print(f" {(stop_time - start_time).total_seconds():.2f} Seconds {voltage_average[1]} samples")
print("----------------------------------------------------------------") print("----------------------------------------------------------------")
# reenable charging # reenable charging

View File

@ -71,7 +71,7 @@ if __name__ == "__main__":
total_times = total_times_new[:] total_times = total_times_new[:]
busy_times = busy_times_new[:] busy_times = busy_times_new[:]
print("CPU %.2f%% - RAM: %.2f%% - Temp %.2fC" % (100. * mean(cores), last_mem, last_temp)) print(f"CPU {100.0 * mean(cores):.2f}% - RAM: {last_mem:.2f}% - Temp {last_temp:.2f}C")
if args.cpu and prev_proclog is not None: if args.cpu and prev_proclog is not None:
procs = {} procs = {}

View File

@ -40,7 +40,7 @@ if __name__ == '__main__':
power_average = average(power_average, power) power_average = average(power_average, power)
power_total_average = average(power_total_average, power_total) power_total_average = average(power_total_average, power_total)
print("%12.2f mW %12.2f mW %12.2f mW" % (power, power_total, power_total-power)) print(f"{power:12.2f} mW {power_total:12.2f} mW {power_total - power:12.2f} mW")
time.sleep(0.25) time.sleep(0.25)
finally: finally:
stop_time = time.monotonic() stop_time = time.monotonic()
@ -48,6 +48,6 @@ if __name__ == '__main__':
voltage = voltage_average[0] voltage = voltage_average[0]
current = current_average[0] current = current_average[0]
power = power_average[0] power = power_average[0]
print("%.2f volts %12.2f ma %12.2f mW %12.2f mW" % (voltage, current, power, power_total)) print(f"{voltage:.2f} volts {current:12.2f} ma {power:12.2f} mW {power_total:12.2f} mW")
print(" {:.2f} Seconds {} samples".format(stop_time - start_time, voltage_average[1])) print(f" {stop_time - start_time:.2f} Seconds {voltage_average[1]} samples")
print("----------------------------------------------------------------") print("----------------------------------------------------------------")

View File

@ -27,11 +27,11 @@ def deleter_thread(exit_event):
continue continue
try: try:
cloudlog.info("deleting %s" % delete_path) cloudlog.info(f"deleting {delete_path}")
shutil.rmtree(delete_path) shutil.rmtree(delete_path)
break break
except OSError: except OSError:
cloudlog.exception("issue deleting %s" % delete_path) cloudlog.exception(f"issue deleting {delete_path}")
exit_event.wait(.1) exit_event.wait(.1)
else: else:
exit_event.wait(30) exit_event.wait(30)

View File

@ -4,5 +4,5 @@ from common.xattr import removexattr
from selfdrive.loggerd.uploader import UPLOAD_ATTR_NAME from selfdrive.loggerd.uploader import UPLOAD_ATTR_NAME
for fn in sys.argv[1:]: for fn in sys.argv[1:]:
print("unmarking %s" % fn) print(f"unmarking {fn}")
removexattr(fn, UPLOAD_ATTR_NAME) removexattr(fn, UPLOAD_ATTR_NAME)

View File

@ -140,7 +140,7 @@ class Uploader():
cloudlog.debug("upload_url v1.4 %s %s", url, str(headers)) cloudlog.debug("upload_url v1.4 %s %s", url, str(headers))
if fake_upload: if fake_upload:
cloudlog.debug("*** WARNING, THIS IS A FAKE UPLOAD TO %s ***" % url) cloudlog.debug(f"*** WARNING, THIS IS A FAKE UPLOAD TO {url} ***")
class FakeResponse(): class FakeResponse():
def __init__(self): def __init__(self):

View File

@ -39,7 +39,7 @@ def launcher(proc, name):
# exec the process # exec the process
mod.main() mod.main()
except KeyboardInterrupt: except KeyboardInterrupt:
cloudlog.warning("child %s got SIGINT" % proc) cloudlog.warning(f"child {proc} got SIGINT")
except Exception: except Exception:
# can't install the crash handler because sys.excepthook doesn't play nice # can't install the crash handler because sys.excepthook doesn't play nice
# with threads, so catch it here. # with threads, so catch it here.
@ -194,7 +194,7 @@ class NativeProcess(ManagerProcess):
return return
cwd = os.path.join(BASEDIR, self.cwd) cwd = os.path.join(BASEDIR, self.cwd)
cloudlog.info("starting process %s" % self.name) cloudlog.info(f"starting process {self.name}")
self.proc = Process(name=self.name, target=nativelauncher, args=(self.cmdline, cwd)) self.proc = Process(name=self.name, target=nativelauncher, args=(self.cmdline, cwd))
self.proc.start() self.proc.start()
self.watchdog_seen = False self.watchdog_seen = False
@ -214,7 +214,7 @@ class PythonProcess(ManagerProcess):
def prepare(self): def prepare(self):
if self.enabled: if self.enabled:
cloudlog.info("preimporting %s" % self.module) cloudlog.info(f"preimporting {self.module}")
importlib.import_module(self.module) importlib.import_module(self.module)
def start(self): def start(self):
@ -225,7 +225,7 @@ class PythonProcess(ManagerProcess):
if self.proc is not None: if self.proc is not None:
return return
cloudlog.info("starting python %s" % self.module) cloudlog.info(f"starting python {self.module}")
self.proc = Process(name=self.name, target=launcher, args=(self.module, self.name)) self.proc = Process(name=self.name, target=launcher, args=(self.module, self.name))
self.proc.start() self.proc.start()
self.watchdog_seen = False self.watchdog_seen = False
@ -260,7 +260,7 @@ class DaemonProcess(ManagerProcess):
# process is dead # process is dead
pass pass
cloudlog.info("starting daemon %s" % self.name) cloudlog.info(f"starting daemon {self.name}")
proc = subprocess.Popen(['python', '-m', self.module], # pylint: disable=subprocess-popen-preexec-fn proc = subprocess.Popen(['python', '-m', self.module], # pylint: disable=subprocess-popen-preexec-fn
stdin=open('/dev/null', 'r'), stdin=open('/dev/null', 'r'),
stdout=open('/dev/null', 'w'), stdout=open('/dev/null', 'w'),

View File

@ -62,7 +62,7 @@ class VisionTest():
disable_model = 0 disable_model = 0
temporal_model = 1 temporal_model = 1
else: else:
raise ValueError("Bad model name: {}".format(model)) raise ValueError(f"Bad model name: {model}")
prevdir = os.getcwd() prevdir = os.getcwd()
os.chdir(_visiond_dir) # tmp hack to find kernels os.chdir(_visiond_dir) # tmp hack to find kernels

View File

@ -9,7 +9,7 @@ TOKEN_PATH = "/data/azure_token"
def get_url(route_name, segment_num, log_type="rlog"): def get_url(route_name, segment_num, log_type="rlog"):
ext = "hevc" if log_type in ["fcamera", "dcamera"] else "bz2" ext = "hevc" if log_type in ["fcamera", "dcamera"] else "bz2"
return BASE_URL + "%s/%s/%s.%s" % (route_name.replace("|", "/"), segment_num, log_type, ext) return BASE_URL + f"{route_name.replace('|', '/')}/{segment_num}/{log_type}.{ext}"
def upload_file(path, name): def upload_file(path, name):
from azure.storage.blob import BlockBlobService # pylint: disable=import-error from azure.storage.blob import BlockBlobService # pylint: disable=import-error

View File

@ -31,7 +31,7 @@ SEND_EXTRA_INPUTS = bool(os.getenv("SEND_EXTRA_INPUTS", "0"))
def get_log_fn(ref_commit): def get_log_fn(ref_commit):
return "%s_%s_%s.bz2" % (TEST_ROUTE, "model_tici" if TICI else "model", ref_commit) return f"{TEST_ROUTE}_{'model_tici' if TICI else 'model'}_{ref_commit}.bz2"
def replace_calib(msg, calib): def replace_calib(msg, calib):

View File

@ -74,7 +74,7 @@ def test_process(cfg, lr, cmp_log_fn, ignore_fields=None, ignore_msgs=None):
break break
else: else:
segment = cmp_log_fn.split("/")[-1].split("_")[0] segment = cmp_log_fn.split("/")[-1].split("_")[0]
raise Exception("Route never enabled: %s" % segment) raise Exception(f"Route never enabled: {segment}")
try: try:
return compare_logs(cmp_log_msgs, log_msgs, ignore_fields+cfg.ignore, ignore_msgs, cfg.tolerance) return compare_logs(cmp_log_msgs, log_msgs, ignore_fields+cfg.ignore, ignore_msgs, cfg.tolerance)
@ -83,30 +83,30 @@ def test_process(cfg, lr, cmp_log_fn, ignore_fields=None, ignore_msgs=None):
def format_diff(results, ref_commit): def format_diff(results, ref_commit):
diff1, diff2 = "", "" diff1, diff2 = "", ""
diff2 += "***** tested against commit %s *****\n" % ref_commit diff2 += f"***** tested against commit {ref_commit} *****\n"
failed = False failed = False
for segment, result in list(results.items()): for segment, result in list(results.items()):
diff1 += "***** results for segment %s *****\n" % segment diff1 += f"***** results for segment {segment} *****\n"
diff2 += "***** differences for segment %s *****\n" % segment diff2 += f"***** differences for segment {segment} *****\n"
for proc, diff in list(result.items()): for proc, diff in list(result.items()):
diff1 += "\t%s\n" % proc diff1 += f"\t{proc}\n"
diff2 += "*** process: %s ***\n" % proc diff2 += f"*** process: {proc} ***\n"
if isinstance(diff, str): if isinstance(diff, str):
diff1 += "\t\t%s\n" % diff diff1 += f"\t\t{diff}\n"
failed = True failed = True
elif len(diff): elif len(diff):
cnt = {} cnt = {}
for d in diff: for d in diff:
diff2 += "\t%s\n" % str(d) diff2 += f"\t{str(d)}\n"
k = str(d[1]) k = str(d[1])
cnt[k] = 1 if k not in cnt else cnt[k] + 1 cnt[k] = 1 if k not in cnt else cnt[k] + 1
for k, v in sorted(cnt.items()): for k, v in sorted(cnt.items()):
diff1 += "\t\t%s: %s\n" % (k, v) diff1 += f"\t\t{k}: {v}\n"
failed = True failed = True
return diff1, diff2, failed return diff1, diff2, failed
@ -139,13 +139,13 @@ if __name__ == "__main__":
print("couldn't find reference commit") print("couldn't find reference commit")
sys.exit(1) sys.exit(1)
print("***** testing against commit %s *****" % ref_commit) print(f"***** testing against commit {ref_commit} *****")
# check to make sure all car brands are tested # check to make sure all car brands are tested
if FULL_TEST: if FULL_TEST:
tested_cars = set(c.lower() for c, _ in segments) tested_cars = set(c.lower() for c, _ in segments)
untested = (set(interface_names) - set(excluded_interfaces)) - tested_cars untested = (set(interface_names) - set(excluded_interfaces)) - tested_cars
assert len(untested) == 0, "Cars missing routes: %s" % (str(untested)) assert len(untested) == 0, f"Cars missing routes: {str(untested)}"
results: Any = {} results: Any = {}
for car_brand, segment in segments: for car_brand, segment in segments:
@ -153,7 +153,7 @@ if __name__ == "__main__":
(not cars_whitelisted and car_brand.upper() in args.blacklist_cars): (not cars_whitelisted and car_brand.upper() in args.blacklist_cars):
continue continue
print("***** testing route segment %s *****\n" % segment) print(f"***** testing route segment {segment} *****\n")
results[segment] = {} results[segment] = {}
@ -165,7 +165,7 @@ if __name__ == "__main__":
(not procs_whitelisted and cfg.proc_name in args.blacklist_procs): (not procs_whitelisted and cfg.proc_name in args.blacklist_procs):
continue continue
cmp_log_fn = os.path.join(process_replay_dir, "%s_%s_%s.bz2" % (segment, cfg.proc_name, ref_commit)) cmp_log_fn = os.path.join(process_replay_dir, f"{segment}_{cfg.proc_name}_{ref_commit}.bz2")
results[segment][cfg.proc_name] = test_process(cfg, lr, cmp_log_fn, args.ignore_fields, args.ignore_msgs) results[segment][cfg.proc_name] = test_process(cfg, lr, cmp_log_fn, args.ignore_fields, args.ignore_msgs)
diff1, diff2, failed = format_diff(results, ref_commit) diff1, diff2, failed = format_diff(results, ref_commit)

View File

@ -28,7 +28,7 @@ if __name__ == "__main__":
for cfg in CONFIGS: for cfg in CONFIGS:
log_msgs = replay_process(cfg, lr) log_msgs = replay_process(cfg, lr)
log_fn = os.path.join(process_replay_dir, "%s_%s_%s.bz2" % (segment, cfg.proc_name, ref_commit)) log_fn = os.path.join(process_replay_dir, f"{segment}_{cfg.proc_name}_{ref_commit}.bz2")
save_log(log_fn, log_msgs) save_log(log_fn, log_msgs)
if not no_upload: if not no_upload:

View File

@ -18,7 +18,7 @@ def _get_fingerprints():
for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]: for car_folder in [x[0] for x in os.walk(BASEDIR + '/selfdrive/car')]:
car_name = car_folder.split('/')[-1] car_name = car_folder.split('/')[-1]
try: try:
fingerprints[car_name] = __import__('selfdrive.car.%s.values' % car_name, fromlist=['FINGERPRINTS']).FINGERPRINTS fingerprints[car_name] = __import__(f'selfdrive.car.{car_name}.values', fromlist=['FINGERPRINTS']).FINGERPRINTS
except (ImportError, IOError, AttributeError): except (ImportError, IOError, AttributeError):
pass pass
@ -80,14 +80,14 @@ if __name__ == "__main__":
for idx2, f2 in enumerate(fingerprints_flat): for idx2, f2 in enumerate(fingerprints_flat):
if idx1 < idx2 and not check_fingerprint_consistency(f1, f2): if idx1 < idx2 and not check_fingerprint_consistency(f1, f2):
valid = False valid = False
print("Those two fingerprints are inconsistent {0} {1}".format(car_names[idx1], car_names[idx2])) print(f"Those two fingerprints are inconsistent {car_names[idx1]} {car_names[idx2]}")
print("") print("")
print(', '.join("%d: %d" % v for v in sorted(f1.items()))) print(', '.join("%d: %d" % v for v in sorted(f1.items())))
print("") print("")
print(', '.join("%d: %d" % v for v in sorted(f2.items()))) print(', '.join("%d: %d" % v for v in sorted(f2.items())))
print("") print("")
print("Found {0} individual fingerprints".format(len(fingerprints_flat))) print(f"Found {len(fingerprints_flat)} individual fingerprints")
if not valid or len(fingerprints_flat) == 0: if not valid or len(fingerprints_flat) == 0:
print("TEST FAILED") print("TEST FAILED")
sys.exit(1) sys.exit(1)

View File

@ -134,7 +134,7 @@ class TestCarModel(unittest.TestCase):
def test_radar_interface(self): def test_radar_interface(self):
os.environ['NO_RADAR_SLEEP'] = "1" os.environ['NO_RADAR_SLEEP'] = "1"
RadarInterface = importlib.import_module('selfdrive.car.%s.radar_interface' % self.CP.carName).RadarInterface RadarInterface = importlib.import_module(f'selfdrive.car.{self.CP.carName}.radar_interface').RadarInterface
RI = RadarInterface(self.CP) RI = RadarInterface(self.CP)
assert RI assert RI

View File

@ -25,7 +25,7 @@ def upload_route(path):
"azcopy", "azcopy",
"copy", "copy",
f"{path}/*", f"{path}/*",
"https://{}.blob.core.windows.net/{}/{}?{}".format(_DATA_ACCOUNT_CI, "openpilotci", destpath, DEST_KEY), f"https://{_DATA_ACCOUNT_CI}.blob.core.windows.net/openpilotci/{destpath}?{DEST_KEY}",
"--recursive=false", "--recursive=false",
"--overwrite=false", "--overwrite=false",
"--exclude-pattern=*/dcamera.hevc", "--exclude-pattern=*/dcamera.hevc",
@ -46,8 +46,8 @@ def sync_to_ci_public(route):
cmd = [ cmd = [
"azcopy", "azcopy",
"copy", "copy",
"https://{}.blob.core.windows.net/{}/{}?{}".format(source_account, source_bucket, key_prefix, source_key), f"https://{source_account}.blob.core.windows.net/{source_bucket}/{key_prefix}?{source_key}",
"https://{}.blob.core.windows.net/{}/{}?{}".format(_DATA_ACCOUNT_CI, "openpilotci", dongle_id, DEST_KEY), f"https://{_DATA_ACCOUNT_CI}.blob.core.windows.net/openpilotci/{dongle_id}?{DEST_KEY}",
"--recursive=true", "--recursive=true",
"--overwrite=false", "--overwrite=false",
"--exclude-pattern=*/dcamera.hevc", "--exclude-pattern=*/dcamera.hevc",

View File

@ -342,7 +342,7 @@ def fetch_update(wait_helper: WaitTimeHelper) -> bool:
new_version = cur_hash != upstream_hash new_version = cur_hash != upstream_hash
git_fetch_result = check_git_fetch_result(git_fetch_output) git_fetch_result = check_git_fetch_result(git_fetch_output)
cloudlog.info("comparing %s to %s" % (cur_hash, upstream_hash)) cloudlog.info(f"comparing {cur_hash} to {upstream_hash}")
if new_version or git_fetch_result: if new_version or git_fetch_result:
cloudlog.info("Running update") cloudlog.info("Running update")

View File

@ -115,9 +115,9 @@ if __name__ == "__main__":
params.put("TermsVersion", terms_version) params.put("TermsVersion", terms_version)
params.put("TrainingVersion", training_version) params.put("TrainingVersion", training_version)
print("Dirty: %s" % is_dirty()) print(f"Dirty: {is_dirty()}")
print("Version: %s" % get_version()) print(f"Version: {get_version()}")
print("Origin: %s" % get_origin()) print(f"Origin: {get_origin()}")
print("Branch: %s" % get_branch()) print(f"Branch: {get_branch()}")
print("Short branch: %s" % get_short_branch()) print(f"Short branch: {get_short_branch()}")
print("Prebuilt: %s" % is_prebuilt()) print(f"Prebuilt: {is_prebuilt()}")

View File

@ -50,7 +50,7 @@ def fingerprint_video(fn):
with FileReader(fn) as f: with FileReader(fn) as f:
header = f.read(4) header = f.read(4)
if len(header) == 0: if len(header) == 0:
raise DataUnreadableError("%s is empty" % fn) raise DataUnreadableError(f"{fn} is empty")
elif header == b"\x00\xc0\x12\x00": elif header == b"\x00\xc0\x12\x00":
return FrameType.raw return FrameType.raw
elif header == b"\x00\x00\x00\x01": elif header == b"\x00\x00\x00\x01":
@ -90,7 +90,7 @@ def vidindex(fn, typ):
try: try:
subprocess.check_call([vidindex, typ, fn, prefix_f.name, index_f.name]) subprocess.check_call([vidindex, typ, fn, prefix_f.name, index_f.name])
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
raise DataUnreadableError("vidindex failed on file %s" % fn) raise DataUnreadableError(f"vidindex failed on file {fn}")
with open(index_f.name, "rb") as f: with open(index_f.name, "rb") as f:
index = f.read() index = f.read()
with open(prefix_f.name, "rb") as f: with open(prefix_f.name, "rb") as f:
@ -308,7 +308,7 @@ class RawFrameReader(BaseFrameReader):
assert num+count <= self.frame_count assert num+count <= self.frame_count
if pix_fmt not in ("yuv420p", "rgb24"): if pix_fmt not in ("yuv420p", "rgb24"):
raise ValueError("Unsupported pixel format %r" % pix_fmt) raise ValueError(f"Unsupported pixel format {pix_fmt!r}")
app = [] app = []
for i in range(num, num+count): for i in range(num, num+count):
@ -548,10 +548,10 @@ class GOPFrameReader(BaseFrameReader):
assert self.frame_count is not None assert self.frame_count is not None
if num + count > self.frame_count: if num + count > self.frame_count:
raise ValueError("{} > {}".format(num + count, self.frame_count)) raise ValueError(f"{num + count} > {self.frame_count}")
if pix_fmt not in ("yuv420p", "rgb24", "yuv444p"): if pix_fmt not in ("yuv420p", "rgb24", "yuv444p"):
raise ValueError("Unsupported pixel format %r" % pix_fmt) raise ValueError(f"Unsupported pixel format {pix_fmt!r}")
ret = [self._get_one(num + i, pix_fmt) for i in range(count)] ret = [self._get_one(num + i, pix_fmt) for i in range(count)]

View File

@ -112,7 +112,7 @@ class Route(object):
if not seg_num.isdigit(): if not seg_num.isdigit():
continue continue
segment_name = '{}--{}'.format(self.route_name, seg_num) segment_name = f'{self.route_name}--{seg_num}'
for seg_f in os.listdir(os.path.join(fullpath, seg_num)): for seg_f in os.listdir(os.path.join(fullpath, seg_num)):
segment_files[segment_name].append((os.path.join(fullpath, seg_num, seg_f), seg_f)) segment_files[segment_name].append((os.path.join(fullpath, seg_num, seg_f), seg_f))
@ -152,7 +152,7 @@ class Route(object):
segments.append(RouteSegment(segment, log_path, qlog_path, camera_path, dcamera_path, ecamera_path, qcamera_path)) segments.append(RouteSegment(segment, log_path, qlog_path, camera_path, dcamera_path, ecamera_path, qcamera_path))
if len(segments) == 0: if len(segments) == 0:
raise ValueError('Could not find segments for route {} in data directory {}'.format(self.route_name, data_dir)) raise ValueError(f'Could not find segments for route {self.route_name} in data directory {data_dir}')
return sorted(segments, key=lambda seg: seg.canonical_name.segment_num) return sorted(segments, key=lambda seg: seg.canonical_name.segment_num)
class RouteSegment(object): class RouteSegment(object):

View File

@ -156,7 +156,7 @@ class URLFile(object):
if self._debug: if self._debug:
t2 = time.time() t2 = time.time()
if t2 - t1 > 0.1: if t2 - t1 > 0.1:
print("get %s %r %.f slow" % (self._url, headers, t2 - t1)) print(f"get {self._url} {headers!r} {t2 - t1:.f} slow")
response_code = c.getinfo(pycurl.RESPONSE_CODE) response_code = c.getinfo(pycurl.RESPONSE_CODE)
if response_code == 416: # Requested Range Not Satisfiable if response_code == 416: # Requested Range Not Satisfiable

View File

@ -152,7 +152,7 @@ def init_plots(arr, name_to_arr_idx, plot_xlims, plot_ylims, plot_names, plot_co
plots.append(plot) plots.append(plot)
idxs.append(name_to_arr_idx[item]) idxs.append(name_to_arr_idx[item])
plot_select.append(i) plot_select.append(i)
axs[i].set_title(", ".join("%s (%s)" % (nm, cl) axs[i].set_title(", ".join(f"{nm} ({cl})"
for (nm, cl) in zip(pl_list, plot_colors[i])), fontsize=10) for (nm, cl) in zip(pl_list, plot_colors[i])), fontsize=10)
axs[i].tick_params(axis="x", colors="white") axs[i].tick_params(axis="x", colors="white")
axs[i].tick_params(axis="y", colors="white") axs[i].tick_params(axis="y", colors="white")

View File

@ -2,7 +2,7 @@
import sys import sys
if len(sys.argv) < 4: if len(sys.argv) < 4:
print("%s <route> <segment> <frame number>" % sys.argv[0]) print(f"{sys.argv[0]} <route> <segment> <frame number>")
print('example: ./fetch_image_from_route.py "02c45f73a2e5c6e9|2020-06-01--18-03-08" 3 500') print('example: ./fetch_image_from_route.py "02c45f73a2e5c6e9|2020-06-01--18-03-08" 3 500')
exit(0) exit(0)
@ -33,5 +33,5 @@ if frame >= fr.frame_count:
im = Image.fromarray(fr.get(frame, count=1, pix_fmt="rgb24")[0]) im = Image.fromarray(fr.get(frame, count=1, pix_fmt="rgb24")[0])
fn = "uxxx_"+route.replace("|", "_")+"_%d_%d.png" % (segment, frame) fn = "uxxx_"+route.replace("|", "_")+"_%d_%d.png" % (segment, frame)
im.save(fn) im.save(fn)
print("saved %s" % fn) print(f"saved {fn}")

View File

@ -52,7 +52,7 @@ def main(argv):
i += 1 i += 1
except StopIteration: except StopIteration:
print('All done') print('All done')
print('Writed {} msgs'.format(i)) print(f'Writed {i} msgs')
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -7,7 +7,7 @@ import sys
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("%s <github username>" % sys.argv[0]) print(f"{sys.argv[0]} <github username>")
exit(1) exit(1)
username = sys.argv[1] username = sys.argv[1]

View File

@ -10,7 +10,7 @@ from typing import NoReturn
print('Available devices:') print('Available devices:')
for fn in os.listdir('/dev/input'): for fn in os.listdir('/dev/input'):
if fn.startswith('js'): if fn.startswith('js'):
print(' /dev/input/%s' % (fn)) print(f' /dev/input/{fn}')
# We'll store the states here. # We'll store the states here.
axis_states = {} axis_states = {}
@ -94,7 +94,7 @@ button_map = []
def wheel_poll_thread(q: 'Queue[str]') -> NoReturn: def wheel_poll_thread(q: 'Queue[str]') -> NoReturn:
# Open the joystick device. # Open the joystick device.
fn = '/dev/input/js0' fn = '/dev/input/js0'
print('Opening %s...' % fn) print(f'Opening {fn}...')
jsdev = open(fn, 'rb') jsdev = open(fn, 'rb')
# Get the device name. # Get the device name.
@ -102,7 +102,7 @@ def wheel_poll_thread(q: 'Queue[str]') -> NoReturn:
buf = array.array('B', [0] * 64) buf = array.array('B', [0] * 64)
ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len) ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
js_name = buf.tobytes().rstrip(b'\x00').decode('utf-8') js_name = buf.tobytes().rstrip(b'\x00').decode('utf-8')
print('Device name: %s' % js_name) print(f'Device name: {js_name}')
# Get number of axes and buttons. # Get number of axes and buttons.
buf = array.array('B', [0]) buf = array.array('B', [0])
@ -118,7 +118,7 @@ def wheel_poll_thread(q: 'Queue[str]') -> NoReturn:
ioctl(jsdev, 0x80406a32, buf) # JSIOCGAXMAP ioctl(jsdev, 0x80406a32, buf) # JSIOCGAXMAP
for _axis in buf[:num_axes]: for _axis in buf[:num_axes]:
axis_name = axis_names.get(_axis, 'unknown(0x%02x)' % _axis) axis_name = axis_names.get(_axis, f'unknown(0x{_axis:02x})')
axis_map.append(axis_name) axis_map.append(axis_name)
axis_states[axis_name] = 0.0 axis_states[axis_name] = 0.0
@ -127,7 +127,7 @@ def wheel_poll_thread(q: 'Queue[str]') -> NoReturn:
ioctl(jsdev, 0x80406a34, buf) # JSIOCGBTNMAP ioctl(jsdev, 0x80406a34, buf) # JSIOCGBTNMAP
for btn in buf[:num_buttons]: for btn in buf[:num_buttons]:
btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn) btn_name = button_names.get(btn, f'unknown(0x{btn:03x})')
button_map.append(btn_name) button_map.append(btn_name)
button_states[btn_name] = 0 button_states[btn_name] = 0
@ -153,19 +153,19 @@ def wheel_poll_thread(q: 'Queue[str]') -> NoReturn:
fvalue = value / 32767.0 fvalue = value / 32767.0
axis_states[axis] = fvalue axis_states[axis] = fvalue
normalized = (1 - fvalue) * 50 normalized = (1 - fvalue) * 50
q.put("throttle_%f" % normalized) q.put(f"throttle_{normalized:f}")
elif axis == "rz": # brake elif axis == "rz": # brake
fvalue = value / 32767.0 fvalue = value / 32767.0
axis_states[axis] = fvalue axis_states[axis] = fvalue
normalized = (1 - fvalue) * 50 normalized = (1 - fvalue) * 50
q.put("brake_%f" % normalized) q.put(f"brake_{normalized:f}")
elif axis == "x": # steer angle elif axis == "x": # steer angle
fvalue = value / 32767.0 fvalue = value / 32767.0
axis_states[axis] = fvalue axis_states[axis] = fvalue
normalized = fvalue normalized = fvalue
q.put("steer_%f" % normalized) q.put(f"steer_{normalized:f}")
elif mtype & 0x01: # buttons elif mtype & 0x01: # buttons
if value == 1: # press down if value == 1: # press down