Civ4 Python/import: Difference between revisions

From Civilization Modding Wiki
Jump to navigationJump to search
(Created page with 'import allow you to add python files to you mod. Example: adding popup to the start of turn: '''Bad way:''' Insert popup code to CvEventManager.py Big files is hard to read....')
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Import guide.'''
import allow you to add python files to you mod.
import allow you to add python files to you mod.


Example:
'''Example:'''


adding popup to the start of turn:
How to add popup to the event:
:'''Bad way:'''
* Insert popup code to CvEventManager.py in event.<br />
'''Result:'''<br />
Big files is hard to read and edit. It is not easy to customize this code and merge to others mod.<br />
:'''Good way:'''<br />
* Create file mymod.py<br />
* Create function that runs popup (for example: run_popup)<br />
* edit CvEventManager.py:<br />
** add string after imports: import mymod.py<br />
** and string in event: mymod.run_popup(*args)<br />
'''Result:'''<br />
It is easy to add / remove this function from mod (just uncomment lines).<br />
It is easy to share this function. <br />
It is easy to call this function in other place. (if yon need it on some event).<br />


'''Bad way:'''
import executes script in file (if you have only functions and classes loads them to memory.)
Insert popup code to CvEventManager.py


Big files is hard to read. It is not easy to customize this code and merge to others mod.
=='''import sintax:'''==
* ''import mymod''<br />
to access functions use:<br />
''mymod.foo()''<br />


'''good way'''
* ''from mymod import *''<br />
Create file mymod.py
to access functions use: <br />
Create function that runs popup (run_popup)
''foo()''<br />
edit CvEventManager.py:


add string after imports:
* ''from mymod import foo, goo''<br />
import mymod.py
to access functions use: <br />
''foo()'' or ''goo()''<br />
(some IDE has autocomplite feature and in this way shows only foo and goo.)


and string:  
* ''from mymod import this_is_long_name_function as foo''<br />
mymod.run_popup() there it needs.
to access functions use:<br />
''foo()''<br />
(It is good idea to have functions with understandable name)


It is easy to add / remove this function (just uncomment lines)
== '''Import folders''' ==
It is easy to share.
* Create folder (mymodule)
It is easy to call this function in other place.
* Create __init__.py (leave it blank)
* Create file: myscript.py


'''import sintax:'''
'''Usage'''<br />
from mymodule import myscript <br />
or<br />
from mymodule.myscript import *<br />


import executes file (if you have only functions and classes loads them to memory.)


import mymod
to access functions use: mymod.foo()


from mymod import *
=='''Import rules:'''==
to access functions use: foo()
Use imports at the file beginning!
 
from mymod import foo, goo
to access functions use: foo()
(some IDE has autocomplite feature and in this way shows only foo and goo. if you don use IDE  use *)
 
from mymod import this_is_long_name_function as foo
to access functions us: foo()
(It is good idea to have functions with understandable name)


Import rules:
== References ==
Use imports at the file beginning.
[http://forums.civfanatics.com/showpost.php?p=2495872 Lesson 09 - 'Borrowing' other people's stuff (with modules)]

Latest revision as of 12:55, 17 July 2010

Import guide.

import allow you to add python files to you mod.

Example:

How to add popup to the event:

Bad way:
  • Insert popup code to CvEventManager.py in event.

Result:
Big files is hard to read and edit. It is not easy to customize this code and merge to others mod.

Good way:
  • Create file mymod.py
  • Create function that runs popup (for example: run_popup)
  • edit CvEventManager.py:
    • add string after imports: import mymod.py
    • and string in event: mymod.run_popup(*args)

Result:
It is easy to add / remove this function from mod (just uncomment lines).
It is easy to share this function.
It is easy to call this function in other place. (if yon need it on some event).

import executes script in file (if you have only functions and classes loads them to memory.)

import sintax:

  • import mymod

to access functions use:
mymod.foo()

  • from mymod import *

to access functions use:
foo()

  • from mymod import foo, goo

to access functions use:
foo() or goo()
(some IDE has autocomplite feature and in this way shows only foo and goo.)

  • from mymod import this_is_long_name_function as foo

to access functions use:
foo()
(It is good idea to have functions with understandable name)

Import folders

  • Create folder (mymodule)
  • Create __init__.py (leave it blank)
  • Create file: myscript.py

Usage
from mymodule import myscript
or
from mymodule.myscript import *


Import rules:

Use imports at the file beginning!

References

Lesson 09 - 'Borrowing' other people's stuff (with modules)