You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/scripts.rst
+31-31Lines changed: 31 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,43 +7,43 @@ Scripts
7
7
8
8
- Why are command line programs useful, compared to Jupyter
9
9
notebooks and similar?
10
-
- How to create a python script?
11
-
- How to generalize a python script?
10
+
- How to create a Python script?
11
+
- How to generalize a Python script?
12
12
13
13
.. objectives::
14
14
15
-
- Learn how to streamline your python notebooks by creating repeatable python scripts
16
-
- Learn how to import other python files
17
-
- Learn to parse command line arguments in python
15
+
- Learn how to streamline your Python notebooks by creating repeatable Python scripts
16
+
- Learn how to import other Python files
17
+
- Learn to parse command line arguments in Python
18
18
19
19
Why scripts?
20
20
-------------
21
21
22
-
So far we have been learning python using Jupyter notebooks. It is very convenient: it allowed us to experiment and prototype python code so we may think that is more than enough for your day to day work.
22
+
So far we have been learning Python using Jupyter notebooks. It is very convenient: it allowed us to experiment and prototype Python code so we may think that is more than enough for your day to day work.
23
23
24
-
But after several weeks of hard work with python, you may end up:
24
+
But after several weeks of hard work with Python, you may end up:
25
25
26
26
- either with 10 different notebooks (so that you can run them concurrently)
27
27
- or with a very long notebook which is becoming hardly readable!
28
28
29
29
Let's imagine you have created 10 notebooks to run for 10 different input parameters and now you are willing to experiment with 1000 sets of input parameters.
30
30
Suppose you find a bug in the original notebook and need to rerun everything: are you willing to re-create manually your 1000 notebooks?
31
31
32
-
In this episode, we will learn how to automate your work using python scripts so that
32
+
In this episode, we will learn how to automate your work using Python scripts so that
33
33
34
34
* you do not need to manually configure your notebooks to be able to run with different parameters
35
35
* can easily run you work via other tools, such as on computing clusters.
36
36
37
37
38
-
From jupyter notebooks to python scripts
38
+
From Jupyter notebooks to Python scripts
39
39
-----------------------------------------
40
40
41
-
Save as python script
41
+
Save as Python script
42
42
---------------------
43
43
44
-
Jupyter notebooks can be parameterized for instance using `papermill <https://papermill.readthedocs.io/en/latest/>`_. It can be an attractive approach when you have short notebooks (to generate automatically plots/reports) but as soon as you have more complex tasks to execute, we strongly recommend to generate python scripts. This will also force you to modularize your code. See `CodeRefinery's lesson on Modular code development <https://coderefinery.github.io/modular-type-along/>`__.
44
+
Jupyter notebooks can be parameterized for instance using `papermill <https://papermill.readthedocs.io/en/latest/>`_. It can be an attractive approach when you have short notebooks (to generate automatically plots/reports) but as soon as you have more complex tasks to execute, we strongly recommend to generate Python scripts. This will also force you to modularize your code. See `CodeRefinery's lesson on Modular code development <https://coderefinery.github.io/modular-type-along/>`__.
45
45
46
-
Within JupyterLab, you can export any jupyter notebook to a python script:
46
+
Within JupyterLab, you can export any Jupyter notebook to a Python script:
If this code was in ``birthday.py`` and we would call ``python birthday.py --help`` it
178
178
would show the following message:
@@ -229,7 +229,7 @@ Exercises 2
229
229
- We can select multiple time ranges without modifying the script.
230
230
- This way we can also loop over file patterns (using shell loops or similar) or use
231
231
the script in a workflow management system and process many files in parallel.
232
-
- By changing from ``sys.argv`` to ``argparse`` we made the script more robust against
232
+
- By changing from :data:`sys.argv` to :mod:`argparse` we made the script more robust against
233
233
user input errors and also got a help text (accessible via ``--help``).
234
234
235
235
@@ -256,11 +256,11 @@ more complex input data, like lists, or dictionaries. We won't go into the detai
256
256
a short example using YAML here.
257
257
258
258
The YAML file format can be simple or very complex allowing a large variety of data structures to be stored.
259
-
One benefit of YAML is that there is already a python module (``yaml``) available for parsing it and it
259
+
One benefit of YAML is that there is already a Python module (`yaml<https://pyyaml.org/>`__) available for parsing it and it
260
260
directly parses numbers as numbers and text as strings, making conversions unnecessary (the same is true for JSON
261
-
with the ``json`` package).
261
+
with the :mod:`json` package).
262
262
263
-
The python module :download:`optionsparser.py <../resources/code/scripts/optionsparser.py>` provides a simple parser for YAML styled options files.
263
+
The Python module :download:`optionsparser.py <../resources/code/scripts/optionsparser.py>` provides a simple parser for YAML styled options files.
264
264
Similar to argparse, it takes a dict of required options, along with a dict of optional parameters.
265
265
Required arguments need to specify a type. Optional argument types are derived from their default values.
266
266
@@ -278,7 +278,7 @@ Exercises 3 (optional)
278
278
.. challenge:: Scripts-3
279
279
280
280
1. Download the :download:`optionsparser.py <https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/code/scripts/optionsparser.py>`
281
-
function and load it into your working folder in jupyterlab.
281
+
function and load it into your working folder in Jupyterlab.
282
282
Modify the previous script to use a config file parser to read all arguments. The config file is passed in as a single argument on the command line
283
283
(using e.g. argparse or sys.argv) still needs to be read from the command line.
0 commit comments