Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jul 2, 2024
1 parent a633bc7 commit 39b60fe
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
16 changes: 8 additions & 8 deletions examples/custom_animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def __init__(
get_loc_at_time1,
get_loc_at_time2,
line_color=red,
line_width=3,
drawing_order=0,
):
line_width: int = 3,
drawing_order: int = 0,
) -> None:
"""
get_loc_at_time 1 and 2 represent the location of the 1st and 2nd
endpoint of this lasso, respectively. They should take a single
Expand All @@ -42,11 +42,11 @@ def __init__(
self.get_loc1 = get_loc_at_time1
self.get_loc2 = get_loc_at_time2

def set_state(self, sim_time, get_xy):
def set_state(self, sim_time, get_xy) -> None:
self.xy1 = get_xy(*self.get_loc1(sim_time))
self.xy2 = get_xy(*self.get_loc2(sim_time))

def draw_to_surface(self, surf):
def draw_to_surface(self, surf) -> None:
pygame.draw.line(surf, self.line_color, self.xy1, self.xy2, self.line_width)

# So long as we are passing LassoViz's in as part of the scene_viz
Expand All @@ -70,9 +70,9 @@ def __init__(
frequency,
time_window,
label,
drawing_order=0,
image="images/train.png",
):
drawing_order: int = 0,
image: str = "images/train.png",
) -> None:
self.clat, self.clon = tie_post
self.lat_dist = lat_dist
self.lon_dist = lon_dist
Expand Down
42 changes: 21 additions & 21 deletions src/osmviz/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SimViz:
subclassed (or at least replicated).
"""

def __init__(self, drawing_order=0):
def __init__(self, drawing_order: int = 0) -> None:
"""
Base constructor for a SimViz.
'drawingOrder' is used to specify the order in which this viz
Expand Down Expand Up @@ -151,13 +151,13 @@ class TrackingViz(SimViz):

def __init__(
self,
label,
image,
label: str,
image: str,
get_lat_lon_at_time_func,
time_window,
bounding_box,
drawing_order=0,
):
time_window: tuple[float, float],
bounding_box: tuple[float, float, float, float],
drawing_order: int = 0,
) -> None:
"""
Constructs a TrackingViz.
Arguments:
Expand Down Expand Up @@ -189,15 +189,15 @@ def get_bounding_box(self):
def get_label(self):
return self.label

def set_state(self, sim_time, get_xy):
def set_state(self, sim_time, get_xy) -> None:
self.xy = None
ll = self.get_location_at_time(sim_time)
if ll is None:
return
x, y = get_xy(*ll)
self.xy = x, y

def draw_to_surface(self, surf):
def draw_to_surface(self, surf) -> None:
if self.xy:
x, y = self.xy
w, h = self.width, self.height
Expand All @@ -219,7 +219,7 @@ class Simulation:
method is provided which displays the simulation in a pygame window.
"""

def __init__(self, actor_vizs, scene_vizs, init_time=0):
def __init__(self, actor_vizs, scene_vizs, init_time: int = 0) -> None:
"""
Given two collections of generic SimViz objects, and optionally an
initial time, creates a Simulation object.
Expand All @@ -238,7 +238,7 @@ def __init__(self, actor_vizs, scene_vizs, init_time=0):
self.time = 10000
self.set_time(init_time)

def __find_bounding_box(self):
def __find_bounding_box(self) -> None:
"""Finds the lat_lon box bounding all objects"""
init_box = (Inf, -Inf, Inf, -Inf)

Expand All @@ -253,7 +253,7 @@ def helper(left, right):

self.bounding_box = reduce(helper, self.actor_vizs, init_box)

def __find_time_window(self):
def __find_time_window(self) -> None:
"""Finds the min and max times over all routes"""
init_window = (Inf, -Inf)

Expand All @@ -263,21 +263,21 @@ def helper(left, right):

self.time_window = reduce(helper, self.actor_vizs, init_window)

def __sort_vizs(self):
def __sort_vizs(self) -> None:
"""Sorts tracked objects in order of Drawing Order"""

def key_function(item):
return item.get_drawing_order()

self.all_vizs.sort(key=key_function)

