Difference between revisions of "Python Development"

From FreeOrionWiki
Jump to: navigation, search
(FAQ)
(Internal)
Line 32: Line 32:
 
== Debugging ==
 
== Debugging ==
 
=== Internal ===
 
=== Internal ===
Debugging from chat coming soon.
+
 
 +
Send 'help' to chat window.
 +
 
 +
Follow instruction.
 +
 
 +
Chat window is not supposed to be console so it looks ugly.
  
 
=== External ===
 
=== External ===

Revision as of 21:46, 11 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

Send 'help' to chat window.

Follow instruction.

Chat window is not supposed to be console so it looks ugly.

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.

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: 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).