简体   繁体   English

代码更改-使用python2更改为python3

[英]code change - using python2 changing to python3

This is a piece of Sample code from O'Reily Twisted Network programming essentials book. 这是O'Reily Twisted Network编程基础知识手册中的一部分示例代码。 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? "h = HeadlineRetriever()行引起了问题。错误日志显示未定义“ HeadlineRetriever”。这是由于py3中的更改引起的吗?如何解决此问题?

Tried: unindenting the line h = HeadlineRetrieveer() but then the line d.addCallbacks(printData, printError) Did not recognize parameters. 尝试过: d.addCallbacks(printData, printError) h = HeadlineRetrieveer() ,然后d.addCallbacks(printData, printError)无法识别参数。

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 我认为在您定义h时以及在类本身中会出现识别错误

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. 也许您缺少__init__方法,也请保持代码在左边界缩进,如果不是,则可能会导致您的代码有问题。

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. printError和print数据函数需要自己作为第一个参数并在调用中。 d.adCallbacks(printData, printError) those needed to be d.addcallbacks(h.printData, h.printError) d.adCallbacks(printData, printError) d.addcallbacks(h.printData, h.printError)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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