Skip to content

Commit

Permalink
refactor: ♻️ Ruff suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffJacobson committed Jan 9, 2025
1 parent 38ca02d commit d9aea97
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
56 changes: 38 additions & 18 deletions elc_test.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
"""Unit test for the elcpy module.
"""
import sys, unittest, wsdotelc
"""Unit test for the elcpy module."""

import unittest
import wsdotelc


class Test_unittest(unittest.TestCase):
def setUp(self):
self.elc = wsdotelc.Elc()

def test_routes(self):
"""Test the retrieval of `elcpy.Elc.routes`.
"""
"""Test the retrieval of `elcpy.Elc.routes`."""
routes = self.elc.routes
self.assertTrue(isinstance(routes, dict), "Returned routes object is a dict.")
self.assertTrue(isinstance(self.elc._routes, dict), "Route dict has been cached.")
self.assertTrue(
isinstance(self.elc._routes, dict), "Route dict has been cached."
)

def test_find_route_locations(self):
"""Test the `elcpy.Elc.find_route_locations` function.
"""
#locations = (elcpy.RouteLocation(route="005", arm=5, reference_date="12/31/2013"))
"""Test the `elcpy.Elc.find_route_locations` function."""
# locations = (elcpy.RouteLocation(route="005", arm=5, reference_date="12/31/2013"))
# Create a set of locations.
locations = (wsdotelc.RouteLocation(Route="005", Arm=5),)
out_locations = self.elc.find_route_locations(locations, "12/31/2013")
self.assertEqual(len(out_locations), 1, "Result has single element.")
self.assertIsInstance(out_locations[0], wsdotelc.RouteLocation, "The first element in the returned array is an `elcpy.RouteLocation`.")
self.assertIsInstance(
out_locations[0],
wsdotelc.RouteLocation,
"The first element in the returned array is an `elcpy.RouteLocation`.",
)

def test_find_nearest_route_location(self):
"""Test the `elcpy.Elc.find_dearest_route_locations` function.
"""
"""Test the `elcpy.Elc.find_dearest_route_locations` function."""
points = [1087403.28714286, 136623.00728571415]
out_locations = self.elc.find_nearest_route_locations(points, "12/31/2013", 200, 2927)
self.assertEqual(1, len(out_locations), "Input and output loctions should have the same number of elements.")
self.assertIsInstance(out_locations[0], wsdotelc.RouteLocation, "The first element in the returned array is an `elcpy.RouteLocation`.")
self.assertListEqual([out_locations[0].Route, out_locations[0].Arm], ["005", 5], "Test for expected Route ID and ARM values.")
out_locations = self.elc.find_nearest_route_locations(
points, "12/31/2013", 200, 2927
)
self.assertEqual(
1,
len(out_locations),
"Input and output loctions should have the same number of elements.",
)
self.assertIsInstance(
out_locations[0],
wsdotelc.RouteLocation,
"The first element in the returned array is an `elcpy.RouteLocation`.",
)
self.assertListEqual(
[out_locations[0].Route, out_locations[0].Arm],
["005", 5],
"Test for expected Route ID and ARM values.",
)

#def test_A(self):
# def test_A(self):
# self.fail("Not implemented")

if __name__ == '__main__':

if __name__ == "__main__":
unittest.main()
# suite = unittest.TestLoader().loadTestsFromTestCase(Test_unittest)
# unittest.TextTestRunner(verbosity=2).run(suite)
32 changes: 20 additions & 12 deletions wsdotelc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""elcpy
This module is designed for accessing the WSDOT ELC REST SOE at https://data.wsdot.wa.gov/arcgis/rest/services/Shared/ElcRestSOE/MapServer/exts/ElcRestSoe/
"""
from __future__ import print_function, absolute_import, division, unicode_literals

import datetime
import json
from dataclasses import dataclass
Expand All @@ -22,8 +22,7 @@ def __init__(self, error_message):

@dataclass
class RouteLocation(object):
"""Represents a route location object used as input and output from the ELC.
"""
"""Represents a route location object used as input and output from the ELC."""

Id: int = None
Route: str = None
Expand Down Expand Up @@ -59,8 +58,7 @@ def dict_contains_any_of_these_keys(d, *args):


class RouteLocationEncoder(json.JSONEncoder):
"""This class is used for converting a `RouteLocation` into JSON.
"""
"""This class is used for converting a `RouteLocation` into JSON."""

def default(self, o):
"""Converts the input object into a `dict`.
Expand All @@ -74,13 +72,15 @@ def default(self, o):

return super().default(o)


def dict_to_route_location(dct):
if "error" in dct:
return ElcError(dct["error"]["message"])
if "Route" in dct:
return RouteLocation(**dct)
return dct


class Elc(object):
"""This object is used to call the ELC REST SOE endpoint.
Expand Down Expand Up @@ -113,7 +113,9 @@ def get_routes(self):
self._routes = response.json()
return self._routes

def find_route_locations(self, locations, reference_date=None, out_sr=None, lrs_year=None):
def find_route_locations(
self, locations, reference_date=None, out_sr=None, lrs_year=None
):
"""Finds the route locations.
Parameters:
Expand All @@ -126,10 +128,7 @@ def find_route_locations(self, locations, reference_date=None, out_sr=None, lrs_
url = self.url + _FIND_ROUTE_LOCATIONS
# Convert the locations into JSON strings.
locations_json = json.dumps(locations, cls=RouteLocationEncoder)
params_dict = {
"f": "json",
"locations": locations_json
}
params_dict = {"f": "json", "locations": locations_json}
if reference_date is not None:
params_dict["referenceDate"] = str(reference_date)
if out_sr is not None:
Expand All @@ -148,7 +147,16 @@ def find_route_locations(self, locations, reference_date=None, out_sr=None, lrs_
raise output
return output

def find_nearest_route_locations(self, coordinates, reference_date, search_radius, in_sr, out_sr=None, lrs_year=None, route_filter=None):
def find_nearest_route_locations(
self,
coordinates,
reference_date,
search_radius,
in_sr,
out_sr=None,
lrs_year=None,
route_filter=None,
):
"""Finds the route locations nearest to the input coordinates.
Parameters:
Expand All @@ -169,7 +177,7 @@ def find_nearest_route_locations(self, coordinates, reference_date, search_radiu
"coordinates": json.dumps(coordinates),
"referenceDate": reference_date,
"searchRadius": search_radius,
"inSR": in_sr
"inSR": in_sr,
}
if out_sr is not None:
param_dict["outSR"] = out_sr
Expand Down

0 comments on commit d9aea97

Please sign in to comment.