Coverage for gpkit/constraints/single_equation.py: 95%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1"Implements SingleEquationConstraint" 

2from operator import le, ge, eq 

3from ..small_scripts import try_str_without 

4from ..repr_conventions import ReprMixin, UNICODE_EXPONENTS 

5 

6 

7class SingleEquationConstraint(ReprMixin): 

8 "Constraint expressible in a single equation." 

9 latex_opers = {"<=": "\\leq", ">=": "\\geq", "=": "="} 

10 unicode_opers = {"<=": "≤", ">=": "≥", "=": "="} 

11 func_opers = {"<=": le, ">=": ge, "=": eq} 

12 

13 def __init__(self, left, oper, right): 

14 self.left, self.oper, self.right = left, oper, right 

15 

16 def str_without(self, excluded=("units")): 

17 "String representation without attributes in excluded list" 

18 leftstr = try_str_without(self.left, excluded) 

19 rightstr = try_str_without(self.right, excluded) 

20 rlines = rightstr.split("\n") 

21 if len(rlines) > 1: 

22 indent = len("%s %s " % (leftstr.split("\n")[-1], self.oper)) 

23 rightstr = ("\n" + " "*indent).join(rlines) 

24 if UNICODE_EXPONENTS: 

25 oper = self.unicode_opers[self.oper] 

26 else: 

27 oper = self.oper 

28 return "%s %s %s" % (leftstr, oper, rightstr) 

29 

30 def latex(self, excluded=("units")): 

31 "Latex representation without attributes in excluded list" 

32 return ("%s %s %s" % ( 

33 try_str_without(self.left, excluded, latex=True), 

34 self.latex_opers[self.oper], 

35 try_str_without(self.right, excluded, latex=True)))