Commit a9ceb33c authored by paugier's avatar paugier
Browse files

Simplify presentation of import

parent 5342e919
......@@ -68,17 +68,6 @@
"\n",
"def print_variables():\n",
" print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')\n",
"\n",
"print('in util.py, __name__ =', __name__)\n",
"# __name__ is a special variable always defined.\n",
"# its value depends on how the file is called (directly executed or imported)\n",
"if __name__ == '__main__':\n",
" # this code is executed only in the file is directly executed\n",
" print('the module util.py has been directly executed')\n",
" print_variables()\n",
" print('end of util.py')\n",
"else:\n",
" print('the module util.py has been imported')\n",
"\n"
]
}
......@@ -97,11 +86,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"begin of util.py\n",
"in util.py, __name__ = __main__\n",
"the module util.py has been directly executed\n",
"in function print_variables: myvar0 = 0; myvar1 = 1\n",
"end of util.py\n"
"begin of util.py\n"
]
}
],
......@@ -140,12 +125,6 @@
"myvar1 += 100\n",
"print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')\n",
"print_variables()\n",
"\n",
"print('in prog.py, __name__ =', __name__)\n",
"# __name__ is a special variable always defined.\n",
"# its value depends on how the file is called (directly executed or imported)\n",
"if __name__ == '__main__':\n",
" print('end of prog.py')\n",
"\n"
]
}
......@@ -165,12 +144,8 @@
"output_type": "stream",
"text": [
"begin of util.py\n",
"in util.py, __name__ = util\n",
"the module util.py has been imported\n",
"in prog.py, util.myvar0 = 100; myvar1 = 101\n",
"in function print_variables: myvar0 = 100; myvar1 = 1\n",
"in prog.py, __name__ = __main__\n",
"end of prog.py\n"
"in function print_variables: myvar0 = 100; myvar1 = 1\n"
]
}
],
......@@ -178,6 +153,85 @@
"run ../pyfiles/example0/prog.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning: files imported more than once are executed only once per process."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Multi-file program (example with 2 files) and imports\n",
"\n",
"`if __name__ == \"__main__\": ...`\n",
"\n",
"- ../pyfiles/example1/util.py\n",
"- ../pyfiles/example1/prog.py"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"print('begin of util.py')\n",
"myvar0 = 0\n",
"myvar1 = 1\n",
"\n",
"def print_variables():\n",
" print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')\n",
"\n",
"print('in util.py, __name__ =', __name__)\n",
"# __name__ is a special variable always defined.\n",
"# its value depends on how the file is called (directly executed or imported)\n",
"if __name__ == '__main__':\n",
" # this code is executed only in the file is directly executed\n",
" print('the module util.py has been directly executed')\n",
" print_variables()\n",
" print('end of util.py')\n",
"else:\n",
" print('the module util.py has been imported')\n",
"\n"
]
}
],
"source": [
"with open('../pyfiles/example1/util.py') as file:\n",
" print(file.read())"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"begin of util.py\n",
"in util.py, __name__ = __main__\n",
"the module util.py has been directly executed\n",
"in function print_variables: myvar0 = 0; myvar1 = 1\n",
"end of util.py\n"
]
}
],
"source": [
"run ../pyfiles/example1/util.py"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -193,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
......@@ -220,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
......@@ -314,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -333,7 +387,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
......@@ -366,7 +420,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"scrolled": true
},
......@@ -375,7 +429,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['', '/home/pierre/.pyenv/versions/3.6.6/lib/python36.zip', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/lib-dynload', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages', '/home/pierre/Dev/fluiddyn', '/home/pierre/Dev/fluidimage', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/PIMS-0.4.1-py3.6.egg', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/slicerator-0.9.8-py3.6.egg', '/home/pierre/Dev/pythran', '/home/pierre/Dev/fluidfft', '/home/pierre/Dev/fluidpythran', '/home/pierre/Dev/fluidsim', '/home/pierre/Dev/fluidsim_ocean', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/astunparse-1.6.1-py3.6.egg', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/wheel-0.32.2-py3.6.egg', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/setuptools_scm-3.1.0-py3.6.egg', '/home/pierre/.pyenv/versions/3.6.6/lib/python3.6/site-packages/IPython/extensions', '/home/pierre/.ipython']\n"
"['/home/pierre/Output/Teach/py-training-2017/ipynb', '/home/pierre/.pyenv/versions/3.7.2/lib/python37.zip', '/home/pierre/.pyenv/versions/3.7.2/lib/python3.7', '/home/pierre/.pyenv/versions/3.7.2/lib/python3.7/lib-dynload', '', '/home/pierre/.local/lib/python3.7/site-packages', '/home/pierre/.pyenv/versions/3.7.2/lib/python3.7/site-packages', '/home/pierre/Dev/fluiddyn', '/home/pierre/Dev/fluidlab', '/home/pierre/Dev/pythran', '/home/pierre/Dev/beniget', '/home/pierre/Dev/transonic', '/home/pierre/Dev/fluidsim', '/home/pierre/Dev/fluidfft', '/home/pierre/Dev/mpi4py-fft', '/home/pierre/.pyenv/versions/3.7.2/lib/python3.7/site-packages/IPython/extensions', '/home/pierre/.ipython']\n"
]
}
],
......@@ -399,7 +453,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [
{
......@@ -428,11 +482,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"./files/file_to_read.txt\n",
"./files/file_to_read.txt\n"
]
}
],
"source": [
"# Building a path to a file to read...\n",
"directory_path = './files/'\n",
......@@ -454,9 +517,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'/home/pierre/opt/miniconda3/lib/python3.6'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Method to get cross-platform home directory ($HOME)\n",
"home_dir = os.path.expanduser('~')\n",
......@@ -472,14 +546,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['helloworld.py', 'example0', 'wrong.py', 'tmp_directory']\n"
"['helloworld.py', 'tmp_directory', 'wrong.py', 'example1', 'example0']\n"
]
}
],
......@@ -499,9 +573,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In the directory ../pyfiles:\n",
"\tdirectories: ['tmp_directory', 'example1', 'example0']\n",
"\tfiles ['helloworld.py', 'wrong.py'].\n",
"In the directory ../pyfiles/tmp_directory:\n",
"\tdirectories: []\n",
"\tfiles [].\n",
"In the directory ../pyfiles/example1:\n",
"\tdirectories: []\n",
"\tfiles ['prog.py', 'util.py'].\n",
"In the directory ../pyfiles/example0:\n",
"\tdirectories: ['__pycache__']\n",
"\tfiles ['prog.py', 'util.py'].\n",
"In the directory ../pyfiles/example0/__pycache__:\n",
"\tdirectories: []\n",
"\tfiles ['util.cpython-37.pyc'].\n",
"True\n",
"False\n",
"In the directory ../pyfiles:\n",
"\tdirectories: ['example1', 'example0']\n",
"\tfiles ['helloworld.py', 'wrong.py'].\n",
"In the directory ../pyfiles/example1:\n",
"\tdirectories: []\n",
"\tfiles ['prog.py', 'util.py'].\n",
"In the directory ../pyfiles/example0:\n",
"\tdirectories: ['__pycache__']\n",
"\tfiles ['prog.py', 'util.py'].\n",
"In the directory ../pyfiles/example0/__pycache__:\n",
"\tdirectories: []\n",
"\tfiles ['util.cpython-37.pyc'].\n"
]
}
],
"source": [
"def list_dir_files():\n",
" for base, path_dir, path_files in os.walk('../pyfiles'):\n",
......@@ -544,15 +654,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"list unsorted: ['pres03_functions.slides.html', 'practical_numpy_img_median.ipynb', 'slides_reveal_wide.tpl', 'index.rst', 'pres14_advanced.ipynb', 'reveal.js', 'pres05_practical0.slides.html', 'practical_numpy_img_median.slides.html', 'pres00_intro_first_steps.slides.html', 'introduction.slides.html', 'pres04_readwritefiles.ipynb', 'pres01_intro_language.ipynb', 'pres09_practical1.slides.html', 'pres03_functions.ipynb', 'introduction.ipynb', 'pres07_data_struct.ipynb', 'pres06_import_standard_library.ipynb', 'table_of_contents.rst', 'pres12_practical2.slides.html', 'pres14_advanced.slides.html', 'pres04_readwritefiles.slides.html', 'pres080_oop_encapsulation.slides.html', 'index.html', 'pres08_oop.ipynb', 'pres02_basic_statements.slides.html', 'pres07_data_struct.slides.html', 'pres11_intro_numpy_matplotlib.ipynb', 'pres11_intro_numpy_matplotlib.slides.html', 'pres00_intro_first_steps.ipynb', 'pres12_practical2.ipynb', 'pres08_oop.slides.html', 'pres080_oop_encapsulation.ipynb', 'pres01_intro_language.slides.html', 'pres05_practical0.ipynb', 'pres10_environnement.ipynb', 'pres10_environnement.slides.html', 'pres02_basic_statements.ipynb', 'pres13_doc_applications.ipynb', 'pres06_import_standard_library.slides.html', 'pres09_practical1.ipynb', 'pres13_doc_applications.slides.html']\n",
"list sorted: ['index.html', 'index.rst', 'introduction.ipynb', 'introduction.slides.html', 'practical_numpy_img_median.ipynb', 'practical_numpy_img_median.slides.html', 'pres00_intro_first_steps.ipynb', 'pres00_intro_first_steps.slides.html', 'pres01_intro_language.ipynb', 'pres01_intro_language.slides.html', 'pres02_basic_statements.ipynb', 'pres02_basic_statements.slides.html', 'pres03_functions.ipynb', 'pres03_functions.slides.html', 'pres04_readwritefiles.ipynb', 'pres04_readwritefiles.slides.html', 'pres05_practical0.ipynb', 'pres05_practical0.slides.html', 'pres06_import_standard_library.ipynb', 'pres06_import_standard_library.slides.html', 'pres07_data_struct.ipynb', 'pres07_data_struct.slides.html', 'pres080_oop_encapsulation.ipynb', 'pres080_oop_encapsulation.slides.html', 'pres08_oop.ipynb', 'pres08_oop.slides.html', 'pres09_practical1.ipynb', 'pres09_practical1.slides.html', 'pres10_environnement.ipynb', 'pres10_environnement.slides.html', 'pres11_intro_numpy_matplotlib.ipynb', 'pres11_intro_numpy_matplotlib.slides.html', 'pres12_practical2.ipynb', 'pres12_practical2.slides.html', 'pres13_doc_applications.ipynb', 'pres13_doc_applications.slides.html', 'pres14_advanced.ipynb', 'pres14_advanced.slides.html', 'reveal.js', 'slides_reveal_wide.tpl', 'table_of_contents.rst']\n"
"list unsorted: ['pres00_intro_first_steps.slides.html', 'pres09_practical1.slides.html', 'pres080_oop_encapsulation.ipynb', 'practical_numpy_img_median.ipynb', 'pres080_oop_encapsulation.slides.html', 'practical_numpy_img_median.slides.html', 'pres081_oop_inheritance.ipynb', 'pres13_doc_applications.slides.html', 'slides_reveal_wide.tpl', 'reveal.js', 'pres06_import_standard_library.slides.html', 'pres12_practical2.slides.html', 'pres111_intro_matplotlib.ipynb', 'pres12_practical2.ipynb', 'pres04_readwritefiles.ipynb', 'index.rst', 'pres15_practical5.ipynb', 'images', 'pres07_data_struct.ipynb', 'pres02_basic_statements.ipynb', 'pres110_intro_numpy_scipy_pandas.slides.html', 'pres13_doc_applications.ipynb', 'pres03_functions.ipynb', 'pres10_environnement.slides.html', 'pres04_readwritefiles.slides.html', 'pres01_intro_language.slides.html', 'pres06_import_standard_library.ipynb', 'pres111_intro_matplotlib.slides.html', 'table_of_contents.rst', 'pres05_practical0.ipynb', 'pres14_advanced.ipynb', 'pres01_intro_language.ipynb', 'pres081_oop_inheritance.slides.html', 'pres15_practical5.slides.html', 'pres05_practical0.slides.html', 'pres07_data_struct.slides.html', 'pres09_practical1.ipynb', 'pres00_intro_first_steps.ipynb', 'pres14_advanced.slides.html', 'pres03_functions.slides.html', 'pres10_environnement.ipynb', 'index.html', 'introduction.slides.html', 'pres02_basic_statements.slides.html', 'pres110_intro_numpy_scipy_pandas.ipynb', 'introduction.ipynb']\n",
"list sorted: ['images', 'index.html', 'index.rst', 'introduction.ipynb', 'introduction.slides.html', 'practical_numpy_img_median.ipynb', 'practical_numpy_img_median.slides.html', 'pres00_intro_first_steps.ipynb', 'pres00_intro_first_steps.slides.html', 'pres01_intro_language.ipynb', 'pres01_intro_language.slides.html', 'pres02_basic_statements.ipynb', 'pres02_basic_statements.slides.html', 'pres03_functions.ipynb', 'pres03_functions.slides.html', 'pres04_readwritefiles.ipynb', 'pres04_readwritefiles.slides.html', 'pres05_practical0.ipynb', 'pres05_practical0.slides.html', 'pres06_import_standard_library.ipynb', 'pres06_import_standard_library.slides.html', 'pres07_data_struct.ipynb', 'pres07_data_struct.slides.html', 'pres080_oop_encapsulation.ipynb', 'pres080_oop_encapsulation.slides.html', 'pres081_oop_inheritance.ipynb', 'pres081_oop_inheritance.slides.html', 'pres09_practical1.ipynb', 'pres09_practical1.slides.html', 'pres10_environnement.ipynb', 'pres10_environnement.slides.html', 'pres110_intro_numpy_scipy_pandas.ipynb', 'pres110_intro_numpy_scipy_pandas.slides.html', 'pres111_intro_matplotlib.ipynb', 'pres111_intro_matplotlib.slides.html', 'pres12_practical2.ipynb', 'pres12_practical2.slides.html', 'pres13_doc_applications.ipynb', 'pres13_doc_applications.slides.html', 'pres14_advanced.ipynb', 'pres14_advanced.slides.html', 'pres15_practical5.ipynb', 'pres15_practical5.slides.html', 'reveal.js', 'slides_reveal_wide.tpl', 'table_of_contents.rst']\n"
]
}
],
......@@ -578,7 +688,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
......@@ -587,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
......@@ -658,9 +768,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"command failed with return code 1\n"
]
}
],
"source": [
"import subprocess\n",
"def call_bash(commands):\n",
......@@ -728,9 +846,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2019-03-31 22:17:22,616 - root - INFO - Would you like to have a \"beer\"?\n"
]
}
],
"source": [
"import logging\n",
"log_level = logging.INFO # to get information messages\n",
......@@ -760,7 +886,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.7.2"
}
},
"nbformat": 4,
......
......@@ -6,9 +6,3 @@ util.myvar0 = 100
myvar1 += 100
print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')
print_variables()
print('in prog.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
print('end of prog.py')
......@@ -4,14 +4,3 @@ myvar1 = 1
def print_variables():
print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')
print('in util.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
# this code is executed only in the file is directly executed
print('the module util.py has been directly executed')
print_variables()
print('end of util.py')
else:
print('the module util.py has been imported')
# 2 different syntaxes for importing a module
import util
from util import myvar1, print_variables
util.myvar0 = 100
myvar1 += 100
print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')
print_variables()
print('in prog.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
print('end of prog.py')
print('begin of util.py')
myvar0 = 0
myvar1 = 1
def print_variables():
print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')
print('in util.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
# this code is executed only in the file is directly executed
print('the module util.py has been directly executed')
print_variables()
print('end of util.py')
else:
print('the module util.py has been imported')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment