Skip to content

Commit 7acbad5

Browse files
committed
content/scripts: Minor changes, mainy linking and phrasing
1 parent 47a3425 commit 7acbad5

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

content/scripts.rst

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@ Scripts
77

88
- Why are command line programs useful, compared to Jupyter
99
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?
1212

1313
.. objectives::
1414

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
1818

1919
Why scripts?
2020
-------------
2121

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.
2323

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:
2525

2626
- either with 10 different notebooks (so that you can run them concurrently)
2727
- or with a very long notebook which is becoming hardly readable!
2828

2929
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.
3030
Suppose you find a bug in the original notebook and need to rerun everything: are you willing to re-create manually your 1000 notebooks?
3131

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
3333

3434
* you do not need to manually configure your notebooks to be able to run with different parameters
3535
* can easily run you work via other tools, such as on computing clusters.
3636

3737

38-
From jupyter notebooks to python scripts
38+
From Jupyter notebooks to Python scripts
3939
-----------------------------------------
4040

41-
Save as python script
41+
Save as Python script
4242
---------------------
4343

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/>`__.
4545

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:
4747

4848
.. figure:: https://jupyterlab.readthedocs.io/en/stable/_images/exporting_menu.png
4949

@@ -54,8 +54,7 @@ Within JupyterLab, you can export any jupyter notebook to a python script:
5454

5555
Actually, you could also export your notebook in many other formats.
5656
Check the `JupyterLab documentation <https://jupyterlab.readthedocs.io/en/stable/user/export.html>`_ for more information.
57-
If you keep working in the jupyterlab folder, you can also convert files in the terminal (File -> New -> Terminal) by running::
58-
57+
If you are working by the command line (File → New → Terminal), you can also convert files in the terminal by running::
5958

6059
$ jupyter nbconvert --to script your_notebook_name.ipynb
6160

@@ -131,48 +130,49 @@ We can try it out:
131130
- Why is this better than modifying the script every time I want it to
132131
plot data for a different period?
133132

134-
- What problems do you expect when using this approach (using ``sys.argv``)?
133+
- What problems do you expect when using this approach (using :data:`sys.argv`)?
135134

136135
This approach is brittle and more robust solutions exist that allow you to fully
137136
customize your scripts and generate help texts at the same time:
138137

139138
- `argparse <https://docs.python.org/3/library/argparse.html>`__:
140139
built-in to Python, this is the one that we will show below.
141140
- `doctopt <http://docopt.org/>`__: you write the help text and this generates a parser for you.
142-
- `click <https://click.palletsprojects.com//>`__: another nice
141+
- `click <https://click.palletsprojects.com/>`__: another nice
143142
library for command line interfaces - very easy to use.
144143

145144

146-
Parsing command line arguments with ``argparse``
147-
------------------------------------------------
145+
Parsing command line arguments with :mod:`argparse`
146+
---------------------------------------------------
148147

149148
:py:mod:`Argparse <argparse>` not only gives you descriptive command line arguments, it also automatically
150149
generates a ``--help`` option for you. To use ``argparse`` you first set up a parser
151-
by calling ``parser = argparse.ArgumentParser()`` and then you add arguments using
150+
by calling :class:`parser = argparse.ArgumentParser() <argparse.ArgumentParser>` and then you add arguments using
152151
:py:meth:`parser.add_argument(args) <argparse.ArgumentParser.add_argument>`. There are two different types of arguments:
153152

154153
- Positional arguments
155154
- Optional arguments
156155

157-
Positional arguments are fixed in their position, while optional arguments need to be
156+
**Positional arguments** are detected by their order, while **optional arguments** need to be
158157
given with their respective flags ( like ``--name`` or ``-n``).
159158
The following example would parse a positional argument ``Name`` of type ``string``
160159
and an optional argument ``date`` of type ``string`` which defaults to ``01/01/2000``.
161160

162161
.. code-block:: python
162+
:emphasize-lines: 3, 5-8, 10
163163
164164
import argparse
165165
166166
parser = argparse.ArgumentParser()
167-
# set start and end time
168-
parser.add_argument('Name', type=str, metavar="N", \
169-
help="The name of the subject")
170-
parser.add_argument('-d', '--date', type=string, default="01/01/2000", \
171-
help="Birth date of the subject")
167+
# One positional and one optional argument
168+
parser.add_argument('name', type=str, metavar="N",
169+
help="The name of the subject")
170+
parser.add_argument('-d', '--date', type=string, default="01/01/2000",
171+
help="Birth date of the subject")
172172
173173
args = parser.parse_args()
174174
175-
print(args.Name + " was born on " + args.date)
175+
print(args.name + " was born on " + args.date)
176176
177177
If this code was in ``birthday.py`` and we would call ``python birthday.py --help`` it
178178
would show the following message:
@@ -229,7 +229,7 @@ Exercises 2
229229
- We can select multiple time ranges without modifying the script.
230230
- This way we can also loop over file patterns (using shell loops or similar) or use
231231
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
233233
user input errors and also got a help text (accessible via ``--help``).
234234

235235

@@ -256,11 +256,11 @@ more complex input data, like lists, or dictionaries. We won't go into the detai
256256
a short example using YAML here.
257257

258258
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
260260
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).
262262

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.
264264
Similar to argparse, it takes a dict of required options, along with a dict of optional parameters.
265265
Required arguments need to specify a type. Optional argument types are derived from their default values.
266266

@@ -278,7 +278,7 @@ Exercises 3 (optional)
278278
.. challenge:: Scripts-3
279279

280280
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.
282282
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
283283
(using e.g. argparse or sys.argv) still needs to be read from the command line.
284284

0 commit comments

Comments
 (0)