If I am welcomed here, where is the most appropriate place for me to post about the Python artificial intelligence? I see a Programming topic, but the description advises that the topic is primarily for developers. Does that include the Python artificial intelligence developers?
My primary experience is with writing standalone scripts. I don't know the best way to use third party modules and packages with the Python artificial intelligence. Should I try to manipulate the bundled Python, or should I add a module or package directory to sys.path? What would be the best place for that directory?
I can code in Python 3 and Python 2. Here is an example of my work. It is a simple Python 3 IRC bot framework with asynchronous I/O and an event-oriented architecture.
Code: Select all
import asynchat
import asyncore
import collections
import socket
#hack substitute for regular expressions or even a parser
def parse(message):
data = message.split(' ')
prefix = None
if data[0].startswith(':'): #prefix present
prefix = data[0][1:]
data = data[1:]
command = data[0]
data = ' '.join(data[1:])
if data.startswith(':'): #only trailing parameter
parameters = [data[1:]]
else:
data = data.split(' :')
trailing = None
if len(data) > 1: #trailing parameter present
trailing = ' :'.join(data[1:])
parameters = data[0].split(' ') #regular parameters
if trailing is not None:
parameters.append(trailing) #add trailing parameter to regular parameters
return prefix, command, parameters
class Connection(asynchat.async_chat):
def __init__(self, nick, user, realname, host, port=6667):
self.nick = nick
self.user = user
self.realname = realname
self.address = (host, port)
self.received = list()
asynchat.async_chat.__init__(self)
self.set_terminator(b'\n')
self.handlers = collections.defaultdict(set)
def collect_incoming_data(self, data):
self.received.append(data)
def found_terminator(self):
data = b''.join(self.received).rstrip(b'\r')
del self.received[:]
prefix, command, parameters = parse(data.decode())
#comment out next line
#print(prefix, command, parameters)
for handler in self.handlers[command]:
try:
handler(self, prefix, parameters)
except Exception as exception:
for handler in self.handlers[exception]:
handler(self, exception)
def message(self, string):
string = ''.join((string, '\r\n'))
self.push(string.encode())
def establish(self):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(self.address)
def subscribe(self, topic, handler):
self.handlers[topic].add(handler)
def unsubscribe(self, topic, handler):
self.handlers[topic].discard(handler)
def handle_connect(self):
for handler in self.handlers['connect']:
try:
handler(self)
except Exception as exception:
for handler in self.handlers[exception]:
handler(self, exception)
if __name__ == '__main__':
#define event handlers here
def on_keyerror(connection, exception):
print(connection, exception)
def pong(connection, prefix, parameters):
connection.message('PONG {0}'.format(parameters[0]))
def on_connect(connection):
connection.message('NICK {0}'.format(connection.nick))
connection.message('USER {0} {1} bla :{2}'.format(connection.user, connection.address[0], connection.realname))
#additional code
class Channel:
def __init__(self, channel):
self.channel = channel
def __call__(self, connection, prefix, parameters):
connection.message('JOIN {0}'.format(self.channel))
#set up connections here
c = Connection('lrh9bot', 'lrh9bot', 'lrh9bot', 'irc.dal.net')
#registering handlers to events
c.subscribe(KeyError, on_keyerror)
c.subscribe('PING', pong)
c.subscribe('connect', on_connect)
c.subscribe('372', Channel('#ai'))
c.establish()
#start asyncore
asyncore.loop()