From d9aea97962e42a2e78e345b5f92fb7f01b59dac3 Mon Sep 17 00:00:00 2001 From: Jeff Jacobson Date: Thu, 9 Jan 2025 11:36:27 -0800 Subject: [PATCH] refactor: :recycle: Ruff suggestions --- elc_test.py | 56 ++++++++++++++++++++++++++++++-------------- wsdotelc/__init__.py | 32 +++++++++++++++---------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/elc_test.py b/elc_test.py index 6a427ee..2310864 100644 --- a/elc_test.py +++ b/elc_test.py @@ -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) diff --git a/wsdotelc/__init__.py b/wsdotelc/__init__.py index 04c5dd7..7c17f7f 100644 --- a/wsdotelc/__init__.py +++ b/wsdotelc/__init__.py @@ -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 @@ -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 @@ -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`. @@ -74,6 +72,7 @@ def default(self, o): return super().default(o) + def dict_to_route_location(dct): if "error" in dct: return ElcError(dct["error"]["message"]) @@ -81,6 +80,7 @@ def dict_to_route_location(dct): return RouteLocation(**dct) return dct + class Elc(object): """This object is used to call the ELC REST SOE endpoint. @@ -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: @@ -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: @@ -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: @@ -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