Commit 00b8c248 authored by Franck Thollard's avatar Franck Thollard
Browse files

correction of the index of the value of the error

parents c715603b 7c9c74eb
syntax: glob
*.pyd*
*.pyc
*~
......@@ -8,9 +6,14 @@ syntax: glob
\#*\#
ipynb/*.slides.html
.ipynb_checkpoints
*/.ipynb_checkpoints/*
**/Untitled.ipynb
**/*.egg-info/
ipynb/index.html
\ No newline at end of file
ipynb/index.html
.vscode
TP/TP2_package/mypackuga/build
TP/TP2_package/mypackuga/dist
\ No newline at end of file
......@@ -37,7 +37,7 @@ ipynb/index.html: ipynb/index.rst
cd $(IPYNBDIR) && rst2html5 index.rst > index.html
%.slides.html: %.ipynb ipynb/slides_reveal_wide.tpl
jupyter-nbconvert $< --to slides --template ipynb/slides_reveal_wide.tpl
jupyter-nbconvert $< --reveal-prefix='./reveal.js' --to slides --template ipynb/slides_reveal_wide.tpl
presentations: $(IPYNBPRES) ipynb/index.html
......
......@@ -9,12 +9,10 @@ To display and/or modify the presentations, use the Makefile. See::
make help
Repositories
------------
Repository
----------
The old repository is https://sourcesup.renater.fr/projects/py-training-uga/
We now use a repository hosted in the Gitlab of UGA:
The repository hosted in the Gitlab of UGA:
https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga
- Clone the repository with Git with https::
......@@ -40,5 +38,4 @@ Authors
-------
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck
Thollard (ISTERRE), Christophe Picard (LJK)
Thollard (ISTERRE), Christophe Picard (LJK), Loïc Huder (ISTerre)
......@@ -6,7 +6,7 @@ form key=val where key is p1, p2 or p3, and val is a float.
import re
import logging
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
def compute_stats(file_name):
"""
......@@ -60,7 +60,7 @@ def check_format_re(file_name, default_values):
for i,v in enumerate(vals):
if v is None:
logging.warning(f'file {file_name} line {line_num}: missing: p{i+1}, setting default value')
vals[i] = default_values[i-1]
vals[i] = default_values[i]
def check_format(file_name, cols_names_set):
......
......@@ -12,7 +12,7 @@
"\n",
"**A training to acquire strong basis in Python to use it efficiently**\n",
"\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK) Loïc Huder (ISTerre)\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
"\n",
"# First steps\n",
"\n",
......@@ -274,9 +274,7 @@
"\n",
"Remarks: \n",
"\n",
"- it is very important to use a good editor to code in Python (for example spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d)).\n",
"\n",
"- a good modern open-source editor: [Visual Studio Code] (https://code.visualstudio.com/docs/languages/python)."
"- it is very important to use a good editor to code in Python (for example Spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d), or [Visual Studio Code](https://code.visualstudio.com/docs/languages/python))."
]
},
{
......
......@@ -12,8 +12,7 @@
"\n",
"**A training to acquire strong basis in Python to use it efficiently**\n",
"\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK),\n",
" Loïc Huder (ISTerre)\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
" \n",
"\n",
"# Characteristics of the Python language"
......
......@@ -902,7 +902,7 @@
" | a | b | c | d | e | f | \n",
" +---+---+---+---+---+---+ \n",
"Slice from front: 0 1 2 3 4 5 6 \n",
"Slice from rear: -6 -5 -4 -3 -2 -1 0\n",
"Slice from rear: -6 -5 -4 -3 -2 -1 None\n",
"```"
]
},
......@@ -2486,7 +2486,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.6.6"
}
},
"nbformat": 4,
......
......@@ -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,
......
{%- extends 'basic.tpl' -%}
{% from 'mathjax.tpl' import mathjax %}
// added by Pierre Augier
{%- block input_group scoped -%}
{%- if not cell.metadata.get('source_hidden', False) -%}
{{ super() }}
{%- endif -%}
{%- endblock input_group -%}
{%- block output_prompt -%}
{%- if not cell.metadata.get('source_hidden', False) -%}
{{ super() }}
{%- endif -%}
{%- endblock output_prompt -%}
{%- block any_cell scoped -%}
{%- if cell.metadata.get('slide_start', False) -%}
<section>
......@@ -189,7 +202,12 @@ require(
controls: true,
progress: true,
history: true,
width: 1200,
// changed by Pierre Augier
width: '85%',
height: '100%',
slideNumber: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
......
......@@ -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