Python tips

正在查看此主题的用户

Cjkjvfnby

Regular
Game module information is stored in python variables. Here some python tips.
1. use IDE
There is some python IDE, I use Aptana Studio 3(http://www.aptana.com/products/studio3/download) 
Some other ide has same features (SciTE,  Notepad++,  Sublime Text2)
IDE is a program to edit code. It has many useful features.  I will describe some of them:

сode highlighting
syntax check (all python syntax errors are highlighted)  Or just run script and see if any syntax errors
easy comment/uncomment lines just select string and press ctrl+. (dot)
bracket count (easy way to find bracket pair)
file navigator and edit window in one screen.
all opened files as tabs, not single window as in win notepad
better search and replace  when win notepad
search in all project files/all opened files
some great(nasty) features as autocomplite quotes and brackets (some dont does not like them)
navigation via imports (I will explain it in post about splitting modules to separate files.)

IDE shortages:
it works little slower when notepad. (Depends of you PC.)
After working with IDE it is hard to use notepad, it looks castrated.

2. list and tuple.
Both are containers for variables.  If you dont know which to use, use list.
Tuple:
Variables in round brackets:
插入代码块:
(eq, ":is_unique", 1) # this is tuple
(1,) # this is tuple too. Because comma at the end. 
(try_begin) #this is not tuple, you can remove brakets, nothing will change. 
Blank tuple is never used.

List:
Variables in square brackets:
插入代码块:
[eq, ":is_unique", 1] # this is list
[1,] # this is list too.  
[try_begin] #and this is list too!!! 
[] blank list.

#Lists can be combined:
a = [1,2]
b= [3,4]
>>> a+b
>>> [1,2,3,4]


3. Splitting module_*.py
Why ?
When you file has over9000 lines it is not funny to scroll it. (Native module_scripts ~ 20000)
With IDE it is easy to work with many single files.
If you work in teem and use version system, you will have less merges.

Lets do it for scripts:
Steps:
1. create new subfolder (scripts)
2. create file  __init__.py in scripts
3. create new python file for scripts. test.py
4. copy-paste import section from module_scripts.py
插入代码块:
from header_common import *
from header_operations import *
from module_constants import *
from header_parties import *
from header_skills import *
from header_mission_templates import *
from header_items import *
from header_triggers import *
from header_terrain_types import *
from header_music import *
from ID_animations import *
5. create you own script/scripts
插入代码块:
new_script = [
    ("my_new_script",
          [
               (display_message,"@Boooooo."
         ]
    )
]
6. open from module_scripts.py
at the end add
插入代码块:
from scripts.test import  new_scripts
scripts = sctipts + new_scripts # adds you script to the end of scripts.
CTRL+CLICK on "new_scripts" in IDE will open it in other tab.

To test it just compile module system and search you script name. (print scripts[-1] # just print last script.)

4. You can use python scripting to bulk modify you modules.  For example add to all units/items name its level/price/type during testing and easy turn it off in release.

5. Stings usage.
Sting is python object. It is 4metods to make string.
1. Single qoute/double 'string' "string"
2. Triple qoute/double '''string''' , """string"""

Examples:
插入代码块:
" here you can use ' (single quote) "
' here you can use " (double quote) '
''' here you can use ', ", """ and
multylining'''


another way of usage of tripple qoutes is commenting files (Don` do it inside structures)
插入代码块:
'''
All text inside tripple quote (or tripple double quote) is ignored by module system compiler.
But don`t use it inside lists. You can use it for making comment inside header_files.
'''
 
后退
顶部 底部