2012-10-20 42 views
1

所以,我最近一直在研究IRC bot。它存儲了上次某人做了某些反垃圾郵件功能的一部分。它曾用於初始化世衛組織答覆中的用戶列表,我一直在使用RPL_NAMREPLY。Python - 局部變量錯誤,其中變量是一個導入的模塊

它幾乎可以工作。但是,我有一個非常古怪的問題..

import time 

# ... 

    def irc_unknown(self, prefix, command, params): 
     """Handle packets that aren't handled by the library.""" 

     # Prefix: asimov.freenode.net 
     # Command: RPL_BANLIST 
     # Params: ['MCBans_Testing', '#mcbans-test', 'a!*@*', '[email protected]/gdude2002', '1330592882'] 

     self.runHook("unknownMessage", {"prefix": prefix, "command": command, "params": params}) 

     if command == "RPL_BANLIST": 
      channel = params[1] 
      mask = params[2] 
      owner = params[3] 
      time = params[4] 

      if channel not in self.banlist.keys(): 
       done = {"done": False, "total": 1} 
       banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask, 
          "channel": channel} 
       done[mask] = banmask 
       self.banlist[channel] = done 

      else: 
       if not self.banlist[channel]["done"]: 
        banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask, 
           "channel": channel} 
        self.banlist[channel][mask] = banmask 
        self.banlist[channel]["total"] += 1 

       else: 
        done = {"done": False, "total": 1} 
        banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask, 
           "channel": channel} 
        done[mask] = banmask 
        self.banlist[channel] = done 

     elif command == "RPL_ENDOFBANLIST": 
      channel = params[1] 

      if channel in self.banlist.keys(): 
       self.banlist[channel]["done"] = True 
      else: 
       self.banlist[channel] = {"done": True, "total": 0} 

      self.prnt("|= Got %s bans for %s." % (self.banlist[channel]["total"], channel)) 

      if self.is_op(channel, self.nickname): 
       stuff = self.banlist[channel].keys() 
       stuff.remove("done") 
       stuff.remove("total") 

       for element in stuff: 
        if stuff == "*!*@*": 
         self.send_raw("KICK %s %s :Do not set such ambiguous bans!" % (
         channel, self.banlist[channel][element]["owner"])) 
         self.send_raw("MODE %s -b *!*@*" % channel) 
         self.send_raw(
          "MODE %s +b *!*@%s" % (channel, self.banlist[channel][element]["ownerhost"].split("@")[1])) 
        else: 
         self.checkban(channel, element, self.banlist[channel][element]["owner"]) 

     elif command == "RPL_NAMREPLY": 
      me, status, channel, names = params 
      users = names.split() 
      ranks = "+%@&~" 

      if not channel in self.chanlist.keys(): 
       self.chanlist[channel] = {} 

      for element in users: 
       rank = "" 

       for part in ranks: 
        if part in element: 
         rank = rank + part 
        element = element.strip(part) 

       done = { 'server': prefix, 
         'status': rank, 
         'last_time': float(time.time() - 0.25) } 
       self.chanlist[channel][element] = done 

      print "Names for %s%s: %s" % (status, channel, names) 

     elif command == "RPL_ENDOFNAMES": 
      me, channel, message = params 
      ops = 0 
      voices = 0 
      opers = 0 
      aways = 0 

      for element in self.chanlist[channel].values(): 
       status = element["status"] 
       if "+" in status: 
        voices += 1 
       if "@" in status: 
        ops += 1 
       if "*" in status: 
        opers += 1 
       if "G" in status: 
        aways += 1 
      print("|= %s users on %s (%s voices, %s ops, %s opers, %s away)" % (
      len(self.chanlist[channel]), channel, voices, ops, opers, aways)) 
      print "[%s] (%s) %s" % (prefix, command, params) 

     else: 
      print "[%s] (%s) %s" % (prefix, command, params) 

有了它,我收到以下錯誤..

File "C:\Users\Gareth\Documents\GitHub\McBlockit---Helpbot\system\irc.py", line 1422, in irc_unknown 
    done = {"server": prefix, "status": rank, "last_time": float(time.time() - 0.25)} 
exceptions.UnboundLocalError: local variable 'time' referenced before assignment 

正如你所看到的,我進口時間在頂部文件。並且該代碼在另一個函數中工作正常。有任何想法嗎?

回答

5

您的功能在if語句定義的局部變量time

 time = params[4] 

因爲它是一個if聲明沒有被分配到這裏面,而是因爲變量,進口time模塊在你的功能中不可用。

重命名該變量以解決衝突。

+1

..三年做Python,我沒有看到。我生鏽了。 xD - 謝謝。 :3 – gdude2002