Skip to content

Commit

Permalink
Get rid of Tuple, List and Dict
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlan committed Feb 29, 2024
1 parent d905916 commit f67e2ae
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 28 deletions.
6 changes: 2 additions & 4 deletions tests/parse/test_parse_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import List

import pytest
from numpy.testing import assert_equal

from vrplib.parse.parse_utils import text2lines


@pytest.mark.parametrize(("text", "expected"), [("", []), ("\n", [])])
def test_empty_lines(text: str, expected: List[str]):
def test_empty_lines(text: str, expected: list[str]):
assert_equal(text2lines(text), expected)


Expand All @@ -23,5 +21,5 @@ def test_empty_lines(text: str, expected: List[str]):
(" # comment after whitespace", []),
],
)
def test_comments(text: str, expected: List[str]):
def test_comments(text: str, expected: list[str]):
assert_equal(text2lines(text), expected)
4 changes: 2 additions & 2 deletions vrplib/parse/parse_distances.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from itertools import combinations
from typing import List, Optional, Union
from typing import Optional, Union

import numpy as np


def parse_distances(
data: List,
data: list,
edge_weight_type: str,
edge_weight_format: Optional[str] = None,
node_coord: Optional[np.ndarray] = None,
Expand Down
6 changes: 3 additions & 3 deletions vrplib/parse/parse_solomon.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Dict, List, Union
from typing import Union

import numpy as np

from .parse_distances import pairwise_euclidean
from .parse_utils import text2lines

Instance = Dict[str, Union[str, float, np.ndarray]]
Instance = dict[str, Union[str, float, np.ndarray]]


def parse_solomon(text: str, compute_edge_weights: bool = True) -> Instance:
Expand Down Expand Up @@ -46,7 +46,7 @@ def parse_solomon(text: str, compute_edge_weights: bool = True) -> Instance:
return instance


def is_valid_solomon_instance(lines: List[str]):
def is_valid_solomon_instance(lines: list[str]):
"""
Checks if the passed-in lines follow the Solomon format requirements.
"""
Expand Down
4 changes: 2 additions & 2 deletions vrplib/parse/parse_solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Dict, List, Union
from typing import Union

from .parse_utils import infer_type, text2lines

Solution = Dict[str, Union[float, str, List]]
Solution = dict[str, Union[float, str, list]]


def parse_solution(text: str) -> Solution:
Expand Down
4 changes: 2 additions & 2 deletions vrplib/parse/parse_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Union
from typing import Union


def text2lines(text: str) -> List[str]:
def text2lines(text: str) -> list[str]:
"""
Takes a string and returns a list of non-empty, stripped lines. Also
removes any comment lines from the given string.
Expand Down
10 changes: 5 additions & 5 deletions vrplib/parse/parse_vrplib.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
from typing import Dict, List, Tuple, Union
from typing import Union

import numpy as np

from .parse_distances import parse_distances
from .parse_utils import infer_type, text2lines

Instance = Dict[str, Union[str, float, np.ndarray]]
Instance = dict[str, Union[str, float, np.ndarray]]


def parse_vrplib(text: str, compute_edge_weights: bool = True) -> Instance:
Expand Down Expand Up @@ -53,7 +53,7 @@ def parse_vrplib(text: str, compute_edge_weights: bool = True) -> Instance:
return instance


def group_specifications_and_sections(lines: List[str]):
def group_specifications_and_sections(lines: list[str]):
"""
Groups instance lines into specifications and section parts.
"""
Expand Down Expand Up @@ -88,7 +88,7 @@ def group_specifications_and_sections(lines: List[str]):
return specs, sections


def parse_specification(line: str) -> Tuple[str, Union[float, str]]:
def parse_specification(line: str) -> tuple[str, Union[float, str]]:
"""
Parses a specification line as keyword-value pair, split at the first colon
occurrence. The keyword is made lowercase and the value is unmodified.
Expand All @@ -97,7 +97,7 @@ def parse_specification(line: str) -> Tuple[str, Union[float, str]]:
return k.lower(), infer_type(v)


def parse_section(lines: List, instance: Dict) -> np.ndarray:
def parse_section(lines: list, instance: dict) -> np.ndarray:
"""
Parses the data section into numpy arrays.
"""
Expand Down
4 changes: 2 additions & 2 deletions vrplib/read/read_instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Any, Dict, Union
from typing import Any, Union

from vrplib.parse import parse_solomon, parse_vrplib

Expand All @@ -8,7 +8,7 @@ def read_instance(
path: Union[str, os.PathLike],
instance_format: str = "vrplib",
compute_edge_weights: bool = True,
) -> Dict[str, Any]:
) -> dict[str, Any]:
"""
Reads the instance from the passed-in file path.
Expand Down
4 changes: 2 additions & 2 deletions vrplib/read/read_solution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from typing import Any, Dict, Union
from typing import Any, Union

from vrplib.parse import parse_solution


def read_solution(path: Union[str, os.PathLike]) -> Dict[str, Union[str, Any]]:
def read_solution(path: Union[str, os.PathLike]) -> dict[str, Union[str, Any]]:
"""
Reads the solution from the passed-in file path.
Expand Down
6 changes: 3 additions & 3 deletions vrplib/write/write_instance.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
from typing import Dict, List, Tuple, TypeVar, Union
from typing import TypeVar, Union

import numpy as np

_ArrayLike = TypeVar("_ArrayLike", List, Tuple, np.ndarray)
_ArrayLike = TypeVar("_ArrayLike", list, tuple, np.ndarray)


def write_instance(
path: Union[str, os.PathLike],
data: Dict[str, Union[str, int, float, _ArrayLike]],
data: dict[str, Union[str, int, float, _ArrayLike]],
):
"""
Writes a VRP instance to file following the VRPLIB format [1].
Expand Down
6 changes: 3 additions & 3 deletions vrplib/write/write_solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from typing import Any, Dict, List, Optional, Union
from typing import Any, Optional, Union


def write_solution(
path: Union[str, os.PathLike],
routes: List[List[int]],
data: Optional[Dict[str, Any]] = None,
routes: list[list[int]],
data: Optional[dict[str, Any]] = None,
):
"""
Writes a VRP solution to file following the VRPLIB convention.
Expand Down

0 comments on commit f67e2ae

Please sign in to comment.