diff --git a/simplex/simplex_dictionary.py b/simplex/simplex_dictionary.py index 9e870dc..8393879 100644 --- a/simplex/simplex_dictionary.py +++ b/simplex/simplex_dictionary.py @@ -2,10 +2,18 @@ import typing +from abc import abstractmethod, ABC from fractions import Fraction 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: 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: -V = typing.TypeVar("V") +V = typing.TypeVar("V", bound=Comparable) 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. Members: @@ -76,37 +85,49 @@ class simplex_dictionary(typing.Generic[V]): self.z = Fraction(0) 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: return "{} is not a basic variable.".format(key) return None 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: return "{} is not a non-basic variable.".format(key) return None @property 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) @property 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 @property 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 @property 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 @a.setter @@ -171,7 +192,8 @@ class simplex_dictionary(typing.Generic[V]): self._z = convert_value(value) 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: name: The name of the variable to convert. @@ -191,7 +213,8 @@ class simplex_dictionary(typing.Generic[V]): return s[0] + "_{" + s[1] + "}" def value_latex(self, value: Fraction) -> str: - """ Convert the given fraction to a latex fraction. + """ + Convert the given fraction to a latex fraction. Args: value: The fraction to convert. @@ -207,7 +230,8 @@ class simplex_dictionary(typing.Generic[V]): ) 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: name: Name of the dictionary. diff --git a/tests/test_magic_dictionary.py b/tests/test_magic_dictionary.py index 59bc462..6671867 100644 --- a/tests/test_magic_dictionary.py +++ b/tests/test_magic_dictionary.py @@ -4,8 +4,8 @@ from simplex.magic_dictionary import magic_dictionary def test_basic(): - """ Tests that a non-customized magic_dictionary acts - as a standard python dictionary. """ + """Tests that a non-customized magic_dictionary acts + as a standard python dictionary.""" d = magic_dictionary() assert len(d) == 0