From 89cb90a9e1fd01269b78e57fcad2fcd1be5e3ae1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 18 Feb 2025 12:07:14 +0100 Subject: [PATCH] all: Fix ruff rules C4, PERF401, and PLC0206. Signed-off-by: Christian Clauss --- examples/bluetooth/ble_advertising.py | 16 +++++++++------- .../esp32/boards/UM_TINYPICO/modules/dotstar.py | 10 ++++------ ports/nrf/boards/make-pins.py | 4 ++-- ports/nrf/examples/powerup.py | 11 ++++++----- ports/nrf/examples/ubluepy_scan.py | 11 ++++++----- ports/stm32/boards/plli2svalues.py | 4 ++-- ports/stm32/boards/pllvalues.py | 4 ++-- ports/stm32/make-stmconst.py | 6 ++++-- tools/codeformat.py | 2 +- tools/dfu.py | 4 ++-- tools/insert-usb-ids.py | 2 +- tools/mpy-tool.py | 8 ++------ tools/uf2conv.py | 7 +++---- 13 files changed, 44 insertions(+), 45 deletions(-) diff --git a/examples/bluetooth/ble_advertising.py b/examples/bluetooth/ble_advertising.py index 2fe17d640b9d2..5a819c0782a8e 100644 --- a/examples/bluetooth/ble_advertising.py +++ b/examples/bluetooth/ble_advertising.py @@ -78,13 +78,15 @@ def decode_name(payload): def decode_services(payload): - services = [] - for u in decode_field(payload, _ADV_TYPE_UUID16_COMPLETE): - services.append(bluetooth.UUID(struct.unpack("= self._n or index < 0: diff --git a/ports/nrf/boards/make-pins.py b/ports/nrf/boards/make-pins.py index 30da035589daf..847e6fd35117d 100644 --- a/ports/nrf/boards/make-pins.py +++ b/ports/nrf/boards/make-pins.py @@ -299,7 +299,7 @@ def print_header(self, out_header): print("extern const pin_obj_t * const pin_adc3[];", file=out_header) def print_af_hdr(self, out_af_const): - af_hdr_set = set([]) + af_hdr_set = set() mux_name_width = 0 for named_pin in self.cpu_pins: pin = named_pin.pin() @@ -307,7 +307,7 @@ def print_af_hdr(self, out_af_const): for af in pin.alt_fn: if af.is_supported(): mux_name = af.mux_name() - af_hdr_set |= set([mux_name]) + af_hdr_set |= {mux_name} if len(mux_name) > mux_name_width: mux_name_width = len(mux_name) for mux_name in sorted(af_hdr_set): diff --git a/ports/nrf/examples/powerup.py b/ports/nrf/examples/powerup.py index 156193d002f8a..d6ee23de38e43 100644 --- a/ports/nrf/examples/powerup.py +++ b/ports/nrf/examples/powerup.py @@ -52,11 +52,12 @@ def bytes_to_str(bytes): def get_device_names(scan_entries): dev_names = [] for e in scan_entries: - scan = e.getScanData() - if scan: - for s in scan: - if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME: - dev_names.append((e, bytes_to_str(s[2]))) + scan = e.getScanData() or [] + dev_names.extend( + (e, bytes_to_str(s[2])) + for s in scan + if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME + ) return dev_names diff --git a/ports/nrf/examples/ubluepy_scan.py b/ports/nrf/examples/ubluepy_scan.py index 37daa6c69baaf..3780755a4da14 100644 --- a/ports/nrf/examples/ubluepy_scan.py +++ b/ports/nrf/examples/ubluepy_scan.py @@ -11,11 +11,12 @@ def bytes_to_str(bytes): def get_device_names(scan_entries): dev_names = [] for e in scan_entries: - scan = e.getScanData() - if scan: - for s in scan: - if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME: - dev_names.append((e, bytes_to_str(s[2]))) + scan = e.getScanData() or [] + dev_names.extend( + (e, bytes_to_str(s[2])) + for s in scan + if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME + ) return dev_names diff --git a/ports/stm32/boards/plli2svalues.py b/ports/stm32/boards/plli2svalues.py index e5ea4e8dfd49b..0418f4f981ec5 100644 --- a/ports/stm32/boards/plli2svalues.py +++ b/ports/stm32/boards/plli2svalues.py @@ -198,9 +198,9 @@ def main(): # Select MCU parameters mcu = mcu_default - for m in mcu_table: + for m, new_mcu in mcu_table.items(): if mcu_series.startswith(m): - mcu = mcu_table[m] + mcu = new_mcu break plli2s_table = compute_plli2s_table(hse, pllm) if c_table: diff --git a/ports/stm32/boards/pllvalues.py b/ports/stm32/boards/pllvalues.py index d8856bfecdbd4..0a714f233e885 100644 --- a/ports/stm32/boards/pllvalues.py +++ b/ports/stm32/boards/pllvalues.py @@ -287,9 +287,9 @@ def main(): # Select MCU parameters mcu = mcu_default - for m in mcu_table: + for m, new_mcu in mcu_table.items(): if mcu_series.startswith(m): - mcu = mcu_table[m] + mcu = new_mcu break # Relax constraint on PLLQ being 48MHz on MCUs which have separate PLLs for 48MHz diff --git a/ports/stm32/make-stmconst.py b/ports/stm32/make-stmconst.py index 4ef6143bda560..bf86dac4badd8 100644 --- a/ports/stm32/make-stmconst.py +++ b/ports/stm32/make-stmconst.py @@ -158,8 +158,10 @@ def parse_file(filename): if m[0] == "IO reg": regs.append((reg, offset, bits, comment)) else: - for i in range(int(d["array"])): - regs.append((reg + str(i), offset + i * bits // 8, bits, comment)) + regs.extend( + (reg + str(i), offset + i * bits // 8, bits, comment) + for i in range(int(d["array"])) + ) m = lexer.next_match() if m[0] in ("}", "} _t"): pass diff --git a/tools/codeformat.py b/tools/codeformat.py index c65174ddc1278..f556df2a2ab6e 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -151,7 +151,7 @@ def main(): # Filter against the default list of files. This is a little fiddly # because we need to apply both the inclusion globs given in PATHS # as well as the EXCLUSIONS, and use absolute paths - files = set(os.path.abspath(f) for f in files) + files = {os.path.abspath(f) for f in files} all_files = set(list_files(PATHS, EXCLUSIONS, TOP)) if args.v: # In verbose mode, log any files we're skipping for f in files - all_files: diff --git a/tools/dfu.py b/tools/dfu.py index 39d68f3b660c4..5bd550117ee45 100755 --- a/tools/dfu.py +++ b/tools/dfu.py @@ -87,7 +87,7 @@ def build(file, targets, device=DEFAULT_DEVICE): ) data += tdata data = struct.pack("<5sBIB", b"DfuSe", 1, len(data) + 11, len(targets)) + data - v, d = map(lambda x: int(x, 0) & 0xFFFF, device.split(":", 1)) + v, d = (int(x, 0) & 0xFFFF for x in device.split(":", 1)) data += struct.pack("<4H3sB", 0, d, v, 0x011A, b"UFD", 16) crc = compute_crc(data) data += struct.pack("