Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
* Included "mypy" configuration. #41
Browse files Browse the repository at this point in the history
  • Loading branch information
garciparedes committed Aug 31, 2019
1 parent 98988ba commit 81eddd8
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ install:
- 'pip install pipenv'
- 'pipenv sync'
script:
- 'pipenv run flake8'
- 'pipenv run flake8'
- 'pipenv run coverage run --source=jinete --module unittest discover tests'
after_success:
Expand Down
51 changes: 51 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions jinete/models/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ def nbest(self, n: int, iterable: Iterable[T], key: Callable[[T], float], inplac

@unique
class DistanceMetric(Enum):

@staticmethod
def _euclidean_distance(a: Iterable[Number], b: Iterable[Number]) -> float:
return sqrt(sum(pow(a_i - b_i, 2) for a_i, b_i in zip(a, b)))

@staticmethod
def _manhattan_distance(a: Iterable[Number], b: Iterable[Number]) -> float:
return sum(abs(a_i - b_i) for a_i, b_i in zip(a, b))

Expand Down
2 changes: 1 addition & 1 deletion jinete/models/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class Position(ABC):
def __init__(self, surface: Surface = None):
def __init__(self, surface: Surface):
self.surface = surface

def __eq__(self, other):
Expand Down
3 changes: 2 additions & 1 deletion jinete/storers/formatters/hashcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ def route_to_str(route) -> str:

def format(self) -> str:
result = str()
result += '\n'.join(self.route_to_str(route) for route in self.routes)
lines = sorted(self.route_to_str(route) for route in self.routes)
result += '\n'.join(lines)
return result
12 changes: 10 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[flake8]
max-line-length = 120
filename =
examples/*/*.py
examples/**/*.py
jinete/**/*.py
tests/**/*.py
per-file-ignores =
./**/__init__.py:F401
./**/__init__.py:F401


[mypy]
ignore_missing_imports = True
files =
examples/**/*.py,
jinete/**/*.py,
tests/**/*.py
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
],
'syntax': [
'flake8',
'mypy',
],
'logs': [
'coloredlogs',
Expand Down
15 changes: 13 additions & 2 deletions tests/test_loaders/test_file.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING
from pathlib import Path

import jinete as jit

if TYPE_CHECKING:
from typing import (
Tuple,
)


class TestFileLoader(unittest.TestCase):
directory_path: Path
file_path: Path
data: Tuple[Tuple[float, ...], ...]

@classmethod
def setUpClass(cls) -> None:
cls.directory_path = Path('/tmp/')
cls.file_path = cls.directory_path / 'jinete_problem_test.txt'
cls.data = (
data = (
(1.0, 1, 480, 6.0, 90.0),
(0.0, -1.044, 2.000, 0.0, 0.0, 0.0, 1440.0),
(1.0, -2.973, 6.414, 10.0, 1.0, 0.0, 1440.0),
(2.0, -5.476, 1.437, 10.0, -1.0, 258.0, 287.0),
)
with cls.file_path.open('w') as file:
file.writelines('\t'.join(map(str, row)) + '\n' for row in cls.data)
file.writelines('\t'.join(map(str, row)) + '\n' for row in data)
cls.data = data

@classmethod
def tearDownClass(cls) -> None:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_loaders/test_formatters/test_cordeau_laporte.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import unittest
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING
import jinete as jit

if TYPE_CHECKING:
from typing import (
Tuple,
)


class TestCordeauLaporteLoaderFormatter(unittest.TestCase):
data: Tuple[Tuple[float, ...], ...]

@classmethod
def setUpClass(cls) -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_loaders/test_formatters/test_hashcode.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from __future__ import annotations

import unittest

from typing import TYPE_CHECKING

import jinete as jit

if TYPE_CHECKING:
from typing import (
Tuple,
)


class TestHashCodeLoaderFormatter(unittest.TestCase):
data: Tuple[Tuple[float, ...], ...]

@classmethod
def setUpClass(cls) -> None:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_models/test_criterions/test_longest_time.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import unittest
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING
import jinete as jit

if TYPE_CHECKING:
from typing import (
List,
)


class TestLongestTimePlannedTripCriterion(unittest.TestCase):
planned_trips: List[jit.PlannedTrip]

@classmethod
def setUpClass(cls) -> None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_models/test_criterions/test_shortest_time.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING

import jinete as jit

if TYPE_CHECKING:
from typing import (
List,
)


class TestShortestTimePlannedTripCriterion(unittest.TestCase):
planned_trips: List[jit.PlannedTrip]

@classmethod
def setUpClass(cls) -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_models/test_objectives/test_dial_a_ride.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@


class TestDialARideObjective(unittest.TestCase):
planned_trip: jit.PlannedTrip
stop: jit.Stop
route: jit.Route
planning: jit.Planning
result: jit.Result

@classmethod
def setUpClass(cls) -> None:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_models/test_positions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import unittest

import jinete as jit
from tests.utils import generate_one_surface


class TestPositions(unittest.TestCase):

def test_xy_position(self):
position = jit.GeometricPosition([3, 4])
surface = generate_one_surface()
position = jit.GeometricPosition([3, 4], surface)
self.assertEqual(3, position[0])
self.assertEqual(4, position[1])

Expand Down
4 changes: 3 additions & 1 deletion tests/test_storers/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


class TestFileStorer(unittest.TestCase):
directory_path: Path
file_path: Path
result: jit.Result

@classmethod
def setUpClass(cls) -> None:
Expand Down Expand Up @@ -39,8 +41,8 @@ def test_store(self):
storer.store()

expected_result = '\n'.join([
"2 0 1",
"1 2",
"2 0 1",
])

self.assertEqual(self.file_path.exists(), True)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_storers/test_formatters/test_hashcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@


class TestHashCodeStorerFormatter(unittest.TestCase):
result: jit.Result
expected_result: str

@classmethod
def setUpClass(cls) -> None:
cls.result = generate_one_result()
cls.expected_result = '\n'.join([
'2 0 1',
'1 2',
'2 0 1',
])

def test_creation(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_storers/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


class TestGraphPlotStorer(unittest.TestCase):
file_path: Path
directory_path: Path
result: jit.Result

@classmethod
def setUpClass(cls) -> None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_storers/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


class TestStorerSet(unittest.TestCase):
file_path: Path
directory_path: Path
result: jit.Result

@classmethod
def setUpClass(cls) -> None:
Expand Down
20 changes: 14 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


def generate_one_surface(*args, **kwargs) -> jit.Surface:
return jit.GeometricSurface(metric=jit.DistanceMetric.MANHATTAN, *args, **kwargs)
kwargs['metric'] = jit.DistanceMetric.MANHATTAN
return jit.GeometricSurface(*args, **kwargs)


def generate_surfaces(n: int, *args, **kwargs) -> Set[jit.Surface]:
Expand All @@ -25,8 +26,9 @@ def generate_one_position(x_min: float = -100, x_max: float = 100, y_min: float
def generate_positions(n: int, surface: jit.Surface = None, *args, **kwargs) -> Set[jit.Position]:
if surface is None:
surface = generate_one_surface(*args, **kwargs)
kwargs['surface'] = surface
return {
generate_one_position(surface=surface, *args, **kwargs) for _ in range(n)
generate_one_position(*args, **kwargs) for _ in range(n)
}


Expand Down Expand Up @@ -61,8 +63,11 @@ def generate_trips(n: int, *args, **kwargs) -> Set[jit.Trip]:


def generate_one_planned_trip(feasible: bool, route: jit.Route = None, *args, **kwargs) -> jit.PlannedTrip:
if route is None:
vehicle = generate_one_vehicle()
route = jit.Route(vehicle)
if feasible:
down_time = 0
down_time = 0.0
kwargs['earliest'] = 0.0
kwargs['timeout'] = float('inf')
else:
Expand Down Expand Up @@ -100,9 +105,12 @@ def generate_one_vehicle(capacity_min: int = 1, capacity_max: int = 3, earliest_


def generate_vehicles(n: int, *args, **kwargs) -> Set[jit.Vehicle]:
return {
generate_one_vehicle(idx=idx, *args, **kwargs) for idx in range(n)
}
vehicles = set()
for idx in range(n):
kwargs['idx'] = idx
vehicle = generate_one_vehicle(*args, **kwargs)
vehicles.add(vehicle)
return vehicles


def generate_one_route(feasible: bool, planned_trips_min: int = 1, planned_trips_max: int = 20,
Expand Down

0 comments on commit 81eddd8

Please sign in to comment.