Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python formatting tools #165

Merged
merged 18 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ jobs:
git submodule update --init --recursive --remote
pip install -r ./modules/common/requirements.txt

# Install computer-vision-python dependencies
# Install project dependencies
- name: Install project dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-pytorch.txt

# Run tests with PyTest
- name: Run tests
# Run linters and formatters
- name: Linters and formatters
run: |
black --check .
flake8 .
pylint .

# Run unit tests with PyTest
- name: Run unit tests
run: pytest -vv
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

# Python
__pycache__/
.pytest_cache/
venv/

# Logging
logs/

# Pytorch and Ultralytics
*.pt
*.pth

# Logging
*log*
12 changes: 6 additions & 6 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Global constants for main with cuda
# Global constants for main with CUDA

queue_max_size: 10

log_directory_path: "logs"

video_input:
camera_name: 0
worker_period: 1.0 # seconds
worker_period: 1.0 # seconds
save_prefix: "log_image"

detect_target:
worker_count: 1
device: 0
model_path: "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" # TODO: update
model_path: "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" # TODO: update
save_prefix: "log_comp"

flight_interface:
address: "tcp:127.0.0.1:14550"
timeout: 10.0 # seconds
worker_period: 0.1 # seconds
timeout: 10.0 # seconds
worker_period: 0.1 # seconds

data_merge:
timeout: 10.0 # seconds
timeout: 10.0 # seconds
22 changes: 19 additions & 3 deletions documentation/main_multiprocess_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
main process.
Main process. To run:
```
python -m documentation.main_multiprocess_example
```
"""

import multiprocessing as mp
import time

Expand All @@ -17,8 +21,11 @@
ADD_RANDOM_TO_CONCATENATOR_QUEUE_MAX_SIZE = 5


# Command: python -m documentation.main_multiprocess_example
if __name__ == "__main__":
# main() is required for early return
def main() -> int:
"""
Main function.
"""
# Main is managing all worker processes and is responsible
# for creating supporting interprocess communication
controller = worker_controller.WorkerController()
Expand Down Expand Up @@ -143,4 +150,13 @@
# Alternatively, create a new WorkerController instance
controller.clear_exit()

return 0


# Main guard is only used to call main()
if __name__ == "__main__":
result_main = main()
if result_main < 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
27 changes: 8 additions & 19 deletions documentation/multiprocess_example/add_random/add_random.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
"""
Contains the AddRandom class.
"""

import time
import random

from .. import intermediate_struct


# This class does very little, but still has state
# pylint: disable=too-few-public-methods
class AddRandom:
"""
Adds a random number to the input.

A new random number is generated every `__ADD_SWITCH_COUNT` times.
"""
def __init__(self, seed: int, max_random_term: int, add_change_count: int):

def __init__(self, seed: int, max_random_term: int, add_change_count: int) -> None:
"""
Constructor seeds the RNG and sets the max add and
number of adds before a new random number is chosen.
"""
random.seed(seed)
# Maximum value that can be added
# Constant within class does not follow Pylint naming
# pylint: disable=invalid-name
self.__MAX_RANDOM_TERM = max_random_term
# pylint: enable=invalid-name
self.__max_random_term = max_random_term

# Number of adds before getting a new random number
# Constant within class does not follow Pylint naming
# pylint: disable=invalid-name
self.__ADD_CHANGE_COUNT = add_change_count
# pylint: enable=invalid-name
self.__add_change_count = add_change_count

self.__current_random_term = self.__generate_random_number(0, self.__MAX_RANDOM_TERM)
self.__current_random_term = self.__generate_random_number(0, self.__max_random_term)
self.__add_count = 0

@staticmethod
Expand All @@ -51,8 +45,8 @@ def run_add_random(self, term: int) -> "tuple[bool, intermediate_struct.Intermed

# Change the random term if the add count has been reached
self.__add_count += 1
if self.__add_count >= self.__ADD_CHANGE_COUNT:
self.__current_random_term = self.__generate_random_number(0, self.__MAX_RANDOM_TERM)
if self.__add_count >= self.__add_change_count:
self.__current_random_term = self.__generate_random_number(0, self.__max_random_term)
self.__add_count = 0

# Pretending this class is hard at work
Expand All @@ -62,13 +56,8 @@ def run_add_random(self, term: int) -> "tuple[bool, intermediate_struct.Intermed
if add_sum % 2 == 0:
add_string = "even"

# For some reason Pylint hates having more than 1 parameter in a constructor
# pylint: disable=too-many-function-args
output = intermediate_struct.IntermediateStruct(add_sum, add_string)
# pylint: enable=too-many-function-args

# Function returns result and the output
# The class is responsible for packing the intermediate type
return True, output

# pylint: enable=too-few-public-methods
18 changes: 8 additions & 10 deletions documentation/multiprocess_example/add_random/add_random_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from . import add_random


# As kwargs is not being used, this function needs many parameters
# pylint: disable=too-many-arguments
def add_random_worker(seed: int,
max_random_term: int,
add_change_count: int,
input_queue: queue_proxy_wrapper.QueueProxyWrapper,
output_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController):
def add_random_worker(
seed: int,
max_random_term: int,
add_change_count: int,
input_queue: queue_proxy_wrapper.QueueProxyWrapper,
output_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController,
) -> None:
"""
Worker process.

