Skip to content

Commit

Permalink
Replace black with ruff, now linting imports so removed unused from r…
Browse files Browse the repository at this point in the history
…equirements.txt (#90)
  • Loading branch information
nateinaction authored Jan 10, 2025
1 parent 8feda29 commit fc514a3
Show file tree
Hide file tree
Showing 23 changed files with 87 additions and 119 deletions.
12 changes: 8 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 3 additions & 4 deletions lib/pysquared/Big_Data.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions lib/pysquared/Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
1 change: 0 additions & 1 deletion lib/pysquared/battery_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
9 changes: 5 additions & 4 deletions lib/pysquared/cdh.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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


Expand Down
26 changes: 11 additions & 15 deletions lib/pysquared/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()

Expand All @@ -334,7 +333,6 @@ def get_imu_data(
tuple[float, float, float],
tuple[float, float, float],
]:

try:
data: list = []
data.append(self.cubesat.accel)
Expand All @@ -357,15 +355,13 @@ def OTA(self) -> None:
"""

def log_face_data(self, data) -> None:

self.debug_print("Logging Face Data")
try:
self.cubesat.log("/faces.txt", data)
except Exception as e:
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)
Expand Down
6 changes: 3 additions & 3 deletions lib/pysquared/packet_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand Down
44 changes: 25 additions & 19 deletions lib/pysquared/pysquared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion lib/pysquared/rv3028.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Authors: Nicole Maggard, Michael Pham, and Rachel Sarmiento
"""

import time
import adafruit_bus_device.i2c_device as i2c_device


Expand Down
2 changes: 0 additions & 2 deletions lib/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
adafruit-circuitpython-asyncio @ git+https://github.com/adafruit/[email protected]
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
Expand Down
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"""

import time

import microcontroller

import lib.pysquared.pysquared as pysquared

print("=" * 70)
Expand All @@ -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

Expand Down Expand Up @@ -96,22 +98,19 @@ 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()

f.Short_Hybernate()

######################### MAIN LOOP ##############################
try:

while True:
# L0 automatic tasks no matter the battery level
c.check_reboot()
Expand Down
2 changes: 1 addition & 1 deletion repl.py
Original file line number Diff line number Diff line change
@@ -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()
30 changes: 1 addition & 29 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions safemode.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit fc514a3

Please sign in to comment.