/
single_equation.py
30 lines (25 loc) · 1.22 KB
/
single_equation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"Implements SingleEquationConstraint"
from operator import le, ge, eq
from ..small_scripts import try_str_without
from ..repr_conventions import ReprMixin
class SingleEquationConstraint(ReprMixin):
"Constraint expressible in a single equation."
latex_opers = {"<=": "\\leq", ">=": "\\geq", "=": "="}
func_opers = {"<=": le, ">=": ge, "=": eq}
def __init__(self, left, oper, right):
self.left, self.oper, self.right = left, oper, right
def str_without(self, excluded=("units")):
"String representation without attributes in excluded list"
leftstr = try_str_without(self.left, excluded)
rightstr = try_str_without(self.right, excluded)
rlines = rightstr.split("\n")
if len(rlines) > 1:
indent = len("%s %s " % (leftstr.split("\n")[-1], self.oper))
rightstr = ("\n" + " "*indent).join(rlines)
return "%s %s %s" % (leftstr, self.oper, rightstr)
def latex(self, excluded=("units")):
"Latex representation without attributes in excluded list"
return ("%s %s %s" % (
try_str_without(self.left, excluded, latex=True),
self.latex_opers[self.oper],
try_str_without(self.right, excluded, latex=True)))