Skip to content

Commit

Permalink
Ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil21 committed Sep 16, 2024
1 parent 1a35e67 commit 8f98845
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion airbrakes/imu/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IMUDataProcessor:
:param data_points: A sequence of EstimatedDataPacket objects to process.
"""

__slots__ = ("_avg_accel", "_avg_accel_mag", "_max_altitude", "_data_points")
__slots__ = ("_avg_accel", "_avg_accel_mag", "_data_points", "_max_altitude")

def __init__(self, data_points: Sequence[EstimatedDataPacket]):
self._avg_accel: tuple[float, float, float] = (0.0, 0.0, 0.0)
Expand Down
3 changes: 1 addition & 2 deletions airbrakes/servo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class Servo:

__slots__ = ("current_extension", "max_extension", "min_extension", "servo")

def __init__(self, gpio_pin_number: int, min_extension: float, max_extension: float,
pin_factory=None):
def __init__(self, gpio_pin_number: int, min_extension: float, max_extension: float, pin_factory=None):
self.min_extension = min_extension
self.max_extension = max_extension
self.current_extension = 0.0
Expand Down
2 changes: 1 addition & 1 deletion airbrakes/state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module for the finite state machine that represents which state of flight we are in."""

from abc import ABC, abstractmethod
from typing import override, TYPE_CHECKING
from typing import TYPE_CHECKING, override

if TYPE_CHECKING:
from airbrakes.airbrakes import AirbrakesContext
Expand Down
15 changes: 8 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
"""Module where fixtures are shared between all test files."""

import pytest
from pathlib import Path

from airbrakes.constants import FREQUENCY, PORT, UPSIDE_DOWN, SERVO_PIN, MIN_EXTENSION, MAX_EXTENSION
import pytest
from gpiozero.pins.mock import MockFactory, MockPWMPin

from airbrakes.constants import FREQUENCY, MAX_EXTENSION, MIN_EXTENSION, PORT, SERVO_PIN, UPSIDE_DOWN
from airbrakes.imu.imu import IMU
from airbrakes.logger import Logger
from airbrakes.servo import Servo


from gpiozero.pins.mock import MockFactory, MockPWMPin


LOG_PATH = Path("tests/logs")


@pytest.fixture
def logger():
return Logger(LOG_PATH)


@pytest.fixture
def imu():
return IMU(port=PORT, frequency=FREQUENCY, upside_down=UPSIDE_DOWN)


@pytest.fixture
def servo():
return Servo(SERVO_PIN, MIN_EXTENSION, MAX_EXTENSION, pin_factory=MockFactory(pin_class=MockPWMPin))
return Servo(SERVO_PIN, MIN_EXTENSION, MAX_EXTENSION, pin_factory=MockFactory(pin_class=MockPWMPin))
2 changes: 1 addition & 1 deletion tests/test_airbrakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_slots(self, airbrakes_context):
def test_init(self, airbrakes_context, logger, imu, servo):
assert airbrakes_context.logger == logger
assert airbrakes_context.servo == servo
assert airbrakes_context.imu == imu
assert airbrakes_context.imu == imu
32 changes: 18 additions & 14 deletions tests/test_data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ def test_str(self, data_processor):

def test_update_data(self, data_processor):
d = data_processor
d.update_data([
EstimatedDataPacket(
1, estCompensatedAccelX=1, estCompensatedAccelY=2, estCompensatedAccelZ=3, estPressureAlt=20
),
EstimatedDataPacket(
2, estCompensatedAccelX=2, estCompensatedAccelY=3, estCompensatedAccelZ=4, estPressureAlt=30
),
])
d.update_data(
[
EstimatedDataPacket(
1, estCompensatedAccelX=1, estCompensatedAccelY=2, estCompensatedAccelZ=3, estPressureAlt=20
),
EstimatedDataPacket(
2, estCompensatedAccelX=2, estCompensatedAccelY=3, estCompensatedAccelZ=4, estPressureAlt=30
),
]
)
assert d._avg_accel == (1.5, 2.5, 3.5) == d.avg_acceleration
assert d._avg_accel_mag == math.sqrt(1.5**2 + 2.5**2 + 3.5**2) == d.avg_acceleration_mag
assert d.avg_acceleration_z == 3.5
Expand All @@ -87,10 +89,12 @@ def test_max_altitude(self, data_processor):
altitudes = simulate_altitude_sine_wave(n_points=1000)
# run update_data every 10 packets, to simulate actual data processing in real time:
for i in range(0, len(altitudes), 10):
d.update_data([
EstimatedDataPacket(
i, estCompensatedAccelX=1, estCompensatedAccelY=2, estCompensatedAccelZ=3, estPressureAlt=alt
)
for alt in altitudes[i : i + 10]
])
d.update_data(
[
EstimatedDataPacket(
i, estCompensatedAccelX=1, estCompensatedAccelY=2, estCompensatedAccelZ=3, estPressureAlt=alt
)
for alt in altitudes[i : i + 10]
]
)
assert d.max_altitude == max(altitudes)
6 changes: 0 additions & 6 deletions tests/test_imu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
import time
from collections import deque

import pytest

from airbrakes.constants import FREQUENCY, PORT, UPSIDE_DOWN
from airbrakes.imu.imu import IMU
from airbrakes.imu.imu_data_packet import EstimatedDataPacket, IMUDataPacket, RawDataPacket





RAW_DATA_PACKET_SAMPLING_RATE = 1 / 1000 # 1kHz
EST_DATA_PACKET_SAMPLING_RATE = 1 / 500 # 500Hz

Expand Down
1 change: 0 additions & 1 deletion tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
class TestLogger:
"""Tests the Logger() class in logger.py"""


@pytest.fixture(autouse=True) # autouse=True means run this function before/after every test
def clear_directory(self):
"""Clear the tests/logs directory after running each test."""
Expand Down

0 comments on commit 8f98845

Please sign in to comment.