Difference between revisions of "Python Development"

From FreeOrionWiki
Jump to: navigation, search
(Python editor)
Line 1: Line 1:
 
This page will be devoted to matters relating to python development in general for the FreeOrion project, including the general boost::python interface.  A separate page is devoted to details specifically regarding the [[AI Python API]].  Other topics relating more specifically to the AI will be covered at [[AI_Development]], and topics relating more specifically to universe creation will be covered at [[Universe_Creation]].  A good understanding of the [[Free_Orion_Content_Script_(FOCS)]] will also likely be very helpful since the use of python in FreeOrion closely relates to scripted content.
 
This page will be devoted to matters relating to python development in general for the FreeOrion project, including the general boost::python interface.  A separate page is devoted to details specifically regarding the [[AI Python API]].  Other topics relating more specifically to the AI will be covered at [[AI_Development]], and topics relating more specifically to universe creation will be covered at [[Universe_Creation]].  A good understanding of the [[Free_Orion_Content_Script_(FOCS)]] will also likely be very helpful since the use of python in FreeOrion closely relates to scripted content.
 +
 +
 +
== Python version ==
 +
 +
Supported python version is 2.7
 +
 +
== Code style ==
 +
 +
C++ API use camelCase style
 +
 +
Python code should be written using general python style according to [https://www.python.org/dev/peps/pep-0008/ PEP8] also usefull to read [https://google-styleguide.googlecode.com/svn/trunk/pyguide.html google recommendations]
 +
 +
 +
== Extend C+ API in python ==
 +
In some cases it is more easy and effective to extend interface of c++ objects from python.
 +
For example string representation of object.
 +
 +
This code located in file default\AI\freeorion_debug\extend_free_orion_AI_interface.py
 +
 +
PS. Add __repr__ and __str__ methods to objects as soon as you need it.
 +
 +
 +
== AI state in save file ==
 +
Instance of AIState is stored as pickled string in save game.
 +
 +
UniverseObject instances should not be stored in AIState, use id instead.
 +
 +
Removing AIState attributes will break save compatibility. Don't remove them,  leave them with comment to remove. To make breaks less frequent.
 +
 +
 +
== Debugging ==
 +
=== Internal ===
 +
Debugging from chat coming soon.
 +
 +
=== External ===
 +
For windows remove Python27.* from installed game folder to use system python(don't forget to install it)
 +
 +
==== PyDev ====
 +
[http://pydev.org/manual_adv_remote_debugger.html Pydev manual]
 +
 +
[http://stackoverflow.com/questions/25018869/pydev-how-to-invoke-debugging-specific-command-from-console-with-breakpoints/25065948#25065948 To open console:] If you want to use the interactive console in the context of a breakpoint, a different approach would be selecting a stack frame (in the debug view) > right-clicking it > pydev > Debug Console (or you can also in the debug view create a new console view connected to the debug session for the same effect).
 +
 +
==== winpdb ====
 +
    [http://winpdb.org/ winpdb ]
  
  
 
== Python editor ==
 
== Python editor ==
It is your choice how to edit python code. Here are some suggestions:
+
  It is your choice how to edit python code. Here are some suggestions:
  
 
- [https://www.jetbrains.com/pycharm/download/ PyCharm community edition]
 
- [https://www.jetbrains.com/pycharm/download/ PyCharm community edition]

Revision as of 18:16, 9 March 2015

This page will be devoted to matters relating to python development in general for the FreeOrion project, including the general boost::python interface. A separate page is devoted to details specifically regarding the AI Python API. Other topics relating more specifically to the AI will be covered at AI_Development, and topics relating more specifically to universe creation will be covered at Universe_Creation. A good understanding of the Free_Orion_Content_Script_(FOCS) will also likely be very helpful since the use of python in FreeOrion closely relates to scripted content.


Python version

Supported python version is 2.7

Code style

C++ API use camelCase style

Python code should be written using general python style according to PEP8 also usefull to read google recommendations


Extend C+ API in python

In some cases it is more easy and effective to extend interface of c++ objects from python. For example string representation of object.

This code located in file default\AI\freeorion_debug\extend_free_orion_AI_interface.py

PS. Add __repr__ and __str__ methods to objects as soon as you need it.


AI state in save file

Instance of AIState is stored as pickled string in save game.

UniverseObject instances should not be stored in AIState, use id instead.

Removing AIState attributes will break save compatibility. Don't remove them, leave them with comment to remove. To make breaks less frequent.


Debugging

Internal

Debugging from chat coming soon.

External

For windows remove Python27.* from installed game folder to use system python(don't forget to install it)

PyDev

Pydev manual

To open console: If you want to use the interactive console in the context of a breakpoint, a different approach would be selecting a stack frame (in the debug view) > right-clicking it > pydev > Debug Console (or you can also in the debug view create a new console view connected to the debug session for the same effect).

winpdb

   winpdb 


Python editor

 It is your choice how to edit python code. Here are some suggestions:

- PyCharm community edition

- Eclipse based.

A good IDE will help you to make fewer mistakes, and keyboard shortcuts and other IDE features can greatly speedup your development. If you are unfamiliar with using an IDE then two key features to be sure to learn are how to quickly navigate to an item's declaration (preferably with a shortcut key), and how to use its code completion feature.

Interactive Debugging via UI

Coming soon...


Deploying code

Possible approaches:

 a) configure game to use AI from your repository folder. (this is best!)
 b) copy code to game folder
 c) edit code in game folder and copy it back to repo

FAQ

- Q: This page can be better / has typo / ...
- A: Welcome to forum, lets do it better.
- Q: Which python version used?
- A: Windows: python shipped with game(2.7.3) Other system use system python.
- Q: Which version is supported by AI and universe generation?  
- A: 2.7 
- Q: Where does print output go?
- A: stdout and stderr redirected to logs in the game settings folder.
- Q: How do I test new code?
- A: start/load a game (changing code during gameplay will not affect a current game; the code is already in memory).