[simplex] Fix formatting and typing for CI.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2021-03-03 20:09:50 +00:00
parent 16453c424d
commit 1bcdb494cf
2 changed files with 39 additions and 15 deletions

View File

@ -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.

View File

@ -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