AI Ladder, 0.5 - Page 10

AI Ladder, 0.5

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

aegis wrote:hibernate?
https://www.hibernate.org/

It makes storing / retrieving things to/from databases really easy in C# and Java. Basically, you just handle lists and objects in C#/Java, and hibernate handles storing them in the db if they change, and retrieving them from the db when you want it to.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

To be honest, since in python, data seems to be handed around as dictionaries and lists anyway, it's half done, for the retrieval side, since you can get such a list of dictionaries in two lines:

Code: Select all

dictcursor.execute("select * from maps")
maps = dictcursor.fetchall()
... then the question is how to change some stuff in maps, and inject the stuff that changed back into the db

... or, more simply, and probably 99% of uses, be able to give a single dictionary (=single 'object') to something, and get that to be pumped automatically into a table.

...also, the question of how to handle joins

PS we have headless spring now. Do you fancy setting up a botrunner or two?
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

Perhaps something like:

Code: Select all

def doinsert( tablename, map ):
   global dictcursor

   sqlinsert = "insert into " + tablename + " ("
   isfirstkey = True
   for key in newmap.keys():
      if not isfirstkey:
         sqlinsert = sqlinsert + " , "
      sqlinsert = sqlinsert + key
      isfirstkey = False
   sqlinsert = sqlinsert + ") values ("
   isfirstkey = True
   for value in newmap.values():
      if not isfirstkey:
         sqlinsert = sqlinsert + " , "
      sqlinsert = sqlinsert + " %s "
      isfirstkey = False
   sqlinsert = sqlinsert + ")"
   #print sqlinsert
   dictcursor.execute( sqlinsert, newmap.values() )
Usage:

Code: Select all

newmap = { 'map_name': 'foo1', 'map_archivechecksum': 12343513, 'map_id': 11, 'map_url': '' }
doinsert("maps", newmap )
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

Addendum: we'd also need some way of identifying which fields are the keys so we can choose automatically between inserting and updating.

I don't imagine I'm the first person to want to do this. There's probably something out there somewhere, but I didn't find it yet.

Or maybe I'm looking for the wrong thing, and there's another way of doing this, which is at least as easy, but not quite the same...
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

prototype for simple hibernate for python: http://pastebin.com/f2dfc92da

usage example:

Code: Select all

print pytodb.doselect("maps")

newmap = { 'map_name': 'foo1', 'map_archivechecksum': 12343513, 'map_id': 11, 'map_url': '' }
pytodb.doinsert("maps", newmap )
print pytodb.doselect("maps")

newmap2 = { 'map_name': 'new map name', 'map_archivechecksum': 135135, 'map_id': 11, 'map_url': '' }
pytodb.doinsertorupdate("maps", ['map_id'], newmap2 )
print pytodb.doselect("maps")

pytodb.dodelete( "maps", ['map_id'], newmap )
print pytodb.doselect("maps")
Whether that's easier to use than coding the sql by hand is another question ;-)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: AI Ladder, 0.3

Post by imbaczek »

sqlalchemy.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

imbaczek wrote:sqlalchemy.
Sweet! I'll check that out. The website gives a great first impression, looks professional, so I'm looking forward to checking that out in more detail.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

Well, that really does seem to behave exactly like hibernate, so I guess what I was looking for was: an object-relational-mapper, and hibernate happens to be one specific implementation of one.
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: AI Ladder, 0.3

Post by aegis »

yeah, I used sqlalchemy in uberserver and I'm a fan.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

aegis wrote:yeah, I used sqlalchemy in uberserver and I'm a fan.
Make sure to tell me that *after* I've spent ages figuring it out ;-)
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.4

Post by hughperkins »

Release 0.4:

http://manageddreams.com/ailadder/downl ... .0.tar.bz2

Finished migrating to xmlrpc.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

Right, so sqlalchemy looks like exactly what I was looking for.

There's a new branch sqlalchemy in git where I've started moving to sqlalchemy.

http://github.com/hughperkins/ailadder/tree/sqlalchemy

It seems to work great. Some of the hardest pages are migrated, and look a lot simpler, eg viewrequests.py and viewresults.py.

sqlalchemy needs at least python 2.4 but it also makes it easy to use sqlite and so on for those who want.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: AI Ladder, 0.3

Post by imbaczek »

now you just need to get rid of those fugly prints; use a template language like jinja or mako.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: AI Ladder, 0.3

Post by AF »

Gets a lot further but, error:
error.JPG
(128.16 KiB) Downloaded 11 times
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: AI Ladder, 0.3

Post by aegis »

looks like normal internet error
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

aegis wrote:looks like normal internet error
So you reckon: just put a try...except around it?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: AI Ladder, 0.3

Post by aegis »

sure.
python has an awesome ability to recover from mostly anything more mild than a segfault, don't waste it ;)
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

imbaczek wrote:now you just need to get rid of those fugly prints; use a template language like jinja or mako.
Ah, sweet! Which do you recommend? Or I just pick one at random and run with that?

I think I will finish migrating to sqlalchemy first though...

You know, it would have been nice to know about this stuff *before* actually coding 80% of the site ;-)
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

AF wrote:Gets a lot further but, error:
error.JPG
Hi AF. The verdict seems to be it's a transient error, and the code just needs not to crash during the transient errors.

This new version:

http://manageddreams.com/ailadder/downl ... .1.tar.bz2

... has the getserverrequest surrounded by a try ... except block, so may resolve the issue you are seeing.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Re: AI Ladder, 0.3

Post by hughperkins »

Guys,

Any hints on the installation process for the website?

I'm thinking, for sqlalchemy, there are a few possibilities:
- ask people to install it themselves, check it is installed
- deploy sqlalchemy with the ailadder setup package, set it up automatically as part of the setup if it's not already installed
- deploy sqlalchemy with the ailadder setup package, and also virtualenv ( http://pypi.python.org/packages/source/ ... 3.4.tar.gz) set it up automatically as part of the setup, to a virtualenv, if it's not already installed
- something else... ?

Thoughts?
Post Reply

Return to “AI”