user2033561
user2033561

Reputation:

Include-like in python?

Is there any way of doing an include in python like in php? This is not like the import function

I'm a beginner in python and am writing an Irc Bot.

Let me explain what i wanna do, this is the script so far

import socket

network = 'irc.irchighway.net'
port = 6667
nick = 'bot';
canal = '#channel'
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK '+nick+'\r\n' )
irc.send ( 'USER '+nick+' '+nick+' '+nick+' :Python IRC\r\n' )
irc.send ( 'PRIVMSG nickserv : identify xxxxx\r\n' )
irc.send ( 'JOIN '+canal+'\r\n' )
irc.send ( 'PRIVMSG '+canal+' :Im here\r\n' )
while True:
    data = irc.recv ( 4096 )
    if data.find ( 'PING' ) != -1:
        irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )    
    print data

What i want to do is put an include inside the while, and create another .py, something like this

bot.py

import socket

network = 'irc.irchighway.net'
port = 6667
nick = 'bot';
canal = '#channel'
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK '+nick+'\r\n' )
irc.send ( 'USER '+nick+' '+nick+' '+nick+' :Python IRC\r\n' )
irc.send ( 'PRIVMSG nickserv : identify xxxxx\r\n' )
irc.send ( 'JOIN '+canal+'\r\n' )
irc.send ( 'PRIVMSG '+canal+' :Im here\r\n' )
while True:
    include('function.py')

functions.py

data = irc.recv ( 4096 )
if data.find ( 'PING' ) != -1:
    irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )    
print data

This way i can modify the function.py file and change how the bot behaves without having to stop the script, which means the bot will disconnect and reconnect to the irc server.

Is there any way of doing that?

Upvotes: 1

Views: 237

Answers (2)

cha0site
cha0site

Reputation: 10737

You could do this... But this is just for debugging, ok?

bot.py:

import socket
import helper

def main():
    network = 'irc.irchighway.net'
    port = 6667
    nick = 'bot';
    canal = '#channel'
    irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    irc.connect ((network, port, ))
    print irc.recv(4096)
    irc.send ('NICK '+nick+'\r\n')
    irc.send ('USER '+nick+' '+nick+' '+nick+' :Python IRC\r\n')
    irc.send ('PRIVMSG nickserv : identify xxxxx\r\n')
    irc.send ('JOIN '+canal+'\r\n' )
    irc.send ('PRIVMSG '+canal+' :Im here\r\n')
    while True:
        reload(helper)
        helper.do_thing(irc)

if __name__ == '__main__':
    main()

helper.py:

def do_thing(sock):
    data = sock.recv(4096)
    if 'PING' in data:
        irc.send ('PONG ' + data.split()[1] + '\r\n')    
    print data

Upvotes: 0

jsbueno
jsbueno

Reputation: 110666

There is not, and there should not be. That simple! If you think you have to generate code on the fly (hint: generally you don't), do it in a string in the file you are running, and compile it with "exec" - no need to write it as Python code in a text file and import it.

now, you could use the "reload" built-in function to do what you want as you describe - But it is far away frombeing a clean design.

What you "can't do" really is to have another file to be pasted as text in the current file before compilation - like it happens in PHP.

Upvotes: 1

Related Questions