[simplex] Fix formatting and typing for CI.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
16453c424d
commit
1bcdb494cf
@ -2,10 +2,18 @@
|
|||||||
|
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
from abc import abstractmethod, ABC
|
||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
|
|
||||||
from .magic_dictionary import magic_dictionary
|
from .magic_dictionary import magic_dictionary
|
||||||
|
|
||||||
|
|
||||||
|
class Comparable(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def __lt__(self, other: typing.Any) -> bool:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
# Type that can be converted by convert_value:
|
# Type that can be converted by convert_value:
|
||||||
convertable_type = typing.Union[str, int, Fraction]
|
convertable_type = typing.Union[str, int, Fraction]
|
||||||
|
|
||||||
@ -18,12 +26,13 @@ def convert_value(value: convertable_type) -> Fraction:
|
|||||||
|
|
||||||
|
|
||||||
# Type of variables in a simplex_dictionary:
|
# Type of variables in a simplex_dictionary:
|
||||||
V = typing.TypeVar("V")
|
V = typing.TypeVar("V", bound=Comparable)
|
||||||
|
|
||||||
|
|
||||||
class simplex_dictionary(typing.Generic[V]):
|
class simplex_dictionary(typing.Generic[V]):
|
||||||
|
|
||||||
""" Class representing a dictionary for the simplex algorithm. The class contains
|
"""
|
||||||
|
Class representing a dictionary for the simplex algorithm. The class contains
|
||||||
multiple members representing the elements of a dictionary.
|
multiple members representing the elements of a dictionary.
|
||||||
|
|
||||||
Members:
|
Members:
|
||||||
@ -76,37 +85,49 @@ class simplex_dictionary(typing.Generic[V]):
|
|||||||
self.z = Fraction(0)
|
self.z = Fraction(0)
|
||||||
|
|
||||||
def _check_basic(self, key: V) -> typing.Optional[str]:
|
def _check_basic(self, key: V) -> typing.Optional[str]:
|
||||||
""" Check if the given key is a basic variable, returning None if it is,
|
"""
|
||||||
and a custom exception string if not. Suitable for magic_dictionary use. """
|
Check if the given key is a basic variable, returning None if it is,
|
||||||
|
and a custom exception string if not. Suitable for magic_dictionary use.
|
||||||
|
"""
|
||||||
if key not in self.B:
|
if key not in self.B:
|
||||||
return "{} is not a basic variable.".format(key)
|
return "{} is not a basic variable.".format(key)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _check_non_basic(self, key: V) -> typing.Optional[str]:
|
def _check_non_basic(self, key: V) -> typing.Optional[str]:
|
||||||
""" Check if the given key is a non-basic variable, returning None if it is,
|
"""
|
||||||
and a custom exception string if not. Suitable for magic_dictionary use. """
|
Check if the given key is a non-basic variable, returning None if it is,
|
||||||
|
and a custom exception string if not. Suitable for magic_dictionary use.
|
||||||
|
"""
|
||||||
if key not in self.N:
|
if key not in self.N:
|
||||||
return "{} is not a non-basic variable.".format(key)
|
return "{} is not a non-basic variable.".format(key)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def variables(self) -> typing.List[V]:
|
def variables(self) -> typing.List[V]:
|
||||||
""" Returns: The list of variables in this dictionary. """
|
"""
|
||||||
|
Returns: The list of variables in this dictionary.
|
||||||
|
"""
|
||||||
return sorted(self.B + self.N)
|
return sorted(self.B + self.N)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def B(self) -> typing.List[V]:
|
def B(self) -> typing.List[V]:
|
||||||
""" Returns: The list of basic variables for this dictionary. """
|
"""
|
||||||
|
Returns: The list of basic variables for this dictionary.
|
||||||
|
"""
|
||||||
return self.__B
|
return self.__B
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def N(self) -> typing.List[V]:
|
def N(self) -> typing.List[V]:
|
||||||
""" Returns: The list of non-basic variables for this dictionary. """
|
"""
|
||||||
|
Returns: The list of non-basic variables for this dictionary.
|
||||||
|
"""
|
||||||
return self.__N
|
return self.__N
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def a(self) -> magic_dictionary[V, magic_dictionary[V, Fraction]]:
|
def a(self) -> magic_dictionary[V, magic_dictionary[V, Fraction]]:
|
||||||
""" Returns: The a matrix of this dictionary. """
|
"""
|
||||||
|
Returns: The a matrix of this dictionary.
|
||||||
|
"""
|
||||||
return self._a
|
return self._a
|
||||||
|
|
||||||
@a.setter
|
@a.setter
|
||||||
@ -171,7 +192,8 @@ class simplex_dictionary(typing.Generic[V]):
|
|||||||
self._z = convert_value(value)
|
self._z = convert_value(value)
|
||||||
|
|
||||||
def name_latex(self, name: typing.Any) -> str:
|
def name_latex(self, name: typing.Any) -> str:
|
||||||
""" Convert the given variable name to a clean latex name.
|
"""
|
||||||
|
Convert the given variable name to a clean latex name.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: The name of the variable to convert.
|
name: The name of the variable to convert.
|
||||||
@ -191,7 +213,8 @@ class simplex_dictionary(typing.Generic[V]):
|
|||||||
return s[0] + "_{" + s[1] + "}"
|
return s[0] + "_{" + s[1] + "}"
|
||||||
|
|
||||||
def value_latex(self, value: Fraction) -> str:
|
def value_latex(self, value: Fraction) -> str:
|
||||||
""" Convert the given fraction to a latex fraction.
|
"""
|
||||||
|
Convert the given fraction to a latex fraction.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
value: The fraction to convert.
|
value: The fraction to convert.
|
||||||
@ -207,7 +230,8 @@ class simplex_dictionary(typing.Generic[V]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def display(self, name: str = None):
|
def display(self, name: str = None):
|
||||||
""" Display this simplex dictionary on the standard Jupyter output.
|
"""
|
||||||
|
Display this simplex dictionary on the standard Jupyter output.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: Name of the dictionary.
|
name: Name of the dictionary.
|
||||||
|
@ -4,8 +4,8 @@ from simplex.magic_dictionary import magic_dictionary
|
|||||||
|
|
||||||
|
|
||||||
def test_basic():
|
def test_basic():
|
||||||
""" Tests that a non-customized magic_dictionary acts
|
"""Tests that a non-customized magic_dictionary acts
|
||||||
as a standard python dictionary. """
|
as a standard python dictionary."""
|
||||||
|
|
||||||
d = magic_dictionary()
|
d = magic_dictionary()
|
||||||
assert len(d) == 0
|
assert len(d) == 0
|
||||||
|
Loading…
Reference in New Issue
Block a user