Example 2.1. odbchelper.py
def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.
Returns string.
"""
return ";".join(["%s=%s" % (k,v) for k,v in params.items()])
if __name__ = "__main__":
myParams = {"server":"mpilgrim",\
"database":"master",\
"uid":"sa",\
"pwd":"secret"
}
print buildConnectionString(myParams)
Output:
server=mpilgrim;uid=sa;database=master;pwd=secret
You can document a Python function by giving it a doc string.
Triple quotes “”“ used when defining a doc string.
- Triple quotes are useful to define a string with both single and double quotes.
- Doc string, if it exists, must be the first thing defined in a function (that is, the first thing after the colon).
Example 2.3. Accessing the buildConnectionString Function’s doc string
In [1]: import odbhelper
In [2]: params = {"server":"mpilgrim","database":"master","uid":"sa","pwd":"secret"}
In [3]: print odbhelper.buildConnectionString(params)
Out[3]: server=mpilgrrim;uid=sa;database=master;pwd=secret
In [4]: print odbhelper.buildConnectionString.__doc__
Out[4]: Build a connection string from a dictionary
Returns string.
Example 2.4. Import Search Path
In [1]: import sys
In [2]: sys.path
Out[2]: ['',
'/usr/local/lib/python2.7/dist-packages/pymc-2.2-py2.7-linux-i686.egg',
'/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/local/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-couch',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
'/usr/lib/python2.7/dist-packages/IPython/extensions']
In [3]: sys
Out[3]: <module 'sys' (built-in)>
In [4]: sys.path.append(os.path.abspath('sphinxext'))
Example 2.5. Indenting the fib Function
def fib(n):
# you can mix and match to print several thing on one line
# by using a comma-separated list of values.
# Each value is printed on the same line, separated by spaces (i.e., "n = 5")
print 'n = ', n
if n > 1:
return n * fib(n-1)
else:
print 'end of the line'
return 1
Python modules are objects and have several useful attributes. You can use this to easily test your modules as you write them.
if __name__ = "__main__":
parentheses are not required around the if expression
all modules have a built-in attribute __name__, A module’s __name__ depends on how you’re using the module.
- If you import the module, then __name__ is the module’s filename, whithout a directory path or file extension
- You can also run the module directly as a standalone program, in which case __name__ will be a special default value, __main__
In [1]: import odbhelper
In [2]: odbhelper.__name__
Out[2]: 'odbhelper'