Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f99183

Browse files
fabioaclpre-commit-ci[bot]despadam
authored
Fix errors found in Input-Output (empa-scientific-it#338)
* Resolving part of the issueempa-scientific-it#272* Corrected issues and tested functions in Issueempa-scientific-it#272* Corrected directories hyperlinks* Resolving part of the issueempa-scientific-it#272* Corrected issues and tested functions in Issueempa-scientific-it#272* Corrected directories hyperlinks* [pre-commit.ci] auto fixes from pre-commit.com hooksfor more information, seehttps://pre-commit.ci* Running pre-commit* [pre-commit.ci] auto fixes from pre-commit.com hooksfor more information, seehttps://pre-commit.ci* Correct filepath* final fixes---------Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Co-authored-by: Despina Adamopoulou <despoina.adamopoulou@empa.ch>
1 parenta77979c commit2f99183

File tree

8 files changed

+98
-58
lines changed

8 files changed

+98
-58
lines changed

‎04_input_output.ipynb‎

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
" - [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n",
4141
" - [Quiz on CSV](#Quiz-on-CSV)\n",
4242
" - [Exercises](#Exercises)\n",
43-
" - [Exercise 1: CSV to dictionary 🌶️](#Exercise-1:-CSV-to-dictionary-🌶️)\n",
44-
" - [Exercise 2: Counting words 🌶️](#Exercise-2:-Counting-words-🌶️)\n",
45-
" - [Exercise 3: Letter statistics 🌶️🌶️](#Exercise-3:-Letter-statistics-🌶️🌶️)\n",
46-
" - [Exercise 4: Translating words 🌶️🌶️](#Exercise-4:-Translating-words-🌶️🌶️)\n",
47-
" - [Exercise 5: Binary format 🌶️🌶️🌶️](#Exercise-5:-Binary-format-🌶️🌶️🌶️)"
43+
" - [Exercise 1: CSV to dictionary](#Exercise-1:-CSV-to-dictionary)\n",
44+
" - [Exercise 2: Counting words](#Exercise-2:-Counting-words)\n",
45+
" - [Exercise 3: Letter statistics](#Exercise-3:-Letter-statistics)\n",
46+
" - [Exercise 4: Translating words](#Exercise-4:-Translating-words)\n",
47+
" - [Exercise 5: Binary format](#Exercise-5:-Binary-format)"
4848
]
4949
},
5050
{
@@ -75,13 +75,7 @@
7575
"- connecting to databases or other network services\n",
7676
"\n",
7777
"\n",
78-
"The majority of these operations are covered by the Python standard library. We are going to see how to use them in this chapter.\n",
79-
"\n",
80-
"<div class=\"alert alert-block alert-info\">\n",
81-
" <h4><b>Note</b></h4>\n",
82-
"In reference to the chapter on functional programming, it is interesting to note that these functions perform <b>side-effects</b>. Therefore, any code containing these operations is no longer <b>pure</b> and is not referentially transparent. The same function can return different values for the same argument if called multiple times, and the function can have <b>long-distance</b> effects. That means they can modify the program state elsewhere, leading to unexpected results.<br><br>\n",
83-
"Therefore, we suggest separating input and output from the other computations in your program. For example, if you have a complex calculation requiring several user inputs at several stages of the process, consider writing a function that only performs the calculation given all inputs and then requires all inputs separately, for example, through a single file. This makes your code easier to debug, test and understand.\n",
84-
"</div>"
78+
"The majority of these operations are covered by the Python standard library. We are going to see how to use them in this chapter."
8579
]
8680
},
8781
{
@@ -108,8 +102,7 @@
108102
"cell_type":"markdown",
109103
"metadata": {},
110104
"source": [
111-
"It is also possible to print any other python object using `print`.\n",
112-
"In that case, the `__str__` **magic method** on that object's class is [called](https://docs.python.org/3/reference/datamodel.html#object.__str__)."
105+
"It is also possible to print any other python object using `print`."
113106
]
114107
},
115108
{
@@ -245,7 +238,7 @@
245238
"metadata": {},
246239
"outputs": [],
247240
"source": [
248-
"%%ipytest debug\n",
241+
"%%ipytest\n",
249242
"\n",
250243
"def solution_print_odd(n: int) -> None:\n",
251244
"\"\"\"Prints all odd numbers from 1 to n\n",
@@ -255,8 +248,7 @@
255248
"\n",
256249
" Returns:\n",
257250
" - None (prints to console)\n",
258-
"\"\"\"\n",
259-
" pass"
251+
"\"\"\""
260252
]
261253
},
262254
{
@@ -287,7 +279,6 @@
287279
" Returns:\n",
288280
" - None (prints to console)\n",
289281
"\"\"\"\n",
290-
" pass\n",
291282
"\n",
292283
"solution_print_salutation()"
293284
]
@@ -526,7 +517,7 @@
526517
"metadata": {},
527518
"source": [
528519
"\n",
529-
"1. Modify the function `solution_find_all_files` to find all files and directories in the[data](./tutorial/tests/data/) (./tutorial/tests/data/) directory and return them as a list of `Path` objects\n",
520+
"1. Modify the function `solution_find_all_files` to find all files and directories in the```/tutorial/tests/data``` directory and return them as a list of `Path` objects\n",
530521
"\n",
531522
"<div class=\"alert alert-block alert-info\">\n",
532523
" <b>Hint:</b> The path to the data directory is available as the argument <code>current_path</code> of the function <code>solution_find_all_files</code>\n",
@@ -613,8 +604,7 @@
613604
"\n",
614605
" Returns:\n",
615606
" - The number of directories in the directory\n",
616-
"\"\"\"\n",
617-
" pass\n"
607+
"\"\"\""
618608
]
619609
},
620610
{
@@ -627,9 +617,9 @@
627617
"### Reading from a file\n",
628618
"\n",
629619
"We now want to learn how to read text from a file.\n",
630-
"Let's see how to do this with an example: we want to open the file [hello.txt](./data/hello.txt) and read its contents.\n",
620+
"Let's see how to do this with an example: we want to open the file [hello.txt](./tutorial/tests/data/hello.txt) and read its contents.\n",
631621
"\n",
632-
"1. The path is already identified, we know the file is in`./data/hello.txt`. We save this in a variable `path`.\n",
622+
"1. The path is already identified, we know the file is in[hello.txt](./tutorial/tests/data/hello.txt). We save this in a variable `path`.\n",
633623
"2. We now can use the built-in function [`open`](https://docs.python.org/3/library/functions.html#open) to open the file. This function returns a [file object](https://docs.python.org/3/glossary.html#term-file-object) that we can use to further manipulate the file. To ensure we only open the file for reading, we pass the string\"r\" to the second argument of `open`.\n",
634624
"3. Now we can read the contents using `read`, `readline` or `readlines`. `read` reads the whole file content into a single string, `readline` reads one line, while `readlines` reads the whole file content as a list of strings, one item per line in the file. This knowledge is useful when we only want to read part of a file or when the file is too big to fit in memory and we can only read parts.\n",
635625
"4. Finally, we close the file using the `close` method on the file object.\n"
@@ -773,7 +763,7 @@
773763
" Returns:\n",
774764
" - A list of strings, each representing a line in the file\n",
775765
"\"\"\"\n",
776-
"pass"
766+
"return"
777767
]
778768
},
779769
{
@@ -787,7 +777,7 @@
787777
"- We use `write` to write a *string* to the file. Other types of object should be converted to string before being written.\n",
788778
"\n",
789779
"\n",
790-
"Let's see this in action by writing your name in a file called `me.txt` in[data](./data/)"
780+
"Let's see this in action by writing your name in a file called `me.txt` in```/tutorial/tests/data```"
791781
]
792782
},
793783
{
@@ -902,26 +892,31 @@
902892
"\n",
903893
" Returns:\n",
904894
" - None (writes to file)\n",
905-
"\"\"\"\n",
906-
" pass"
895+
"\"\"\""
907896
]
908897
},
909898
{
910899
"attachments": {},
911900
"cell_type":"markdown",
912901
"metadata": {},
913902
"source": [
914-
"2. Modify the function `solution_read_write_file` to read the lines from the file `input_file` and write them in the form `line, length`, to the file `output_file`. Here `line` is the line of text in `input_file` **without the line ending**, `length` is **number of characters** in that line **without the line separator**.\n",
915-
"If `input_file` contains these lines:\n",
903+
"2. Modify the function `solution_read_write_file` to read the lines from the file `input_file` and write them in the form `line, length`, to the file `output_file`. Here `line` is the line of text in `input_file` **without the line ending (\\r\\n)**, `length` is **number of characters** in that line. The characters are added automatically by the operating system when reading a file. Windows uses **\\r\\n** whereas Linux and MacOS use only **\\n**. To remove the line ending characters, use ```strip(\"\\r\\n\")```, e.g.: ```line.strip(\"\\r\\n\")```.\n",
904+
"\n",
905+
" If `input_file` contains these lines:\n",
906+
"\n",
916907
" ```\n",
917908
" first\n",
918909
" second\n",
919910
" ```\n",
911+
"\n",
920912
" we expect the output file to contain these lines:\n",
913+
"\n",
921914
" ```\n",
922915
" first, 5\n",
923916
" second, 6\n",
924-
" ```"
917+
" ```\n",
918+
"\n",
919+
" Do not forget to add a **\\n** when writing lines in the file."
925920
]
926921
},
927922
{
@@ -945,8 +940,7 @@
945940
"\n",
946941
" Returns:\n",
947942
" - None (writes to file)\n",
948-
"\"\"\"\n",
949-
" pass"
943+
"\"\"\""
950944
]
951945
},
952946
{
@@ -1201,7 +1195,9 @@
12011195
"source": [
12021196
"message =\"Ciao\"\n",
12031197
"message_secret = bytes(message,\"utf-8\")\n",
1204-
"[print(f\"The `uft8` codepoint is = {enc}, the bytes representation = {enc.to_bytes(4, 'little')}, the representation is {chr(enc)}\") for plain, enc in zip(message, message_secret)]"
1198+
"print_messages = [f\"The `uft8` codepoint is = {enc}, the bytes representation = {enc.to_bytes(4, 'little')}, the representation is {chr(enc)}\" for plain, enc in zip(message, message_secret)]\n",
1199+
"for msg in print_messages:\n",
1200+
" print(msg)"
12051201
]
12061202
},
12071203
{
@@ -1225,7 +1221,7 @@
12251221
"These packages are outside of the scope of this tutorial and will not be covered here.\n",
12261222
"\n",
12271223
"\n",
1228-
"Let's see how to read csv files using `csv` with an example by reading [example.csv](./data/example.csv):"
1224+
"Let's see how to read csv files using `csv` with an example by reading [example.csv](./tutorial/tests/data/example.csv):"
12291225
]
12301226
},
12311227
{
@@ -1330,7 +1326,7 @@
13301326
"tags": []
13311327
},
13321328
"source": [
1333-
"### Exercise 1: CSV to dictionary 🌶️"
1329+
"### Exercise 1: CSV to dictionary"
13341330
]
13351331
},
13361332
{
@@ -1340,6 +1336,8 @@
13401336
"tags": []
13411337
},
13421338
"source": [
1339+
"**Difficulty: 🌶️**\n",
1340+
"\n",
13431341
"Write a function that reads a CSV file and returns a dictionary.\n",
13441342
"\n",
13451343
"- The dictionary keys are in the first **column**\n",
@@ -1404,7 +1402,7 @@
14041402
" Returns:\n",
14051403
" - A dictionary with each row represented as a key, value pair\n",
14061404
"\"\"\"\n",
1407-
"pass"
1405+
"return"
14081406
]
14091407
},
14101408
{
@@ -1414,7 +1412,7 @@
14141412
"tags": []
14151413
},
14161414
"source": [
1417-
"### Exercise 2: Counting words 🌶️"
1415+
"### Exercise 2: Counting words"
14181416
]
14191417
},
14201418
{
@@ -1424,6 +1422,8 @@
14241422
"tags": []
14251423
},
14261424
"source": [
1425+
"**Difficulty: 🌶️**\n",
1426+
"\n",
14271427
"Write a function to read all the lines from `input_file` and count the number of words in the file. The solution should be a single number.\n",
14281428
"\n",
14291429
"For example, for the file\n",
@@ -1443,7 +1443,7 @@
14431443
" The file is available as the parameter <code>input_file</code> of <code>solution_exercise2</code> function\n",
14441444
" </li>\n",
14451445
" <li>\n",
1446-
" A word consists of <b>printable</b> characters without whitespaces, line breaks etc. Have a look at the basic_datatypes notebook if you forgot how to split a text intoit's words.\n",
1446+
" A word consists of <b>printable</b> characters without whitespaces, line breaks etc. Have a look at the basic_datatypes notebook if you forgot how to split a text intoits words.\n",
14471447
" </li>\n",
14481448
" </ul>\n",
14491449
"</div>\n",
@@ -1472,7 +1472,7 @@
14721472
" Returns:\n",
14731473
" - The number of words in the file\n",
14741474
"\"\"\"\n",
1475-
"pass"
1475+
"return"
14761476
]
14771477
},
14781478
{
@@ -1482,7 +1482,7 @@
14821482
"tags": []
14831483
},
14841484
"source": [
1485-
"### Exercise 3: Letter statistics 🌶️🌶️"
1485+
"### Exercise 3: Letter statistics"
14861486
]
14871487
},
14881488
{
@@ -1492,7 +1492,9 @@
14921492
"tags": []
14931493
},
14941494
"source": [
1495-
"Write a function that reads all the lines from `lines.txt` and counts the occurences of every letter present in all the words.\n",
1495+
"**Difficulty: 🌶️🌶️**\n",
1496+
"\n",
1497+
"Write a function that reads all the lines from [lines.txt](./tutorial/tests/data/lines.txt) and counts the occurences of every letter present in all the words.\n",
14961498
"\n",
14971499
"The result should be a dictionary, where each key-value pair is like `{letter: count}`. For example, `{a: 5}` means that the letter `a` appeared five times in this file.\n",
14981500
"\n",
@@ -1522,7 +1524,7 @@
15221524
"metadata": {},
15231525
"outputs": [],
15241526
"source": [
1525-
"%%ipytest input_output\n",
1527+
"%%ipytest\n",
15261528
"\n",
15271529
"import pathlib as pl\n",
15281530
"import string\n",
@@ -1538,7 +1540,7 @@
15381540
" Returns:\n",
15391541
" - A dictionary with a count of each letter\n",
15401542
"\"\"\"\n",
1541-
"pass"
1543+
"return"
15421544
]
15431545
},
15441546
{
@@ -1548,7 +1550,7 @@
15481550
"tags": []
15491551
},
15501552
"source": [
1551-
"### Exercise 4: Translating words 🌶️🌶️"
1553+
"### Exercise 4: Translating words"
15521554
]
15531555
},
15541556
{
@@ -1558,7 +1560,9 @@
15581560
"tags": []
15591561
},
15601562
"source": [
1561-
"Write a function which takes the words from the file `english.txt` and translates them to Italian using the dictionary file `dict.csv`. The output should be a **list of tuples** with the pair `italian, english` if the word is found and nothing otherwise.\n",
1563+
"**Difficulty: 🌶️🌶️**\n",
1564+
"\n",
1565+
"Write a function which takes the words from the file [english.txt](./tutorial/tests/data/english.txt) and translates them to Italian using the dictionary file [dict.csv](./tutorial/tests/data/dict.csv). The output should be a **list of tuples** with the pair `italian, english` **if the word is found and nothing otherwise**.\n",
15621566
"\n",
15631567
"For example, given the `english.txt` file:\n",
15641568
"\n",
@@ -1600,6 +1604,7 @@
16001604
"source": [
16011605
"%%ipytest\n",
16021606
"\n",
1607+
"import csv\n",
16031608
"import pathlib as pl\n",
16041609
"\n",
16051610
"def solution_exercise4(english: pl.Path, dictionary: pl.Path) -> list[(str, str)]:\n",
@@ -1615,8 +1620,7 @@
16151620
" Returns:\n",
16161621
" - A list of tuples with the english / italian words\n",
16171622
"\"\"\"\n",
1618-
"\n",
1619-
" pass"
1623+
" return"
16201624
]
16211625
},
16221626
{
@@ -1626,7 +1630,7 @@
16261630
"tags": []
16271631
},
16281632
"source": [
1629-
"### Exercise 5: Binary format 🌶️🌶️🌶️"
1633+
"### Exercise 5: Binary format"
16301634
]
16311635
},
16321636
{
@@ -1636,8 +1640,9 @@
16361640
"tags": []
16371641
},
16381642
"source": [
1643+
"**Difficulty: 🌶️🌶️🌶️**\n",
16391644
"\n",
1640-
"The file `super_secret.dat` contains a secret message. We know that the message is stored in binary format as a sequence of bytes. The message starts with the byte sequence `b'\\xff\\xee\\xdd\\xcc\\xbb\\xaa'` and finishes with `b'\\xaa\\xbb\\xcc\\xdd\\xee\\xff'`.\n",
1645+
"The file `secret_file` contains a secret message. We know that the message is stored in binary format as a sequence of bytes. The message starts with the byte sequence `b'\\xff\\xee\\xdd\\xcc\\xbb\\xaa'` and finishes with `b'\\xaa\\xbb\\xcc\\xdd\\xee\\xff'`.\n",
16411646
"Write a function that reads the file and returns **only** the secret message as a string.\n",
16421647
"\n",
16431648
"\n",
@@ -1678,14 +1683,14 @@
16781683
" Returns:\n",
16791684
" - The secret message\n",
16801685
"\"\"\"\n",
1681-
"pass"
1686+
"return"
16821687
]
16831688
}
16841689
],
16851690
"metadata": {
16861691
"celltoolbar":"Slideshow",
16871692
"kernelspec": {
1688-
"display_name":"Python 3",
1693+
"display_name":"Python 3 (ipykernel)",
16891694
"language":"python",
16901695
"name":"python3"
16911696
},
@@ -1699,7 +1704,7 @@
16991704
"name":"python",
17001705
"nbconvert_exporter":"python",
17011706
"pygments_lexer":"ipython3",
1702-
"version":"3.12.4"
1707+
"version":"3.13.5"
17031708
}
17041709
},
17051710
"nbformat":4,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp