Table Of Contents

This Page

vtools.examples package


vtools.examples.os_and_filenames module

Examples of some system, operating system and file/path chores. For more information, use the os and sys modules. Note that if you really want to use the os interactively from python you should use ipython.

vtools.examples.script_style module

Example script with arguments. This style will make it easier to use your script in several settings: 1. Called from another script with new data 2. Used as a standalone script or with the system 3. Called from a batch file

Ideally, the module should have an docstring describing its use. The module name should match the most important public function in the module.


Standalone runnable main function This function will ordinarily know how to get the arguments it needs, either from defaults, a config system or options parsing (which makes batch files easier).

This example uses options parsing with optparse.

OK, a fair question is ‘what is options parsing’. Lets say you want to run a script from the command line. Assuming Python is the default for *.py files, you type:

> –first=hello –second=goodbye -i 4 ????

We are using optparse, and the code below registers –first, –second and -i as option flags. The command above assigns them values hello, goodbye and 4 respectively. There is also a hidden –help option, and if you type

> –help

you will see what it does.

Anything after all the options (???? in this case) is an argument to python. It gets stored in a list called sys.argv. You would can get at them if you type import sys print sys.argv

However, since we are using optparse, it does this for us behind the scenes.

script_style(first, second, myinteger, ending='!!!')

The most important public function returns a string. Note this function will ordinarily have data arguments, not files or file names. Arguments: first string, not None second string, may be omitted myint an integer less than 5


Simple test This is a good place to put a simple test if this is possible.

Module contents