1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-09-28 16:11:42 +00:00
sitc/python/1__10_Modules_Packages.ipynb
2016-02-01 13:05:58 +01:00

188 lines
4.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](http://www.upm.es/sfs/Rectorado/Gabinete%20del%20Rector/Logos/UPM/EscPolitecnica/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Modules and Packages\n",
"\n",
"*Modules* contains definitions and objects that can be reused in other programmes.\n",
"\n",
"*Packages* contain subpackages of modules. They are directories and provide the namespace for modules.\n",
"\n",
"For example, let's have this package sound, a subpackage effects and the modules echo and surround:\n",
"\n",
"<code>\n",
"babel/\n",
" messages/\n",
" \\_\\_init\\_\\_.py\n",
" plurals.py\n",
" catalog.py\n",
"</code>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'plurals' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-9-1ca6b7751408>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# We can import the module plural with import\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbabel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessages\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplurals\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mplurals\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_plural\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'plurals' is not defined"
]
}
],
"source": [
"# We can import the module plural with import, but we should use the full name\n",
"import babel.messages.plurals\n",
"plurals.get_plural()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import babel.messages.plurals\n",
"babel.messages.plurals.get_plural()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from babel.messages import plurals # with from-import, we can use the short name\n",
"plurals.get_plural()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from babel.messages.plurals import get_plural # now we can use directly get_plural()\n",
"get_plural()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To write a module, it is just needed to include the code in a file ended with \".py\" and the name of the file is the name of the module.\n",
"\n",
"Packages are directories that should have a file \\_\\_.init\\_\\_.py, that can be an empty file or contain initialization code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}