{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n",
"===================================\n",
"First Edition\n",
"-----------------------------------\n",
"\n",
"Chapter 8: Modules\n",
"=============================\n",
"_____________________________\n",
"For Python, modules are source files that can be imported into a program. They can contain any Python structure and run when imported. They are compiled when first imported and stored in a file (with the extension \".pyc\" or \".pyo\"), have their own *namespaces* and support *Doc Strings*. They are singleton objects (only one instance is loaded into memory, which is available globally for the program).\n",
"\n",
"\n",
"\n",
"The modules are located by the interpreter through the list of folders `PYTHONPATH` (sys.path), which usually includes the current directory first.\n",
"\n",
"The modules are loaded with the `import` statement. Thus, when using a module structure, it is necessary to identify the module. This is called absolute import."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"print os.name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"posix\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also import modules with relative form:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from os import name\n",
"print name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"posix\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To avoid problems such as variable obfuscation, the absolute import is considered a better programming practice than the relative import.\n",
"\n",
"Example of module:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# File calc.py\n",
"\n",
"# Function defined in module\n",
"def average(list):\n",
"\n",
" return float(sum(list)) / len(list)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of module usage:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Imports calc module\n",
"import calc\n",
"\n",
"l = [23, 54, 31, 77, 12, 34]\n",
"\n",
"# Calls the function defined in calc\n",
"print calc.average(l)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"38.5\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The main module of a program has the variable `__name__` equals to `__main__`, thus it is possible to test if the main module:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if __name__ == \"__main__\":\n",
" # Code here will only be run \n",
" # if it is the main module\n",
" # and not when it is imported by another program\n",
" pass"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That way it is easy to turn a program into a module.\n",
"\n",
"Another module example:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"\"\"\n",
"modutils => utility routines for modules\n",
"\"\"\"\n",
"\n",
"import os.path\n",
"import sys\n",
"import glob\n",
"\n",
"def find(txt):\n",
" \"\"\"find modules with name containing the parameter\n",
" \"\"\"\n",
"\n",
" resp = []\n",
"\n",
" for path in sys.path:\n",
" mods = glob.glob('%s/*.py' % path)\n",
"\n",
" for mod in mods:\n",
" if txt in os.path.basename(mod):\n",
" resp.append(mod)\n",
"\n",
" return resp"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example module use:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from os.path import getsize, getmtime\n",
"from time import localtime, asctime\n",
"\n",
"import modutils\n",
"\n",
"mods = modutils.find('xml')\n",
"\n",
"for mod in mods:\n",
"\n",
" tm = asctime(localtime(getmtime(mod)))\n",
" kb = getsize(mod) / 1024\n",
" print '%s: (%d kbytes, %s)' % (mod, kb, tm)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"/usr/lib/python2.7/xmlrpclib.py: (50 kbytes, Fri Apr 19 16:20:45 2013)\n",
"/usr/lib/python2.7/xmllib.py: (34 kbytes, Fri Apr 19 16:20:45 2013)\n",
"/usr/lib/python2.7/dist-packages/libxml2.py: (335 kbytes, Wed May 1 14:19:10 2013)\n",
"/usr/lib/python2.7/dist-packages/drv_libxml2.py: (14 kbytes, Wed May 1 14:19:10 2013)\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Splitting programs into modules makes it easy to reuse and locate faults in the code."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
""
],
"output_type": "pyout",
"prompt_number": 1,
"text": [
""
]
}
],
"prompt_number": 1
}
],
"metadata": {}
}
]
}