-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyDeferdS.py
More file actions
29 lines (24 loc) · 838 Bytes
/
PyDeferdS.py
File metadata and controls
29 lines (24 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from twisted.internet import reactor, defer
class HeadlineRetriever(object):
def processHeadline(self, headline):
if len(headline) > 50:
self.d.errback("The headline ``%s'' is too long!" % (headline,))
else:
self.d.callback(headline)
def _toHTML(self, result):
return "<h1>%s</h1>" % (result,)
def getHeadline(self, input):
self.d = defer.Deferred()
reactor.callLater(1, self.processHeadline, input)
self.d.addCallback(self._toHTML)
return self.d
def printData(result):
print(result)
reactor.stop()
def printError(failure):
print(failure)
reactor.stop()
h = HeadlineRetriever()
d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
d.addCallbacks(printData, printError)
reactor.run()