Introducing Myself by lrh9

For topics that do not fit in another sub-forum.

Moderators: Oberlus, Oberlus

Post Reply
Message
Author
lrh9
Krill Swarm
Posts: 12
Joined: Mon Jul 02, 2012 2:40 pm

Introducing Myself by lrh9

#1 Post by lrh9 » Mon Jul 02, 2012 4:47 pm

Hello. I enjoy space 4x games and I can program in Python. I am thinking about trying to contribute to FreeOrion's Python artificial intelligence, but I wanted to introduce myself first.

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()

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 12484
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Introducing Myself by lrh9

#2 Post by Geoff the Medio » Mon Jul 02, 2012 5:08 pm

lrh9 wrote:...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?
Yes, AI scripting falls under programming. Post any questions or ideas you want to discuss there.

lrh9
Krill Swarm
Posts: 12
Joined: Mon Jul 02, 2012 2:40 pm

Re: Introducing Myself by lrh9

#3 Post by lrh9 » Mon Jul 02, 2012 5:09 pm

Geoff the Medio wrote:
lrh9 wrote:...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?
Yes, AI scripting falls under programming. Post any questions or ideas you want to discuss there.
Thank you.

User avatar
eleazar
Design & Graphics Lead Emeritus
Posts: 3858
Joined: Sat Sep 23, 2006 7:09 pm
Location: USA — midwest

Re: Introducing Myself by lrh9

#4 Post by eleazar » Mon Jul 02, 2012 5:22 pm

Welcome!
The AI currently is pretty sad, we could use some more hands on board.

lrh9
Krill Swarm
Posts: 12
Joined: Mon Jul 02, 2012 2:40 pm

Re: Introducing Myself by lrh9

#5 Post by lrh9 » Mon Jul 02, 2012 9:29 pm

eleazar wrote:Welcome!
The AI currently is pretty sad, we could use some more hands on board.
Thanks!

I know how to use IRC and I am on the FreeOrion channel if anyone ever wants to chat.

Post Reply