Created
May 4, 2012 21:14
-
-
Save Drvanon/2597758 to your computer and use it in GitHub Desktop.
Excuses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.internet import reactor, defer | |
from twisted.internet.protocol import ClientCreator | |
from twisted.protocols import amp | |
from ampserver import Sum, Divide | |
def doMath(): | |
def menu(): | |
menu = raw_input('Do you want to divide (d) or add (a)?\n>') | |
if menu == 'a': | |
print 'You chose for adding' | |
askInputAdding() | |
elif menu == 'd': | |
print 'You chose for dividing' | |
askInputDividing() | |
else: | |
print 'That is not an option' | |
menu() | |
def askInputAdding(): | |
a = raw_input('First variable \n>') | |
b = raw_input('Second variable \n>') | |
try: | |
a = int(a) | |
b = int(b) | |
add(a, b) | |
except ValueError: | |
print 'Please insert a number' | |
askInputAdding() | |
def askInputDividing(): | |
a = raw_input('Numerator \n>') | |
b = raw_input('Denumerator \n>') | |
try: | |
a = int(a) | |
b = int(b) | |
divide(a, b) | |
except ValueError: | |
print 'Please insert a number' | |
askInput() | |
def add(a, b): | |
d1 = ClientCreator(reactor, amp.AMP).connectTCP( | |
'127.0.0.1', 1234).addCallback( | |
lambda p: p.callRemote(Sum, a=a, b=b)).addCallback( | |
lambda result: result['total']) | |
d1.addCallback(doneAdding) | |
def divide(a, b): | |
d2 = ClientCreator(reactor, amp.AMP).connectTCP( | |
'127.0.0.1', 1234).addCallback( | |
lambda p: p.callRemote(Divide, numerator=a, | |
denominator=b)).addErrback(trapZero) | |
d2.addCallback(doneDividing) | |
def trapZero(result): | |
result.trap(ZeroDivisionError) | |
print "Divided by zero: returning INF" | |
return 1e1000 | |
def doneAdding(result): | |
print 'The answer to the sum is:', result | |
menu() | |
def doneDividing(result): | |
print 'The answer to the sum is', result['result'] | |
menu() | |
menu() | |
if __name__ == '__main__': | |
doMath() | |
reactor.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.protocols import amp | |
class Sum(amp.Command): | |
arguments = [('a', amp.Integer()), | |
('b', amp.Integer())] | |
response = [('total', amp.Integer())] | |
class Divide(amp.Command): | |
arguments = [('numerator', amp.Integer()), | |
('denominator', amp.Integer())] | |
response = [('result', amp.Float())] | |
errors = {ZeroDivisionError: 'ZERO_DIVISION'} | |
class Math(amp.AMP): | |
def sum(self, a, b): | |
total = a + b | |
print 'Did a sum: %d + %d = %d' % (a, b, total) | |
return {'total': total} | |
Sum.responder(sum) | |
def divide(self, numerator, denominator): | |
result = float(numerator) / denominator | |
print 'Divided: %d / %d = %f' % (numerator, denominator, result) | |
return {'result': result} | |
Divide.responder(divide) | |
def main(): | |
from twisted.internet import reactor | |
from twisted.internet.protocol import Factory | |
pf = Factory() | |
pf.protocol = Math | |
reactor.listenTCP(1234, pf) | |
print 'started' | |
reactor.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment