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
Select File (top menu bar) → Export Notebook as → **Export notebook to Executable Script**.
51
51
@@ -69,9 +69,13 @@ Exercises 1
69
69
70
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.
71
71
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
+
**Hint:** Copy the URL above (right-click) and in JupyterLab, use
73
+
File → Open from URL → Paste the URL. It will both download it to
74
+
the directory JupyterLab is in and open it for you.
73
75
74
-
2. Open a terminal in Jupyter (File → New → Terminal).
76
+
2. Open a terminal in Jupyter: File → New Launcher, then click
77
+
"Terminal" there. (if you do it this way, it will be in the right
78
+
directory. File → New → Terminal might not be.)
75
79
76
80
3. Convert the Jupyter script to a Python script by calling::
77
81
@@ -81,6 +85,8 @@ Exercises 1
81
85
82
86
$ python weather_observations.py
83
87
88
+
89
+
84
90
Command line arguments with :data:`sys.argv`
85
91
--------------------------------------------
86
92
@@ -100,29 +106,31 @@ and any further argument (separated by space) is appended to this list, like suc
100
106
$ # sys.argv[2] is 'B'
101
107
102
108
Lets see how it works: We modify the **weather_observations.py** script such that we allow start
103
-
and end times as well as the output file to be passed in as arguments to the function:
109
+
and end times as well as the output file to be passed in as arguments
110
+
to the function. Open it (find the ``.py`` file from the JupyterLab
We can try it out (see the file ``spring_in_tapiola.png`` made in the
133
+
file browser):
126
134
127
135
.. code-block:: console
128
136
@@ -185,6 +193,7 @@ would show the following message:
185
193
186
194
.. code-block:: console
187
195
196
+
$ python birthday.py --help
188
197
usage: birthday.py [-h] [-d DATE] N
189
198
190
199
positional arguments:
@@ -201,7 +210,7 @@ Exercises 2
201
210
.. challenge:: Scripts-2
202
211
203
212
1. Take the Python script (``weather_observations.py``) we have written in the preceding exercise and use
204
-
:py:mod:`argparse` to specify the input and output files and allow the start and end dates to be set.
213
+
:py:mod:`argparse` to specify the input (URL) and output files and allow the start and end dates to be set.
205
214
206
215
* Hint: try not to do it all at once, but add one or two arguments, test, then add more, and so on.
207
216
* Hint: The input and output filenames make sense as positional arguments, since they must always be given. Input is usually first, then output.
@@ -236,6 +245,7 @@ Exercises 2
236
245
237
246
- We can now process different input files without changing the script.
238
247
- We can select multiple time ranges without modifying the script.
248
+
- We can easily save these commands to know what we did.
239
249
- This way we can also loop over file patterns (using shell loops or similar) or use
240
250
the script in a workflow management system and process many files in parallel.
241
251
- By changing from :data:`sys.argv` to :mod:`argparse` we made the script more robust against
@@ -287,9 +297,9 @@ Exercises 3 (optional)
287
297
.. challenge:: Scripts-3
288
298
289
299
1. Download the :download:`optionsparser.py <https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/code/scripts/optionsparser.py>`
290
-
function and load it into your working folder in Jupyterlab.
300
+
function and load it into your working folder in Jupyterlab (Hint: in JupyterLab, File → Open from URL).
291
301
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
292
-
(using e.g. argparse or sys.argv) still needs to be read from the command line.
302
+
(using e.g. :mod:`argparse` or :data:`sys.argv`) still needs to be read from the command line.
293
303
294
304
295
305
2. Run your script with different config files.
@@ -303,6 +313,12 @@ Exercises 3 (optional)
303
313
:language: python
304
314
:emphasize-lines: 5,9-12,15-27,30,33,36-37,58
305
315
316
+
What did this config file parser get us? Now, we have separated the
317
+
code from the configuration. We could save all the configuration in
318
+
version control - separately and have one script that runs them. If
319
+
done right, our work could be much more reproducible and
0 commit comments