root/trunk/doc/core/examples/ampserver.py

Revision 23920, 1.0 KB (checked in by exarkun, 2 years ago)

Apply 0001-Fix-example.patch

Author: davep
Reviewer: exarkun
Fixes: #3288

Change the ampserver example to always do float division and to use the
correct name when returning the result of the Divide command.

Line 
1from twisted.protocols import amp
2
3class Sum(amp.Command):
4    arguments = [('a', amp.Integer()),
5                 ('b', amp.Integer())]
6    response = [('total', amp.Integer())]
7
8
9class Divide(amp.Command):
10    arguments = [('numerator', amp.Integer()),
11                 ('denominator', amp.Integer())]
12    response = [('result', amp.Float())]
13    errors = {ZeroDivisionError: 'ZERO_DIVISION'}
14
15
16class Math(amp.AMP):
17    def sum(self, a, b):
18        total = a + b
19        print 'Did a sum: %d + %d = %d' % (a, b, total)
20        return {'total': total}
21    Sum.responder(sum)
22
23    def divide(self, numerator, denominator):
24        result = float(numerator) / denominator
25        print 'Divided: %d / %d = %f' % (numerator, denominator, result)
26        return {'result': result}
27    Divide.responder(divide)
28
29
30def main():
31    from twisted.internet import reactor
32    from twisted.internet.protocol import Factory
33    pf = Factory()
34    pf.protocol = Math
35    reactor.listenTCP(1234, pf)
36    print 'started'
37    reactor.run()
38
39if __name__ == '__main__':
40    main()
Note: See TracBrowser for help on using the browser.