test with no * between number and letter - doctest in operator

Conflicts:
	pymath/operator.py
This commit is contained in:
lafrite 2014-11-25 10:46:00 +01:00 committed by Lafrite
parent 39cc36869c
commit fec78c03d4
2 changed files with 31 additions and 41 deletions

View File

@ -75,24 +75,20 @@ class Operator(str):
:*args: Operands for this operation :*args: Operands for this operation
:returns: String with operator and his operands :returns: String with operator and his operands
>>> mul = Operator("*", 2) >>> op.mul.__txt__('1','2')
>>> add = Operator("+", 2)
>>> sub1 = Operator("-", 1)
>>> div = Operator("/", 1)
>>> mul.__txt__('1','2')
'1 * 2' '1 * 2'
>>> add.__txt__('1','2') >>> op.add.__txt__('1','2')
'1 + 2' '1 + 2'
>>> f = save_mainOp('2 + 3',add) >>> f = save_mainOp('2 + 3',op.add)
>>> mul.__txt__(f, '4') >>> op.mul.__txt__(f, '4')
'( 2 + 3 ) * 4' '( 2 + 3 ) * 4'
>>> f = save_mainOp('-3',sub1) >>> f = save_mainOp('-3',op.sub1)
>>> sub1.__txt__(f) >>> op.sub1.__txt__(f)
'- ( -3 )' '- ( -3 )'
>>> sub1.__txt__('-3') >>> op.sub1.__txt__('-3')
'- ( -3 )' '- ( -3 )'
>>> f = save_mainOp('2 + 3',add) >>> f = save_mainOp('2 + 3',op.add)
>>> sub1.__txt__(f) >>> op.sub1.__txt__(f)
'- ( 2 + 3 )' '- ( 2 + 3 )'
""" """
replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)} replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)}
@ -107,24 +103,20 @@ class Operator(str):
:*args: Operands for this operation :*args: Operands for this operation
:returns: String with operator and his operands :returns: String with operator and his operands
>>> mul = Operator("*", 2) >>> op.mul.__tex__('1','2')
>>> add = Operator("+", 2)
>>> sub1 = Operator("-", 1)
>>> div = Operator("/", 1)
>>> mul.__tex__('1','2')
'1 \\\\times 2' '1 \\\\times 2'
>>> add.__tex__('1','2') >>> op.add.__tex__('1','2')
'1 + 2' '1 + 2'
>>> f = save_mainOp('2 + 3',add) >>> f = save_mainOp('2 + 3',op.add)
>>> mul.__tex__(f, '4') >>> op.mul.__tex__(f, '4')
'( 2 + 3 ) \\\\times 4' '( 2 + 3 ) \\\\times 4'
>>> f = save_mainOp('-3',sub1) >>> f = save_mainOp('-3',op.sub1)
>>> sub1.__tex__(f) >>> op.sub1.__tex__(f)
'- ( -3 )' '- ( -3 )'
>>> sub1.__tex__('-3') >>> op.sub1.__tex__('-3')
'- ( -3 )' '- ( -3 )'
>>> f = save_mainOp('2 + 3',add) >>> f = save_mainOp('2 + 3',op.add)
>>> sub1.__tex__(f) >>> op.sub1.__tex__(f)
'- ( 2 + 3 )' '- ( 2 + 3 )'
""" """
replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)} replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)}
@ -139,21 +131,19 @@ class Operator(str):
:*args: Operands for this operation :*args: Operands for this operation
:returns: list with the operator surrounded by operands :returns: list with the operator surrounded by operands
>>> mul = Operator("*", 2) # TODO: order doctest |lun. nov. 24 07:17:29 CET 2014
>>> add = Operator("+", 2) >>> op.mul.__p2i__(1,2)
>>> sub1 = Operator("-", 1)
>>> mul.__p2i__(1,2)
[1, '*', 2] [1, '*', 2]
>>> f = save_mainOp([2, add, 3],add) >>> f = save_mainOp([2, op.add, 3],op.add)
>>> mul.__p2i__(f, 4) >>> op.mul.__p2i__(f, 4)
['(', 2, '+', 3, ')', '*', 4] ['(', 2, '+', 3, ')', '*', 4]
>>> f = save_mainOp([sub1, 3],sub1) >>> f = save_mainOp([op.sub1, 3],op.sub1)
>>> sub1.__p2i__(f) >>> op.sub1.__p2i__(f)
['-', '(', '-', 3, ')'] ['-', '(', '-', 3, ')']
>>> sub1.__p2i__(-3) >>> op.sub1.__p2i__(-3)
['-', '(', -3, ')'] ['-', '(', -3, ')']
>>> f = save_mainOp([2, add, 3],add) >>> f = save_mainOp([2, op.add, 3],op.add)
>>> sub1.__p2i__(f) >>> op.sub1.__p2i__(f)
['-', '(', 2, '+', 3, ')'] ['-', '(', 2, '+', 3, ')']
""" """
# TODO: Attention à gestion des fractions qui se comportent chelou avec les parenthèses |dim. nov. 9 09:21:52 CET 2014 # TODO: Attention à gestion des fractions qui se comportent chelou avec les parenthèses |dim. nov. 9 09:21:52 CET 2014

View File

@ -98,25 +98,25 @@ class TestPolynom(unittest.TestCase):
def test_reduce(self): def test_reduce(self):
p = Polynom([1, [2, 3], 4]) p = Polynom([1, [2, 3], 4])
self.assertEqual(str(p.reduce()[-1]),'4 * x ^ 2 + 5 * x + 1') self.assertEqual(str(p.reduce()[-1]),'4 x ^ 2 + 5 x + 1')
def test_add_int(self): def test_add_int(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
q = (p + 2)[-1] q = (p + 2)[-1]
self.assertEqual(str(q), '3 * x ^ 2 + 2 * x + 3') self.assertEqual(str(q), '3 x ^ 2 + 2 x + 3')
def test_add_frac(self): def test_add_frac(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
f = Fraction(1, 2) f = Fraction(1, 2)
q = (p + f)[-1] q = (p + f)[-1]
#ans = repr(Polynom([Expression(Fraction(3, 2)), Expression(2), Expression(3)])) #ans = repr(Polynom([Expression(Fraction(3, 2)), Expression(2), Expression(3)]))
self.assertEqual(str(q),'3 * x ^ 2 + 2 * x + 3 / 2') self.assertEqual(str(q),'3 x ^ 2 + 2 x + 3 / 2')
def test_add_poly(self): def test_add_poly(self):
p = Polynom([1, 0, 3]) p = Polynom([1, 0, 3])
q = Polynom([0, 2, 3]) q = Polynom([0, 2, 3])
r = (p + q)[-1] r = (p + q)[-1]
self.assertEqual(str(r), '6 * x ^ 2 + 2 * x + 1') self.assertEqual(str(r), '6 x ^ 2 + 2 x + 1')
def test_radd_int(self): def test_radd_int(self):
pass pass