def quadratic_service():
    # http://localhost:8080/quadratic?a=25&b=80&c=4
    try:
        a = float(request.query.get('a', '0.0'))
        b = float(request.query.get('b', '0.0'))
        c = float(request.query.get('c', '0.0'))
    except ValueError:
        return dict(error='Must supply a floating point radius')
    x1, x2 = algebra.quadratic(a, b, c)
    return template('day2/notes2/quadratic.tpl', a=a, b=b, c=c, x1=x1, x2=x2)
def quadratic_solver():
    a = float(request.query.get('a', '0'))
    b = float(request.query.get('b', '0'))
    c = float(request.query.get('c', '0'))

    try:
        x1, x2 = algebra.quadratic(a, b, c)
    except ZeroDivisionError:
        x1 = x2 = 0

    if 'text/html' in request.headers.get('Accept', ''):
        return template('notes/quadratic.tpl', a=a, b=b, c=c, x1=x1, x2=x2)

    x1 = complex(x1)
    x2 = complex(x2)

    return dict(service=request.path,
                a=a,
                b=b,
                c=c,
                x1=dict(real=x1.real, imag=x1.imag),
                x2=dict(real=x2.real, imag=x2.imag))
Exemple #3
0
    except RuntimeError:
        raise ValueError('Negative base or height not supported, use positive inputs instead')

algebra.area_triangle = better_area_triangle              # Step 3:  Monkey patch

orig_sqrt = math.sqrt                                     # Step 1:  Save reference to original

def better_sqrt(x):                                       # Step 2:  Write a wrapper function
    'Wraps math.sqrt to add support for negative inputs'
    if x >= 0.0:
        return orig_sqrt(x)
    return orig_sqrt(-x) * 1j

math.sqrt = better_sqrt                                   # Step 3:  Monkey patch

#############################################################################

if __name__ == '__main__':

    print u'My sources tell me that \N{greek small letter pi} =', algebra.pi
    print 'and that the area of a circle of radius ten is:', algebra.area(10)
    print 'The area of the 1st triangle is', algebra.area_triangle(base=25, height=10)
    try:
        print 'The area of the 2nd triangle is', algebra.area_triangle(base=-5, height=20)
    except ValueError:
        print '... oops, I did it again!'
    print u'The solutions to 12x\N{superscript two} + 23x + 10 = 0 are:'
    print algebra.quadratic(a=12, b=23, c=10)
    print u'The solutions to 12x\N{superscript two} + 8x + 10 = 0 are:'
    print algebra.quadratic(a=12, b=8, c=10)
 def test_is_not_a_quadratic_function(self):
     # 0x^2 + 1x + 0 : isn't a quadratic
     with pytest.raises(ValueError):
         algebra.quadratic(0, 1, 0)
 def test_has_no_x_intercepts_2(self):
     # -x^2 - 1 : no X intercepts
     assert [] == algebra.quadratic(-1, 0, -1)
 def test_has_no_x_intercepts_1(self):
     # x^2 + 1 : no X intercepts
     assert [] == algebra.quadratic(1, 0, 1)
 def test_has_one_x_intercept_2(self):
     # -x^2
     assert [0] == algebra.quadratic(-1, 0, 0)
 def test_has_x_intercepts_3(self):
     # 3x^2 + 3x
     assert [-1.0, 0] == algebra.quadratic(3, 3, 0)
 def test_has_x_intercepts_2(self):
     # x^2 - 4
     assert [-2.0, 2.0] == algebra.quadratic(1, 0, -4)
 def test_has_x_intercepts_1(self):
     # -2x^2 + x + 3
     assert [-1.0, 1.5] == algebra.quadratic(-2, 1, 3)