Not having fun with Python

Once hooked in, Python is very nice for coding.  Where it is NOT nice, so far at least, is the work having to expose new APIs to it.  I must be doing something wrong because it seems like there’s a lot of work to expose methods and such.

And worse, even once I do that, IDLE (the Python shell I’m using) doesn't seem to have any intellisense for the imported APIs and classes.

45,567 views 22 replies
Reply #1 Top

Have you tried the open source Komodo Edit IDE? I like it but I am just a hack who started learning Python this summer - so don't take my word ;)

EDIT: http://www.activestate.com/komodo_edit/

Reply #3 Top

Ouch, sorry to hear that exposure and intellisense isn't working as it should.

I've never tried exposing c++ to python, but this library/tutorial looks promising:

http://www.boost.org/doc/libs/1_40_0/libs/python/doc/tutorial/doc/html/python/exposing.html

Not sure if you want to use the Boost library but the license seems pretty explicitly designed so that you can use it commercially with no cost or hassle: http://www.boost.org/users/license.html .

Does that help at all?  I'd keep looking but it's quite likely you've already looked at all this stuff.

Reply #4 Top

I can't believe you're using IDLE. Both Komodo and Eclipse are much better in my experience. I wasn't aware of Komodo Edit, but looking on activestate's website it looks nice other than the lack of debugging and interactive shells.

Reply #5 Top

Bah eclipse! Use VI you'll have a much better experience. :D

All joking aside your definitely better off using Komodo or Eclipse. IDLE is way too clunky.

Reply #6 Top

Don't bother with IDLE. I haven't tried the Eclipse plug-in or Komodo, but the IDE I use (apart from Textpad or Vim) is SPE -    http://pythonide.blogspot.com/

 

To write bindings for Python, have you tried looking at SWIG yet? I tried writing Python bindings for a library by hand once. These days I'd either use SWIG or just use ctypes.

Reply #7 Top

If you want a python shell try ipython, it is superior to IDLE.  (Lots of help, easy access to input/output from other executables, great 'history' features ...)

http://ipython.scipy.org/moin/

 

 

Reply #8 Top

even NetBeans has a far far superior Python system then IDLE

...who the hell would use IDLE anymore??

Reply #10 Top

I'm wondering if IDLE was selected because the IEnumerable<PythonIDE> property returned a ridiculous number of elements and he just called First();

I probably would have, being new to it.

Reply #13 Top

Mini Dev Journal:

In my opinion, you can't really appreciate the advantages of something like Boost.Python until you've done it the long way first.  On top of that, when we first started Elemental, we wanted to see what our possibilities were as far as scripting went.

So we started looking into Python.  I was assigned to get it in the game and doing something... anything.  So I started with a script called Music.py.  It was called once per turn.  The script would get the turn number from the game, and would tell the sound system to play a different track depending on the turn number (for turns 1, 5, and 10 if I remember correctly).  Again, this was just a test bed, and sort of a proof-of-concept.  So we used the Python/C API, because that's what the python docs show you how to do. 

During this time, I also created a more complicated test script called Missions.py that would create mission objects on the map, and when the player collided with them, it would call the script again to see what to do.  Still, this was just a test to see how things worked.

Fast forward a bunch of time, and we're back to working on Elemental full steam and we realize that the python stuff hasn't been touched since way back when we did these test scripts.  But, as we work on AI (a very iterative process), we find that if we could just get it to work via python then we could iterate much faster with our AI behavior changes without having to close the game, change the c++ code and then compile and run the game again.

So again, I use the python/c API to expose parts of our c++ AI functionality to a script.  One reason for this is because I already know how it works (because of having done it for test scripts already).  Another reason is that it needed to be done soon.  The third reason is to see how much code it actually takes to expose this stuff using python/c API.

In the script, which is called once per turn per AI player, it would just test to see if it could build a city, and then do it if so (this functionality is exposed from the game).  Clearly this isn't smart AI, they don't even move around, they just plop down one city and camp there.  But, it was a quick and dirty way to see how much python/c API wrapper code was necessary to even expose this little amount of functionality.

