From fc514a307c59cfbd83929da8eb3bbe78d1cca199 Mon Sep 17 00:00:00 2001 From: Nate Gay Date: Fri, 10 Jan 2025 15:42:57 -0600 Subject: [PATCH] Replace black with ruff, now linting imports so removed unused from requirements.txt (#90) --- .pre-commit-config.yaml | 12 ++++++--- lib/pysquared/Big_Data.py | 7 +++--- lib/pysquared/Field.py | 5 ++-- lib/pysquared/battery_helper.py | 1 - lib/pysquared/cdh.py | 9 ++++--- lib/pysquared/functions.py | 26 +++++++++---------- lib/pysquared/packet_manager.py | 6 ++--- lib/pysquared/pysquared.py | 44 +++++++++++++++++++-------------- lib/pysquared/rv3028.py | 1 - lib/requirements.txt | 2 -- main.py | 9 +++---- repl.py | 2 +- requirements.txt | 30 +--------------------- safemode.py | 6 +++-- tests/repl/LiDARtest.py | 5 ++-- tests/repl/Yagi.py | 2 -- tests/repl/comms.py | 2 -- tests/repl/detumbletest.py | 6 ++--- tests/repl/echo.py | 4 +-- tests/repl/facetest.py | 12 ++++++--- tests/repl/fsk_test.py | 4 --- tests/repl/packet_receiver.py | 6 ++--- tests/repl/radio_test.py | 5 +--- 23 files changed, 87 insertions(+), 119 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 22702101..ed7c5d30 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,9 +7,13 @@ repos: - id: check-yaml - id: check-added-large-files -- repo: https://github.com/psf/black - rev: 24.4.2 - hooks: - - id: black +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.6 + hooks: + - id: ruff + args: [ --fix ] + - id: ruff + args: [ --fix, --select, I ] # import sorting + - id: ruff-format exclude: 'lib/adafruit_.*|lib/asyncio/.*|lib/neopixel\.py' diff --git a/lib/pysquared/Big_Data.py b/lib/pysquared/Big_Data.py index 95db3873..cb04c191 100755 --- a/lib/pysquared/Big_Data.py +++ b/lib/pysquared/Big_Data.py @@ -1,8 +1,7 @@ -from lib.pysquared.debugcolor import co -import time -import traceback import gc +from lib.pysquared.debugcolor import co + class Face: def __init__(self, Add, Pos, debug_state, tca): @@ -90,6 +89,6 @@ def Face_Test_All(self): temp = face.mcp.temperature if face.sensors.get("MCP") else None light = face.veml.lux if face.sensors.get("VEML") else None results.append([temp, light]) - except Exception as e: + except Exception: results.append([None, None]) return results diff --git a/lib/pysquared/Field.py b/lib/pysquared/Field.py index 6d39d539..09372ff2 100755 --- a/lib/pysquared/Field.py +++ b/lib/pysquared/Field.py @@ -4,13 +4,12 @@ Authors: Nicole Maggard, Michael Pham, and Rachel Sarmiento """ -import time -from lib.pysquared.debugcolor import co import traceback +from lib.pysquared.debugcolor import co + class Field: - def debug_print(self, statement): if self.debug: print(co("[Field]" + statement, "pink", "bold")) diff --git a/lib/pysquared/battery_helper.py b/lib/pysquared/battery_helper.py index d6530785..5050c78a 100644 --- a/lib/pysquared/battery_helper.py +++ b/lib/pysquared/battery_helper.py @@ -102,7 +102,6 @@ def _send_command(self, cmd): str: Response message or empty string on failure """ try: - # Send command self.uart.write(bytes(cmd.encode())) diff --git a/lib/pysquared/cdh.py b/lib/pysquared/cdh.py index 63ac250b..4aab42f1 100644 --- a/lib/pysquared/cdh.py +++ b/lib/pysquared/cdh.py @@ -1,7 +1,8 @@ -import time +import json import random +import time + import lib.pysquared.commandsConfig as commandsConfig -import json commands = commandsConfig.commands # parses json & assigns data to variables @@ -20,7 +21,7 @@ def hotstart_handler(cubesat, msg): try: cubesat.radio1.node = cubesat.cfg["id"] # this sat's radiohead ID cubesat.radio1.destination = cubesat.cfg["gs"] # target gs radiohead ID - except: + except Exception: pass # check that message is for me if msg[0] == cubesat.radio1.node: @@ -109,7 +110,7 @@ def hreset(cubesat): cubesat.radio1.send(data=b"resetting") cubesat.micro.on_next_reset(cubesat.micro.RunMode.NORMAL) cubesat.micro.reset() - except: + except Exception: pass diff --git a/lib/pysquared/functions.py b/lib/pysquared/functions.py index c797dc16..9e406c9a 100755 --- a/lib/pysquared/functions.py +++ b/lib/pysquared/functions.py @@ -5,27 +5,29 @@ Authors: Nicole Maggard, Michael Pham, and Rachel Sarmiento """ -import time -import alarm import gc -import traceback -import random import json -from lib.pysquared.debugcolor import co +import random +import time +import traceback + +import alarm + from lib.pysquared.battery_helper import BatteryHelper +from lib.pysquared.debugcolor import co from lib.pysquared.packet_manager import PacketManager from lib.pysquared.packet_sender import PacketSender try: - from typing import List, Dict, OrderedDict, Literal, Union, Any + from typing import Any, List, Literal, OrderedDict, Union + import circuitpython_typing -except: +except Exception: pass from lib.pysquared.pysquared import Satellite class functions: - def debug_print(self, statement: Any) -> None: if self.debug: print(co("[Functions]" + str(statement), "green", "bold")) @@ -70,7 +72,6 @@ def safe_sleep(self, duration: int = 15) -> None: iterations: int = 0 while duration > 15 and iterations < 12: - time_alarm: circuitpython_typing.Alarm = alarm.time.TimeAlarm( monotonic_time=time.monotonic() + 15 ) @@ -109,7 +110,7 @@ def send(self, msg: Union[str, bytearray]) -> None: message: str = f"{self.callsign} " + str(msg) + f" {self.callsign}" self.field.Beacon(message) if self.cubesat.is_licensed: - self.debug_print(f"Sent Packet: " + message) + self.debug_print("Sent Packet: " + message) else: self.debug_print("Failed to send packet") del self.field @@ -290,7 +291,6 @@ def listen_joke(self) -> bool: """ def all_face_data(self) -> list: - # self.cubesat.all_faces_on() self.debug_print(gc.mem_free()) gc.collect() @@ -317,7 +317,6 @@ def all_face_data(self) -> list: def get_battery_data( self, ) -> Union[tuple[float, float, float, float, bool, float], None]: - try: return self.battery.get_power_metrics() @@ -334,7 +333,6 @@ def get_imu_data( tuple[float, float, float], tuple[float, float, float], ]: - try: data: list = [] data.append(self.cubesat.accel) @@ -357,7 +355,6 @@ def OTA(self) -> None: """ def log_face_data(self, data) -> None: - self.debug_print("Logging Face Data") try: self.cubesat.log("/faces.txt", data) @@ -365,7 +362,6 @@ def log_face_data(self, data) -> None: self.debug_print("SD error: " + "".join(traceback.format_exception(e))) def log_error_data(self, data) -> None: - self.debug_print("Logging Error Data") try: self.cubesat.log("/error.txt", data) diff --git a/lib/pysquared/packet_manager.py b/lib/pysquared/packet_manager.py index ae1787f3..d4c6ef5b 100644 --- a/lib/pysquared/packet_manager.py +++ b/lib/pysquared/packet_manager.py @@ -17,13 +17,13 @@ def create_retransmit_request(self, missing_packets): - 2 bytes: Number of missing packets - Remaining bytes: Missing packet sequence numbers """ - header = b"\xFF\xFF" + len(missing_packets).to_bytes(2, "big") + header = b"\xff\xff" + len(missing_packets).to_bytes(2, "big") payload = b"".join(seq.to_bytes(2, "big") for seq in missing_packets) return header + payload def is_retransmit_request(self, packet): """Check if packet is a retransmit request""" - return len(packet) >= 4 and packet[:2] == b"\xFF\xFF" + return len(packet) >= 4 and packet[:2] == b"\xff\xff" def parse_retransmit_request(self, packet): """Extract missing packet numbers from retransmit request""" @@ -85,7 +85,7 @@ def unpack_data(self, packets): # Sort packets by sequence number try: packets = sorted(packets, key=lambda p: int.from_bytes(p[:2], "big")) - except: + except Exception: return None # Verify all packets are present diff --git a/lib/pysquared/pysquared.py b/lib/pysquared/pysquared.py index 8ade8441..be5d6784 100755 --- a/lib/pysquared/pysquared.py +++ b/lib/pysquared/pysquared.py @@ -8,33 +8,39 @@ """ # Common CircuitPython Libs -import gc -import board, machine, microcontroller -import busio, time, sys, traceback -from storage import mount, umount, VfsFat -import digitalio, sdcardio, pwmio -from os import listdir, stat, statvfs, mkdir, chdir -from lib.pysquared.bitflags import bitFlag, multiBitFlag, multiByte -from micropython import const -from lib.pysquared.debugcolor import co +import json +import sys +import time +import traceback from collections import OrderedDict +from os import chdir, mkdir, stat + +import board +import busio +import digitalio +import machine +import microcontroller +import sdcardio +from micropython import const +from storage import VfsFat, mount, umount -# Hardware Specific Libs -from lib.adafruit_rfm import rfm9x, rfm9xfsk # Radio -import lib.neopixel as neopixel # RGB LED -from lib.adafruit_lsm6ds.lsm6dsox import LSM6DSOX # IMU import lib.adafruit_lis2mdl as adafruit_lis2mdl # Magnetometer import lib.adafruit_tca9548a as adafruit_tca9548a # I2C Multiplexer +import lib.neopixel as neopixel # RGB LED import lib.pysquared.rv3028 as rv3028 # Real Time Clock +from lib.adafruit_lsm6ds.lsm6dsox import LSM6DSOX # IMU -import json - +# Hardware Specific Libs +from lib.adafruit_rfm import rfm9x, rfm9xfsk # Radio +from lib.pysquared.bitflags import bitFlag, multiBitFlag +from lib.pysquared.debugcolor import co # Importing typing libraries try: - from typing import List, Dict, OrderedDict, Literal, Union, Any, TextIO + from typing import Any, OrderedDict, TextIO, Union + import circuitpython_typing -except: +except Exception: pass @@ -718,7 +724,7 @@ def log(self, filedir: str, msg: str) -> None: def print_file(self, filedir: str = None, binary: bool = False) -> None: try: - if filedir == None: + if filedir is None: raise Exception("file directory is empty") self.debug_print(f"--- Printing File: {filedir} ---") if binary: @@ -738,7 +744,7 @@ def read_file( self, filedir: str = None, binary: bool = False ) -> Union[bytes, TextIO, None]: try: - if filedir == None: + if filedir is None: raise Exception("file directory is empty") self.debug_print(f"--- reading File: {filedir} ---") if binary: diff --git a/lib/pysquared/rv3028.py b/lib/pysquared/rv3028.py index fb1605ab..7515876b 100644 --- a/lib/pysquared/rv3028.py +++ b/lib/pysquared/rv3028.py @@ -4,7 +4,6 @@ Authors: Nicole Maggard, Michael Pham, and Rachel Sarmiento """ -import time import adafruit_bus_device.i2c_device as i2c_device diff --git a/lib/requirements.txt b/lib/requirements.txt index 5342d736..631e6e61 100644 --- a/lib/requirements.txt +++ b/lib/requirements.txt @@ -1,11 +1,9 @@ adafruit-circuitpython-asyncio @ git+https://github.com/adafruit/adafruit_circuitpython_asyncio@1.3.3 adafruit-circuitpython-drv2605==1.3.4 adafruit-circuitpython-lis2mdl==2.1.23 -adafruit-circuitpython-lsm303-accel==1.1.22 adafruit-circuitpython-lsm6ds==4.5.13 adafruit-circuitpython-mcp9808==3.3.24 adafruit-circuitpython-neopixel==6.3.12 -adafruit-circuitpython-pca9685==3.4.16 adafruit-circuitpython-register==1.10.1 adafruit-circuitpython-rfm==1.0.3 adafruit-circuitpython-tca9548a==0.7.4 diff --git a/main.py b/main.py index 12adb9d5..76c3ca22 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,9 @@ """ import time + import microcontroller + import lib.pysquared.pysquared as pysquared print("=" * 70) @@ -29,9 +31,9 @@ c = pysquared.Satellite() c.watchdog_pet() - import asyncio - import traceback import gc # Garbage collection + import traceback + import lib.pysquared.functions as functions from lib.pysquared.debugcolor import co @@ -96,14 +98,12 @@ def main(): f.listen_loiter() def critical_power_operations(): - initial_boot() c.watchdog_pet() f.Long_Hybernate() def minimum_power_operations(): - initial_boot() c.watchdog_pet() @@ -111,7 +111,6 @@ def minimum_power_operations(): ######################### MAIN LOOP ############################## try: - while True: # L0 automatic tasks no matter the battery level c.check_reboot() diff --git a/repl.py b/repl.py index 3f0f750b..f52b1ac7 100644 --- a/repl.py +++ b/repl.py @@ -1,4 +1,4 @@ -print("Initializing a cubesat object as `c` in the REPL...") import lib.pysquared.pysquared as pysquared +print("Initializing a cubesat object as `c` in the REPL...") c = pysquared.Satellite() diff --git a/requirements.txt b/requirements.txt index 68a7cba4..5be13572 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,31 +1,3 @@ -Adafruit-Blinka==8.50.0 -adafruit-circuitpython-busdevice==5.2.10 -adafruit-circuitpython-connectionmanager==3.1.2 -adafruit-circuitpython-requests==4.1.8 adafruit-circuitpython-typing==1.11.2 -Adafruit-PlatformDetect==3.75.0 -Adafruit-PureIO==1.1.11 -binho-host-adapter==0.1.6 -black==24.4.2 -cfgv==3.4.0 -click==8.1.7 -distlib==0.3.8 -filelock==3.15.4 -identify==2.6.0 -exceptiongroup==1.2.2 -iniconfig==2.0.0 -mypy-extensions==1.0.0 -nodeenv==1.9.1 -packaging==24.1 -pathspec==0.12.1 -platformdirs==4.2.2 -pre-commit==3.8.0 -pyftdi==0.56.0 -pyserial==3.5 -pyusb==1.2.1 -PyYAML==6.0.1 -pluggy==1.5.0 +pre-commit==4.0.1 pytest==8.3.2 -tomli==2.0.1 -typing_extensions==4.12.2 -virtualenv==20.26.3 diff --git a/safemode.py b/safemode.py index 8a0f6f32..0d4496a9 100644 --- a/safemode.py +++ b/safemode.py @@ -1,6 +1,8 @@ -print("I am in safemode. Help!") -import microcontroller import time +import microcontroller + +print("I am in safemode. Help!") + time.sleep(10) microcontroller.reset() diff --git a/tests/repl/LiDARtest.py b/tests/repl/LiDARtest.py index 785ee4f9..683299da 100644 --- a/tests/repl/LiDARtest.py +++ b/tests/repl/LiDARtest.py @@ -1,7 +1,8 @@ -import board -import busio import time + import adafruit_vl6180x # LiDAR Distance Sensor for Antenna +import board +import busio import neopixel # RGB LED i2c = busio.I2C(board.SCL1, board.SDA1) diff --git a/tests/repl/Yagi.py b/tests/repl/Yagi.py index 9d735706..5bbec866 100644 --- a/tests/repl/Yagi.py +++ b/tests/repl/Yagi.py @@ -1,9 +1,7 @@ -import time from pysquared import cubesat class Yagi: - def __init__(self): self.cubesat = cubesat self.cubesat.radio1.spreading_factor = 8 diff --git a/tests/repl/comms.py b/tests/repl/comms.py index ae732189..c11b45d3 100644 --- a/tests/repl/comms.py +++ b/tests/repl/comms.py @@ -1,9 +1,7 @@ -import time from pysquared import cubesat class Field: - def __init__(self): self.testMsg = [] self.testMsg.append("Hello There!") diff --git a/tests/repl/detumbletest.py b/tests/repl/detumbletest.py index 07f2f87e..f3105a89 100644 --- a/tests/repl/detumbletest.py +++ b/tests/repl/detumbletest.py @@ -1,10 +1,8 @@ -from pysquared import cubesat as c -import asyncio import time -import traceback -import gc # Garbage collection + import functions from debugcolor import co +from pysquared import cubesat as c def debug_print(statement): diff --git a/tests/repl/echo.py b/tests/repl/echo.py index bed2d49d..7b41d50f 100644 --- a/tests/repl/echo.py +++ b/tests/repl/echo.py @@ -1,6 +1,7 @@ -from pycubed import cubesat import time +from pycubed import cubesat + yagiMode = True # testMsg = "According to all known laws of aviation, there is no way a bee should be able to fly." # testMsg = "Its over Anakin I have the high ground!" @@ -90,7 +91,6 @@ def banger(): while True: - # banger() if yagiMode: diff --git a/tests/repl/facetest.py b/tests/repl/facetest.py index 1e494905..7203361d 100644 --- a/tests/repl/facetest.py +++ b/tests/repl/facetest.py @@ -1,7 +1,13 @@ +import time + +import adafruit_drv2605 +import adafruit_mcp9808 +import adafruit_pca9685 +import adafruit_tca9548a +import adafruit_veml7700 import board import busio -import time -import adafruit_pca9685, adafruit_tca9548a, adafruit_veml7700, adafruit_mcp9808, adafruit_drv2605, ina219 +import ina219 i2c = busio.I2C(board.SCL0, board.SDA0) pca = adafruit_pca9685.PCA9685(i2c, address=86) @@ -16,7 +22,7 @@ mcp1 = adafruit_mcp9808.MCP9808(tca[1], address=27) try: drv1 = adafruit_drv2605.DRV2605(tca[1]) -except: +except Exception: drv1 = adafruit_drv2605.DRV2605(tca[1], address=95) ina = ina219.INA219(tca[5], addr=64) drv0.sequence[0] = adafruit_drv2605.Effect(47) diff --git a/tests/repl/fsk_test.py b/tests/repl/fsk_test.py index e4c618d6..c5912978 100644 --- a/tests/repl/fsk_test.py +++ b/tests/repl/fsk_test.py @@ -1,7 +1,3 @@ -import board -import busio -import digitalio -import time from pysquared import cubesat test_message = "Hello There!" diff --git a/tests/repl/packet_receiver.py b/tests/repl/packet_receiver.py index 89c34063..32b3fa2b 100644 --- a/tests/repl/packet_receiver.py +++ b/tests/repl/packet_receiver.py @@ -198,7 +198,7 @@ def send_retransmit_request(self, missing_packets): # Wait for retransmitted packets start_time = time.monotonic() original_missing = set(missing_packets) - last_receive_time = start_time + # last_receive_time = start_time print("Waiting for retransmitted packets...") while time.monotonic() - start_time < retransmit_timeout: @@ -207,7 +207,7 @@ def send_retransmit_request(self, missing_packets): time.sleep(0.5) if packet: - last_receive_time = time.monotonic() + # last_receive_time = time.monotonic() try: seq_num = int.from_bytes(packet[:2], "big") if seq_num in original_missing: @@ -293,7 +293,7 @@ def fast_receive_until_complete( if packet: try: seq_num = int.from_bytes(packet[:2], "big") - packet_total = int.from_bytes(packet[2:4], "big") + # packet_total = int.from_bytes(packet[2:4], "big") if seq_num not in self.received_packets: self.received_packets[seq_num] = packet diff --git a/tests/repl/radio_test.py b/tests/repl/radio_test.py index ffc4e71d..b25bd53b 100755 --- a/tests/repl/radio_test.py +++ b/tests/repl/radio_test.py @@ -5,8 +5,8 @@ # The is a test script to facilitate a simple ping pong style communications test between two radios. import time + from pysquared import cubesat -from time import sleep test_message = "Hello There!" debug_mode = True @@ -72,7 +72,6 @@ def debug_print(message): def device_under_test(attempts): - debug_print("Device Under Test Selected") debug_print("Setting up Radio...") @@ -101,7 +100,6 @@ def device_under_test(attempts): def receiver(): - debug_print("Receiver Selected") debug_print("Setting up Radio...") @@ -273,7 +271,6 @@ def handle_ping(): ) while True: - if device_selection == "A": time.sleep(1) device_under_test(number_of_attempts)