2011-01-25 167 views
2

我試圖導入該文件導入腳本到另一個腳本

http://pastebin.com/bEss4J6Q

到這個文件

def MainLoop(self): #MainLoop is used to make the commands executable ie !google !say etc; 
       try: 
        while True: 
         # This method sends a ping to the server and if it pings it will send a pong back 
         #in other clients they keep receiving till they have a complete line however mine does not as of right now 
         #The PING command is used to test the presence of an active client or 
         #server at the other end of the connection. Servers send a PING 
         #message at regular intervals if no other activity detected coming 
         #from a connection. If a connection fails to respond to a PING 
         #message within a set amount of time, that connection is closed. A 
         #PING message MAY be sent even if the connection is active. 
         #PONG message is a reply to PING message. If parameter <server2> is 
         #given, this message will be forwarded to given target. The <server> 
         #parameter is the name of the entity who has responded to PING message 
         #and generated this message. 

         self.data = self.irc.recv(4096) 
         print self.data 
         if self.data.find ('PING') != -1: 
          self.irc.send(("PONG %s \r\n") % (self.data.split() [ 1 ])) #Possible overflow problem 

         if "!chat" in self.data: 
          ..... 

所以,我可以成功地在導入的文件調用(ipibot)每當 self.data中的'!chat':被調用。

但我不知道如何寫它。這是我迄今爲止

if "!chat" in self.data: 
     user = ipibot.ipibot() 
     user.respond 

我想聲明我已經採取了看的Python模塊部分以及作爲進口我似乎就是無法抓住它,我猜?

文件 - >類 - >功能是我所理解的。

回答

4

模塊不過是一個python源文件。您將該python源文件保存在與其他源文件相同的目錄中,並且可以將該模塊導入其他源文件。當您導入該模塊時,該模塊中定義的類和函數可供您使用。 例如你的情況,你只是做

import ipibot 

在源代碼的頂部,提供了ipibot.py(你的引擎收錄)文件存在於同一個目錄或PYTHONPATH(標準目錄,Python程序可以用於查找一個模塊),然後開始使用ipibot.ipibot()來使用該模塊的功能ipibot()。而已。

+0

yes但是我需要它運行在IRC上它只能運行在IDLE – 21days 2011-01-25 03:33:46

相關問題