Expand Down Expand Up @@ -52,5 +52,3 @@ def add_random_worker(seed: int,
# If the queue is full, the worker process will block
# until the queue is non-empty
output_queue.queue.put(value)

# pylint: enable=too-many-arguments
14 changes: 6 additions & 8 deletions documentation/multiprocess_example/concatenator/concatenator.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
"""
Contains the Concatenator class.
"""

import time

from .. import intermediate_struct


# This class does very little, but still has state
# pylint: disable=too-few-public-methods
class Concatenator:
"""
Concatenates a prefix and suffix to the object.
"""
def __init__(self, prefix: str, suffix: str):

def __init__(self, prefix: str, suffix: str) -> None:
"""
Constructor sets the prefix and suffix.
"""
self.__prefix = prefix
self.__suffix = suffix


# The working function
def run_concatenation(self,
middle: intermediate_struct.IntermediateStruct) -> "tuple[bool, str]":
def run_concatenation(
self, middle: intermediate_struct.IntermediateStruct
) -> "tuple[bool, str]":
"""
Concatenate the prefix and suffix to the input.
"""
Expand All @@ -42,5 +42,3 @@ def run_concatenation(self,

# Function returns result and the output
return True, concatenated_string

# pylint: enable=too-few-public-methods
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from . import concatenator


def concatenator_worker(prefix: str,
suffix: str,
input_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController):
def concatenator_worker(
prefix: str,
suffix: str,
input_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController,
) -> None:
"""
Worker process.

Expand Down
9 changes: 3 additions & 6 deletions documentation/multiprocess_example/countup/countup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
"""
Contains the Countup class.
"""

import time


# This class does very little, but still has state
# pylint: disable=too-few-public-methods
class Countup:
"""
Increments its internal counter and outputs current counter.
"""
def __init__(self, start_thousands: int, max_iterations: int):

def __init__(self, start_thousands: int, max_iterations: int) -> None:
"""
Constructor initializes the start and max points.
"""
self.__start_count = start_thousands * 1000
self.__max_count = self.__start_count + max_iterations
self.__current_count = self.__start_count


def run_countup(self) -> "tuple[bool, int]":
"""
Counts upward.
Expand All @@ -33,5 +32,3 @@ def run_countup(self) -> "tuple[bool, int]":

# Function returns result and the output
return True, self.__current_count

# pylint: enable=too-few-public-methods
10 changes: 6 additions & 4 deletions documentation/multiprocess_example/countup/countup_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from . import countup


def countup_worker(start_thousands:int,
max_iterations: int,
output_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController):
def countup_worker(
start_thousands: int,
max_iterations: int,
output_queue: queue_proxy_wrapper.QueueProxyWrapper,
controller: worker_controller.WorkerController,
) -> None:
"""
Worker process.

Expand Down
9 changes: 3 additions & 6 deletions documentation/multiprocess_example/intermediate_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
"""


# This class is just a struct containing some members
# pylint: disable=too-few-public-methods
class IntermediateStruct:
"""
Example of a simple struct.
"""
def __init__(self, number: int, sentence: str):

def __init__(self, number: int, sentence: str) -> None:
"""
Constructor
Constructor.
"""
self.number = number
self.sentence = sentence

# pylint: enable=too-few-public-methods
24 changes: 0 additions & 24 deletions documentation/tests/test_pytest_example.py

This file was deleted.

Loading
Loading