def set_time(self, time):
def set_time(self, time) -> None:
"""
Moves all bus tracks to the given time.
"""
self.time = min(max(time, self.time_window[0]), self.time_window[1])

def print_time(self):
def print_time(self) -> None:
hours = int(self.time / 3600)
minutes = int((self.time % 3600) / 60)
seconds = int(self.time % 60)
Expand All @@ -295,14 +295,14 @@ def get_xy(self, lat, lon, bounds, screen_size):

def run(
self,
speed=0.0,
speed: float = 0.0,
window_size=(1280, 800),
refresh_rate=1.0,
font="/Library/Frameworks/Python.framework/Versions/2.5/"
refresh_rate: float = 1.0,
font: str = "/Library/Frameworks/Python.framework/Versions/2.5/"
"lib/python2.5/site-packages/pygame/freesansbold.ttf",
font_size=10,
osm_zoom=14,
):
font_size: int = 10,
osm_zoom: int = 14,
) -> None:
"""
Pops up a window and displays the simulation on it.
speed is advancement of sim in seconds/second.
Expand Down
14 changes: 7 additions & 7 deletions src/osmviz/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ImageManager:
by an OSMManager object.
"""

def __init__(self):
def __init__(self) -> None:
self.image = None

# TO BE OVERRIDDEN #
Expand Down Expand Up @@ -93,7 +93,7 @@ def prepare_image(self, width, height):
raise Exception(msg)
self.image = self.create_image(width, height)

def destroy_image(self):
def destroy_image(self) -> None:
"""
Destroys internal representation of the image, if it was
ever created.
Expand Down Expand Up @@ -134,7 +134,7 @@ class PygameImageManager(ImageManager):
An ImageManager which works with Pygame images.
"""

def __init__(self):
def __init__(self) -> None:
ImageManager.__init__(self)
try:
import pygame
Expand All @@ -149,7 +149,7 @@ def create_image(self, width, height):
def load_image_file(self, image_file):
return self.pygame.image.load(image_file)

def paste_image(self, img, xy):
def paste_image(self, img, xy) -> None:
self.get_image().blit(img, xy)


Expand All @@ -158,7 +158,7 @@ class PILImageManager(ImageManager):
An ImageManager which works with PIL images.
"""

def __init__(self, mode):
def __init__(self, mode) -> None:
"""
Constructs a PIL Image Manager.
Arguments:
Expand All @@ -179,7 +179,7 @@ def create_image(self, width, height):
def load_image_file(self, image_file):
return self.PILImage.open(image_file)

def paste_image(self, img, xy):
def paste_image(self, img, xy) -> None:
self.get_image().paste(img, xy)


Expand All @@ -191,7 +191,7 @@ class OSMManager:
into one big image.
"""

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
"""
Creates an OSMManager.
Arguments:
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def find_bounds(route):
return (min_time, max_time), (min_lat, max_lat, min_lon, max_lon)


def test_sim(route, zoom, image="test/images/train.png"):
def test_sim(route, zoom, image: str = "test/images/train.png") -> None:
time_window, bbox = find_bounds(route)

def get_ll(time):
Expand All @@ -45,7 +45,7 @@ def get_ll(time):
sim.run(speed=0, refresh_rate=0.1, osmzoom=zoom, windowsize=(600, 600))


def test_sim_one():
def test_sim_one() -> None:
begin_ll = 45 + 46.0 / 60, -(68 + 39.0 / 60)
end_ll = 30 + 3.0 / 60, -(118 + 15.0 / 60)

Expand Down
2 changes: 1 addition & 1 deletion test/functional/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from osmviz.manager import OSMManager, PILImageManager


def test_pil():
def test_pil() -> None:
image_manager = PILImageManager("RGB")
osm = OSMManager(image_manager=image_manager)
image, bounds = osm.create_osm_image((30, 31, -117, -116), 9)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_image_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from osmviz.manager import ImageManager


def test_unimplemented():
def test_unimplemented() -> None:
# Arrange
image_manager = ImageManager()
# Dummy parameters
Expand Down
12 changes: 6 additions & 6 deletions test/unit/test_osm_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def osm_manager():
yield osm_manager


def test_get_tile_coord(osm_manager):
def test_get_tile_coord(osm_manager) -> None:
# Arrange
lon_deg = 24.945831
lat_deg = 60.192059
Expand All @@ -29,7 +29,7 @@ def test_get_tile_coord(osm_manager):
assert coord == (18654, 9480)


