Skip to content

Commit

Permalink
Fixed linting errors detected by Flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalRombach committed Nov 15, 2023
1 parent 8dc8b55 commit d49ae5f
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 445 deletions.
254 changes: 140 additions & 114 deletions anki/control/controller.py

Large diffs are not rendered by default.

52 changes: 28 additions & 24 deletions anki/control/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
import abc
from typing import TypeVar


def reorder_map(map: list[TrackPiece]):
# Basically: Move the last piece to the front until START is at index 0 and FINISH is at index -1 (i.e. the end)
# Basically: Move the last piece to the front until START
# is at index 0 and FINISH is at index -1 (i.e. the end)
while not (map[0].type is TrackPieceType.START and map[-1].type is TrackPieceType.FINISH):
map.insert(0,map.pop(-1))
map.insert(0, map.pop(-1))
pass
pass


class BaseScanner(abc.ABC):
"""Abstract base class for all custom scanners.
Any subclasses of this must override the methods
Any subclasses of this must override the methods
:meth:`BaseScanner.scan` :meth:`BaseScanner.align`.
:param vehicle: :class:`Vehicle`
The vehicle used for the scanning operation.
"""

__slots__ = ("vehicle","map")
__slots__ = ("vehicle", "map")

def __init__(self, vehicle: Vehicle):
self.vehicle = vehicle
self.map: list[TrackPiece] = []
Expand All @@ -31,7 +35,7 @@ def __init__(self, vehicle: Vehicle):
async def scan(self) -> list[TrackPiece]:
"""
This method should scan in the map using various functionalities.
The returned list of track pieces should begin with a type of
The returned list of track pieces should begin with a type of
`TrackPieceType.START` and end with `TrackPieceType.FINISH`.
Returns
Expand All @@ -45,11 +49,11 @@ async def scan(self) -> list[TrackPiece]:

@abc.abstractmethod
async def align(
self,
self,
vehicle: Vehicle,
*,
target_previous_track_piece_type: TrackPieceType=TrackPieceType.FINISH
) -> None:
) -> None:
"""
This method should be used to align a vehicle to the START piece.
It is required for this method to work without a functional scan.
Expand All @@ -64,6 +68,7 @@ async def align(
------
"""


class Scanner(BaseScanner):
"""A scanner object performs a simple map scan without any alignment.
Expand All @@ -73,50 +78,48 @@ class Scanner(BaseScanner):

async def scan(self) -> list[TrackPiece]:
"""Perform the scan"""
completed = [False] # In a list because of global local issues
track_types = []
# This keeps track of the types we've visited
# (could also be a set,
completed = [False] # In a list because of global local issues
track_types = []
# This keeps track of the types we've visited
# (could also be a set,
# but TrackPieceType didn't have hashes back then)

def watcher():
track = self.vehicle._current_track_piece
if track is not None:
if track is not None:
# track might be None for the first time this event is called
self.map.append(track)
track_types.append(track.type)
if TrackPieceType.START in track_types and TrackPieceType.FINISH in track_types:
# This marks the scan as complete
if TrackPieceType.START in track_types and TrackPieceType.FINISH in track_types:
# This marks the scan as complete
# once both START and FINISH have been found
completed[0] = True
pass
pass
pass

self.vehicle.on_track_piece_change = watcher

await self.vehicle.set_speed(300)
while not completed[0]:
# Drive along until the scan is marked as complete.
# (This does NOT cause parallelity issues because we're
while not completed[0]:
# Drive along until the scan is marked as complete.
# (This does NOT cause parallelity issues because we're
# running the watcher synchronously in the background)
await asyncio.sleep(0.5)
pass

self.vehicle.on_track_piece_change = lambda: None
await self.vehicle.stop()

reorder_map(self.map)
reorder_map(self.map)
# Assure that START is at the beginning and FINISH is at the end

return self.map
pass

async def align(
self,
self,
vehicle: Vehicle,
*,
target_previous_track_piece_type: TrackPieceType=TrackPieceType.FINISH
) -> None:
) -> None:
"""
Aligns a vehicle to the START piece
Expand All @@ -129,5 +132,6 @@ async def align(
pass
pass


_Scanner = TypeVar("_Scanner", bound=BaseScanner)
_ScannerType = type[_Scanner]
_ScannerType = type[_Scanner]
Loading

0 comments on commit d49ae5f

Please sign in to comment.