Skip to content

Commit b2164fc

Browse files
committed
content/scripts: Some suggestions for clarity
- Mostly minor, automerge after some time
1 parent 7acbad5 commit b2164fc

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

content/scripts.rst

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,39 @@ Exercises 1
6767
.. highlight:: console
6868

6969

70-
1. Download the :download:`weather_observations.ipynb <../resources/code/scripts/weather_observations.ipynb>` and the weather_data file and upload them to your jupyterlab. The script plots the temperature data for Tapiola in Espoo. The data is originally from `rp5.kz <https://rp5.kz>`_ and was slightly adjusted for this lecture.
70+
1. Download the :download:`weather_observations.ipynb <../resources/code/scripts/weather_observations.ipynb>` and the weather_data file and upload them to your Jupyterlab. The script plots the temperature data for Tapiola in Espoo. The data is originally from `rp5.kz <https://rp5.kz>`_ and was slightly adjusted for this lecture.
7171

72-
**Note:** If you haven't downloaded the file directly to your jupyterlab folder, it will be located in your **Downloads** folder or the folder you selected. In jupyterlab click on the 'upload file' button, navigate to the folder containing the file and select it to load it into your jupyterlab folder.
72+
**Note:** If you haven't downloaded the file directly to your Jupyterlab folder, it will be located in your **Downloads** folder or the folder you selected. In Jupyterlab click on the 'upload file' button, navigate to the folder containing the file and select it to load it into your Jupyterlab folder.
7373

74-
2. Open a terminal in jupyter (File -> New -> Terminal).
74+
2. Open a terminal in Jupyter (File New Terminal).
7575

76-
3. Convert the jupyter script to a python script by calling::
76+
3. Convert the Jupyter script to a Python script by calling::
7777

7878
$ jupyter nbconvert --to script weather_observations.ipynb
7979

8080
4. Run the script (note: you may have ``python3`` rather than ``python``)::
8181

8282
$ python weather_observations.py
8383

84-
Command line arguments with ``sys.argv``
85-
----------------------------------------
84+
Command line arguments with :data:`sys.argv`
85+
--------------------------------------------
8686

87-
We now have a python script that is callable from the command line (e.g. for use on an HPC system).
87+
We now have a Python script that is callable from the command line (e.g. for use on an HPC system).
8888
However, this code is still not adjustable, as we still need to have a copy for each single
8989
time range we want to plot, or need to modify our file whenever we want to just change parameters.
90-
What we need is to allow arguments to be put in from the command line in order to have the same code
91-
plot information for different time ranges without odifying the code itself. This can be achieved by
92-
using pythons :py:mod:`sys` package, which provides access to arguments given to the python interpreter at
93-
startup in the :py:data:`sys.argv` list. The first (i.e. ``sys.argv[0]`` entry of this array is the called script,
94-
and any further argument (separated by space) is appended to this list. Lets see how it works:
90+
**What we need is to allow the code to do something different based on something outside the code itself**: in this case, to
91+
plot information for different time ranges. This can be achieved by
92+
using Pythons :py:mod:`sys` package, which provides access to arguments given to the Python interpreter at
93+
startup in the :py:data:`sys.argv` list. The first (i.e. ``sys.argv[0]`` entry of this array is the script that is running,
94+
and any further argument (separated by space) is appended to this list, like such:
9595

96-
We modify the **weather_observations.py** script such that we allow start
96+
.. code-block:: console
97+
98+
$ python my_script.py A B
99+
$ # sys.argv[1] is 'A'
100+
$ # sys.argv[2] is 'B'
101+
102+
Lets see how it works: We modify the **weather_observations.py** script such that we allow start
97103
and end times as well as the output file to be passed in as arguments to the function:
98104

99105
.. code-block:: python
@@ -194,12 +200,15 @@ Exercises 2
194200

195201
.. challenge:: Scripts-2
196202

197-
1. Take the python script we have written in the preceding exercise and use
203+
1. Take the Python script (``weather_observations.py``) we have written in the preceding exercise and use
198204
:py:mod:`argparse` to specify the input and output files and allow the start and end dates to be set.
199-
The start and end dates should be optional parameters with the defaults as they are in the current script.
200205

201-
2. Execute your script for a few different time intervals (e.g. from January 2019 to June 2020, or from Mai 2020 to October 2020).
202-
Also use data for cairo (``https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/data/scripts/weather_cairo.csv``)
206+
* Hint: try not to do it all at once, but add one or two arguments, test, then add more, and so on.
207+
* Hint: The input and output filenames make sense as positional arguments, since they must always be given. Input is usually first, then output.
208+
* Hint: The start and end dates should be optional parameters with the defaults as they are in the current script.
209+
210+
2. Execute your script for a few different time intervals (e.g. from January 2019 to June 2020, or from May 2020 to October 2020).
211+
Also try using this data for Cairo: ``https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/data/scripts/weather_cairo.csv``
203212

204213

205214
.. solution::

0 commit comments

Comments
 (0)