def test_get_tile_url(osm_manager):
def test_get_tile_url(osm_manager) -> None:
# Arrange
tile_coord = (18654, 9480)
zoom = 15
Expand All @@ -41,7 +41,7 @@ def test_get_tile_url(osm_manager):
assert url == "https://tile.openstreetmap.org/15/18654/9480.png"


def test_get_local_tile_filename(osm_manager):
def test_get_local_tile_filename(osm_manager) -> None:
# Arrange
tile_coord = (18654, 9480)
zoom = 15
Expand All @@ -53,7 +53,7 @@ def test_get_local_tile_filename(osm_manager):
assert filename.endswith("-15_18654_9480.png")


def test_retrieve_tile_image(osm_manager):
def test_retrieve_tile_image(osm_manager) -> None:
# Arrange
tile_coord = (18654, 9480)
zoom = 15
Expand All @@ -65,7 +65,7 @@ def test_retrieve_tile_image(osm_manager):
assert filename.endswith("-15_18654_9480.png")


def test_tile_nw_lat_lon(osm_manager):
def test_tile_nw_lat_lon(osm_manager) -> None:
# Arrange
tile_coord = (18654, 9480)
zoom = 15
Expand All @@ -77,7 +77,7 @@ def test_tile_nw_lat_lon(osm_manager):
assert (lat_deg, lon_deg) == (60.19615576604439, 24.93896484375)


def test_create_osm_image(osm_manager):
def test_create_osm_image(osm_manager) -> None:
# Arrange
minlat = 59.9225115912
maxlat = 60.297839409
Expand Down
16 changes: 8 additions & 8 deletions test/unit/test_pil_image_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def image_manager():
yield image_manager


def test_prepare_image(image_manager):
def test_prepare_image(image_manager) -> None:
# Arrange
width, height = 200, 100

Expand All @@ -27,7 +27,7 @@ def test_prepare_image(image_manager):
assert image_manager.image.size == (200, 100)


def test_prepare_image__twice(image_manager):
def test_prepare_image__twice(image_manager) -> None:
# Arrange
width, height = 200, 100

Expand All @@ -39,7 +39,7 @@ def test_prepare_image__twice(image_manager):
image_manager.prepare_image(width, height)


def test_destroy_image__no_image(image_manager):
def test_destroy_image__no_image(image_manager) -> None:
# Arrange
# Act
image_manager.destroy_image()
Expand All @@ -48,7 +48,7 @@ def test_destroy_image__no_image(image_manager):
assert image_manager.image is None


def test_destroy_image__with_image(image_manager):
def test_destroy_image__with_image(image_manager) -> None:
# Arrange
width, height = 200, 100
image_manager.prepare_image(width, height)
Expand All @@ -61,7 +61,7 @@ def test_destroy_image__with_image(image_manager):
assert image_manager.image is None


def test_paste_image_file__image_not_prepared(image_manager):
def test_paste_image_file__image_not_prepared(image_manager) -> None:
# Arrange
filename = "dummy.jpg"
xy = (0, 0)
Expand All @@ -71,7 +71,7 @@ def test_paste_image_file__image_not_prepared(image_manager):
image_manager.paste_image_file(filename, xy)


def test_paste_image_file__could_not_load_image(image_manager):
def test_paste_image_file__could_not_load_image(image_manager) -> None:
# Arrange
width, height = 200, 100
image_manager.prepare_image(width, height)
Expand All @@ -84,7 +84,7 @@ def test_paste_image_file__could_not_load_image(image_manager):
image_manager.paste_image_file(filename, xy)


def test_paste_image_file(image_manager):
def test_paste_image_file(image_manager) -> None:
# Arrange
width, height = 200, 100
image_manager.prepare_image(width, height)
Expand All @@ -99,7 +99,7 @@ def test_paste_image_file(image_manager):
assert image_manager.image.size == (200, 100)


def test_get_image(image_manager):
def test_get_image(image_manager) -> None:
# Arrange
width, height = 200, 100
image_manager.prepare_image(width, height)
Expand Down

0 comments on commit 39b60fe

Please sign in to comment.