简体   繁体   中英

code change - using python2 changing to python3

This is a piece of Sample code from O'Reily Twisted Network programming essentials book. the line "h = HeadlineRetriever() is causing issues. The error log says 'HeadlineRetriever' is not defined. Is this due to changes in py3? How would I fix the issue?

Tried: unindenting the line h = HeadlineRetrieveer() but then the line d.addCallbacks(printData, printError) Did not recognize parameters.

from twisted.internet import reactor, defer

    class HeadlineRetriever(object):
        def processHeadline(self, headline):
            if len(headline) > 50:
                self.d.errback(
                    b"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()

Error log: Traceback (most recent call last):

  File "C:/Users/jessica/Twisted/3.4asynchronousHeadlineRetriever.py", line 3, in <module>
    class HeadlineRetriever(object):
  File "C:/Users/jessica/Twisted/3.4asynchronousHeadlineRetriever.py", line 28, in HeadlineRetriever
    h = HeadlineRetriever()
NameError: name 'HeadlineRetriever' is not defined

I think there is an identation mistake starting when you define h and in the class itself

from twisted.internet import reactor, defer

class HeadlineRetriever(object):
    def processHeadline(self, headline):
        if len(headline) > 50:
            self.d.errback(
                b"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()

This should work

how are you?

Maybe you're missing the __init__ method, also, maintain your code indented at the left margin, if it isn't, may cause your code to be a little buggy.

Try to add:

 def __init__(self):
            pass

See if it works, then update me.

Cheers!

    from twisted.internet import reactor, defer

        class HeadlineRetriever(object):
            def processHeadline(self, headline):
                if len(headline) > 50:
                    self.d.errback(
                        b"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(self, result):
                print(result)
                reactor.stop()

            def printError(self, failure):
                print(failure)
                reactor.stop()

  h = HeadlineRetriever()
  d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
  d.addCallbacks(h.printData, h.printError)

  reactor.run()

The printError and print data functions needed self as the first paramteter and in the call. d.adCallbacks(printData, printError) those needed to be d.addcallbacks(h.printData, h.printError)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM