导入脚本到另一个脚本
问题描述:
我试图导入该文件导入脚本到另一个脚本
到这个文件
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模块部分以及作为进口我似乎就是无法抓住它,我猜?
文件 - >类 - >功能是我所理解的。
答
模块不过是一个python源文件。您将该python源文件保存在与其他源文件相同的目录中,并且可以将该模块导入其他源文件。当您导入该模块时,该模块中定义的类和函数可供您使用。 例如你的情况,你只是做
import ipibot
在源代码的顶部,提供了ipibot.py(你的引擎收录)文件存在于同一个目录或PYTHONPATH
(标准目录,Python程序可以用于查找一个模块),然后开始使用ipibot.ipibot()
来使用该模块的功能ipibot()
。而已。
yes但是我需要它运行在IRC上它只能运行在IDLE – 21days 2011-01-25 03:33:46