-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
type
sparse.py
, enable mypy in CI (#773)
* typing configs * remove fallback implemenations of combination/permutation functions * refactor "test" to a test * some types * stash * register test_sparse in `run_tests.py` * isort * types * revert * unused * remove type restriction * python 3.7 * does claude know travis? * add comprehensive blacklist * travis * omit one more * black * add mypy to github actions * remove travis * better type ignore * remove whitespace cruft from merge * turns out black wanted that whitespace... * took out versions from requirements-dev.txt and simplified exclude rule for mypy --------- Co-authored-by: Franco Peschiera <[email protected]>
- Loading branch information
Showing
7 changed files
with
94 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Dict, Generic, List, Tuple, TypeVar, cast | ||
|
||
# Sparse : Python basic dictionary sparse matrix | ||
|
||
# Copyright (c) 2007, Stuart Mitchell ([email protected]) | ||
|
@@ -27,22 +29,31 @@ | |
notably this allows the sparse matrix to be output in various formats | ||
""" | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Matrix(dict): | ||
class Matrix(Generic[T], Dict[Tuple[int, int], T]): | ||
"""This is a dictionary based sparse matrix class""" | ||
|
||
def __init__(self, rows, cols): | ||
def __init__(self, rows: List[int], cols: List[int]): | ||
"""initialises the class by creating a matrix that will have the given | ||
rows and columns | ||
""" | ||
self.rows = rows | ||
self.cols = cols | ||
self.rowdict = {row: {} for row in rows} | ||
self.coldict = {col: {} for col in cols} | ||
self.rows: List[int] = rows | ||
self.cols: List[int] = cols | ||
self.rowdict: Dict[int, Dict[int, T]] = {row: {} for row in rows} | ||
self.coldict: Dict[int, Dict[int, T]] = {col: {} for col in cols} | ||
|
||
def add(self, row, col, item, colcheck=False, rowcheck=False): | ||
if not (rowcheck and row not in self.rows): | ||
if not (colcheck and col not in self.cols): | ||
def add( | ||
self, | ||
row: int, | ||
col: int, | ||
item: T, | ||
colcheck: bool = False, | ||
rowcheck: bool = False, | ||
) -> None: | ||
if not rowcheck or row in self.rows: | ||
if not colcheck or col in self.cols: | ||
dict.__setitem__(self, (row, col), item) | ||
self.rowdict[row][col] = item | ||
self.coldict[col][row] = item | ||
|
@@ -52,20 +63,26 @@ def add(self, row, col, item, colcheck=False, rowcheck=False): | |
else: | ||
raise RuntimeError(f"row {row} is not in the matrix rows") | ||
|
||
def addcol(self, col, rowitems): | ||
def addcol(self, col: int, rowitems: Dict[int, T]) -> None: | ||
"""adds a column""" | ||
if col in self.cols: | ||
for row, item in rowitems.items(): | ||
self.add(row, col, item, colcheck=False) | ||
else: | ||
raise RuntimeError("col is not in the matrix columns") | ||
|
||
def get(self, k, d=0): | ||
return dict.get(self, k, d) | ||
def get( # type: ignore[override] | ||
self, | ||
coords: Tuple[int, int], | ||
default: T = 0, # type: ignore[assignment] | ||
) -> T: | ||
return dict.get(self, coords, default) | ||
|
||
def col_based_arrays(self): | ||
def col_based_arrays( | ||
self, | ||
) -> Tuple[int, List[int], List[int], List[int], List[T]]: | ||
numEls = len(self) | ||
elemBase = [] | ||
elemBase: List[T] = [] | ||
startsBase = [] | ||
indBase = [] | ||
lenBase = [] | ||
|
@@ -74,5 +91,6 @@ def col_based_arrays(self): | |
elemBase.extend(list(self.coldict[col].values())) | ||
indBase.extend(list(self.coldict[col].keys())) | ||
lenBase.append(len(elemBase) - startsBase[-1]) | ||
|
||
startsBase.append(len(elemBase)) | ||
return numEls, startsBase, lenBase, indBase, elemBase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[tool.mypy] | ||
exclude = [ | ||
"doc/", | ||
"examples/", | ||
"pulp/__init__.py", | ||
"pulp/apis/", | ||
"pulp/mps_lp.py", | ||
"pulp/pulp.py", | ||
"pulp/solverdir/", | ||
"pulp/tests/", | ||
"pulp/utilities.py", | ||
"setup.py", | ||
"venv/.*" | ||
] | ||
follow_imports = "silent" | ||
strict = true | ||
check_untyped_defs = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
black | ||
mypy | ||
pre-commit==2.12.0 | ||
sphinx | ||
sphinx_rtd_theme | ||
black |