Now that we had experienced the pain of doing it the "hard" way, it was time to look to the "supposedly easier" way.  Enter Boost.Python.  Boost is a collection of all sorts of useful and common functionality that isn't already built into the standard c++ language.  Some things that were part of Boost in the past have now become part of the standard c++ language.  We use various parts of Boost throughout our games, even in GalCiv2.  Anyway, Boost.Python is a section of Boost that deals with working with Python.

That's kind of where we're at right now.  Learning how Boost.Python works so we can use it and see the savings in wrapper code, so that we can quickly expose parts of our game to python so that the AI can be quickly iterated on using python scripts that can be changed while the game is running.

~C

ps.  To answer some replies above:

Q: Don't you know about Boost.Python
A: Yes.  And I'm neck deep in the docs but thanks for the link anyway.

Q: I can't believe you're using IDLE. ...IDLE is way too clunky ...who the hell would use IDLE anymore??
A:
It came with the Python install.  We haven't written much Python code yet, in fact the test scripts were written in notepad if I remember correctly.  The AI one is where I used IDLE, because the python docs mention it and it was already on my PC from when I installed Python.  We do appreciate the other IDE suggestions, so thanks!

Q: have you tried looking at SWIG yet?
A: There was an article about SWIG in Game Developer magazine this month.  It discussed using SWIG with Lua and C# though, but did mention that it could be used with Python and C++.  In the end I think we'll likely stick with Boost.Python.

Reply #14 Top

Crazy C,

Thanks for the insight, that makes a lot of sense.  I also have to do things the long way around before using generalized tools from other people, and often I just wind up writing my own generalizations once I've figured out the process from nuts and bolts (as an example, I started trying to adapt our web app development to ASP.NET and wound up writing my own custom HttpHandler and pure C# app structure).  All that to say I really sympathize with not just starting with something like Boost.Python off the bat.

By the way, how much overhead are these python calls going to have?  I had heard that Civ4's python calls involved building a python instance in a new thread, running it, then tearing it down afterward.  Will y'all be doing the same thing or can you just create a python thread (or threads) at the start and pass the calls/messages to them as needed?

Reply #15 Top

Just throw self. in front of EVERYTHING

EVERYWHERE

It fixes it. Trufax.

Also, today I spent an today hour writing Python to write SQL D: Just one of it's everyday uses.

Reply #16 Top

I am not having fun with python either, mine decided it would be funny to take a huge dump when I had guests over... Oh wait this is the coding python your talking about not an actual python. My bad :D

Reply #17 Top

Working with python in Elemental should be as easy as

import awesomeness

and you're done.:)

Reply #18 Top

I thought AI programming was easy

 

Select Case monster

   Case goblin : "kill"

   Case wolf    : "kill"

   Case dragon : "run"

   Else "ignore"

 

:)

Reply #19 Top

That would actually be more intelligent than many AIs shipped with games nowadays.

Reply #20 Top

I toyed with several Python IDEs for a while but I mostly used Eclipse for all my javascript/php/ruby work in the past so PyDev was a natural solution.

And I must say I'm pretty content with PyDev. It's fairly complete with coding assist with auto import, debugging, interactive shell... you name it (most interesting features are non-free however).
If you can cope with Eclipse general hogginess, PyDev seems a very nice choice.

Reply #21 Top

I don't what implementation and what version of Python you're using in Elemental (IronPython ~2.6 ?) but this should make things easier if you intend to use Python 3k: http://mail.python.org/pipermail/python-ideas/2009-October/006305.html

About IronPython and IDEs btw: http://lynanda.com/mediawiki/index.php/Main_Page

Reply #22 Top

sad to hear there is trouble, but CrazyC made it sound like you guys are determined to make it work...   so :beer:     I'm rooting for you and excited to hear about when it pays off.