AttributeError:实例在__init__中没有属性

时间:2013-08-09 23:23:24

标签: python ubuntu-12.04 attributeerror bitcoin linuxmint

我似乎无法弄清楚问题出在哪里,

错误:

Traceback (most recent call last):
  File "./miningScreensaver", line 171, in <module>
    miningScreensaver().loop.run()
  File "./miningScreensaver", line 81, in __init__
    self.rxAddress = self.getRxAddress
AttributeError: miningScreensaver instance has no attribute 'getRxAddress'

代码:

#! Python
class miningScreensaver:
    def __init__(self):
        DBusGMainLoop(set_as_default=True)
        self.mem='ActiveChanged'
        self.dest='org.gnome.ScreenSaver'
        self.bus=SessionBus()
        self.loop=MainLoop()
        self.bus.add_signal_receiver(self.catch,self.mem,self.dest)
        self.pipe = ""

        #if you would like to specify a different rx address
        # change rxAddress to the desired rx address 
        self.rxAddress = self.getRxAddress()   #<--------------------ERROR HERE line 81
        #self.rxaddress = "18X3TEigc6PVTsF9Atx5br7rEXfuZRqXEz"

    def catch(self,ssOn):
        if ssOn == 1: #Screensaver turned on
            self.start()
        else: #Screensaver turned off
            self.stop()

    def start(self):
        self.pipe = Popen(["cgminer -o stratum+tcp://stratum.mining.eligius.st:3334 -u " + self.rxAddress + " -p x -I 9"], shell=True)

    def stop(self):
        self.pipe.kill()

    def getRxAddress(self):
        #check if bitcoin.conf exists
        cmd = "ls $HOME/.bitcoin/bitcoin.conf"
        pipe = Popen(cmd,stdout=PIPE,stderr=STDOUT)
        pout = pipe.stdout.read()
        pout = pout.split()
        if pout[len(pout)-5]=='No' and \
          pout[len(pout)-4]=='such' and \
          pout[len(pout)-3]=='file' and \
          pout[len(pout)-2]=='or' and \
          pout[len(pout)-1]=='directory\n':
            password = self.createBitConf()      
        else: 
            #check password 
            password = self.checkPassword()

        #Launch bitcoin-qt -server
        Popen(["bitcoin-qt","-server"])
        #Access
        access = ServiceProxy("http://darkPenguin:"+password+"@127.0.0.1:8332")
        #access.getinfo()
        return access.listreceivedbyaddress(0)
        #access.sendtoaddress("12yBwyDJHABCvohdT8qBTeMJEYDqpXnVYV", 0.01)

    def createBitConf(self):
        randomPW = self.createRandomPW()
        path = expanduser("~") + "/"
        defFile = open("bitosbitcoinconf", "r")
        newFile = open(path + ".bitcoin/bitcoin.conf","w")
        for line in range(1,55):
            newFile.write(defFile.readline())
        password = "rpcpassword="+randomPW+"\n"
        for line in range(56,110):
            newFile.write(defFile.readline())
        defFile.close()
        newFile.close()
        return randomPW

    def createRandomPW(self):
        myrg = random.SystemRandom()
        length = 44
        alphabet = string.ascii_letters + string.digits
        pw = str().join(myrg.choice(alphabet) for _ in range(length))
        return pw

    def checkPassword(self):
        path = expanduser("~") + "/"
        bitConfFile = open(path + ".bitcoin/bitcoin.conf","r")
        password = bitConfFile.readline(56)
        bitConfFile.close()
        return password[12:12+44-1] # "rpcpassword="+randomPW+"\n"

miningScreensaver().loop.run()

这让我感到沮丧,方法调用和方法拼写完全相同,解决其他人的问题一直没有帮助。

2 个答案:

答案 0 :(得分:2)

您有混合标签和空格。将编辑器设置为显示空格,您将看到问题。使用-tt选项运行python可以提供帮助。

答案 1 :(得分:2)

使用“编辑”按钮快速检查,表明您正在混合标签和空格。

特别是,miningScreensaver的前几个方法由一堆空格缩进,而其余的 - 包括getRxAddress - 由一个制表符缩进。因此,Python认为这些方法在类之外缩小,这意味着它们被编译为顶级函数,而不是miningScreensaver实例的方法。

要解决此问题,请解压缩代码并手动清理缩进。

要在将来避免这种情况,请使用更好的文本编辑器,并使用-t-tt标志运行Python,以便它显示有关标签/空间使用不一致的明显错误而不是试图解析代码并给你一个你无法理解的错误。