|
40 | 40 | " - [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n", |
41 | 41 | " - [Quiz on CSV](#Quiz-on-CSV)\n", |
42 | 42 | " - [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)" |
48 | 48 | ] |
49 | 49 | }, |
50 | 50 | { |
|
75 | 75 | "- connecting to databases or other network services\n", |
76 | 76 | "\n", |
77 | 77 | "\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." |
85 | 79 | ] |
86 | 80 | }, |
87 | 81 | { |
|
108 | 102 | "cell_type": "markdown", |
109 | 103 | "metadata": {}, |
110 | 104 | "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`." |
113 | 106 | ] |
114 | 107 | }, |
115 | 108 | { |
|
245 | 238 | "metadata": {}, |
246 | 239 | "outputs": [], |
247 | 240 | "source": [ |
248 | | - "%%ipytest debug\n", |
| 241 | + "%%ipytest\n", |
249 | 242 | "\n", |
250 | 243 | "def solution_print_odd(n: int) -> None: \n", |
251 | 244 | " \"\"\"Prints all odd numbers from 1 to n\n", |
|
255 | 248 | "\n", |
256 | 249 | " Returns:\n", |
257 | 250 | " - None (prints to console)\n", |
258 | | - " \"\"\"\n", |
259 | | - " pass" |
| 251 | + " \"\"\"" |
260 | 252 | ] |
261 | 253 | }, |
262 | 254 | { |
|
287 | 279 | " Returns:\n", |
288 | 280 | " - None (prints to console)\n", |
289 | 281 | " \"\"\"\n", |
290 | | - " pass\n", |
291 | 282 | "\n", |
292 | 283 | "solution_print_salutation()" |
293 | 284 | ] |
|
526 | 517 | "metadata": {}, |
527 | 518 | "source": [ |
528 | 519 | "\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", |
530 | 521 | "\n", |
531 | 522 | "<div class=\"alert alert-block alert-info\">\n", |
532 | 523 | " <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 | 604 | "\n", |
614 | 605 | " Returns:\n", |
615 | 606 | " - The number of directories in the directory\n", |
616 | | - " \"\"\"\n", |
617 | | - " pass\n" |
| 607 | + " \"\"\"" |
618 | 608 | ] |
619 | 609 | }, |
620 | 610 | { |
|
627 | 617 | "### Reading from a file\n", |
628 | 618 | "\n", |
629 | 619 | "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", |
631 | 621 | "\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", |
633 | 623 | "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", |
634 | 624 | "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", |
635 | 625 | "4. Finally, we close the file using the `close` method on the file object.\n" |
|
773 | 763 | " Returns:\n", |
774 | 764 | " - A list of strings, each representing a line in the file\n", |
775 | 765 | " \"\"\"\n", |
776 | | - " pass" |
| 766 | + " return" |
777 | 767 | ] |
778 | 768 | }, |
779 | 769 | { |
|
787 | 777 | "- We use `write` to write a *string* to the file. Other types of object should be converted to string before being written.\n", |
788 | 778 | "\n", |
789 | 779 | "\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```" |
791 | 781 | ] |
792 | 782 | }, |
793 | 783 | { |
|
902 | 892 | "\n", |
903 | 893 | " Returns:\n", |
904 | 894 | " - None (writes to file)\n", |
905 | | - " \"\"\"\n", |
906 | | - " pass" |
| 895 | + " \"\"\"" |
907 | 896 | ] |
908 | 897 | }, |
909 | 898 | { |
910 | 899 | "attachments": {}, |
911 | 900 | "cell_type": "markdown", |
912 | 901 | "metadata": {}, |
913 | 902 | "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", |
916 | 907 | " ```\n", |
917 | 908 | " first\n", |
918 | 909 | " second\n", |
919 | 910 | " ```\n", |
| 911 | + " \n", |
920 | 912 | " we expect the output file to contain these lines:\n", |
| 913 | + " \n", |
921 | 914 | " ```\n", |
922 | 915 | " first, 5\n", |
923 | 916 | " second, 6\n", |
924 | | - " ```" |
| 917 | + " ```\n", |
| 918 | + " \n", |
| 919 | + " Do not forget to add a **\\n** when writing lines in the file." |
925 | 920 | ] |
926 | 921 | }, |
927 | 922 | { |
|
945 | 940 | "\n", |
946 | 941 | " Returns:\n", |
947 | 942 | " - None (writes to file)\n", |
948 | | - " \"\"\"\n", |
949 | | - " pass" |
| 943 | + " \"\"\"" |
950 | 944 | ] |
951 | 945 | }, |
952 | 946 | { |
|
1201 | 1195 | "source": [ |
1202 | 1196 | "message = \"Ciao\"\n", |
1203 | 1197 | "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)" |
1205 | 1201 | ] |
1206 | 1202 | }, |
1207 | 1203 | { |
|
1225 | 1221 | "These packages are outside of the scope of this tutorial and will not be covered here.\n", |
1226 | 1222 | "\n", |
1227 | 1223 | "\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):" |
1229 | 1225 | ] |
1230 | 1226 | }, |
1231 | 1227 | { |
|
1330 | 1326 | "tags": [] |
1331 | 1327 | }, |
1332 | 1328 | "source": [ |
1333 | | - "### Exercise 1: CSV to dictionary 🌶️" |
| 1329 | + "### Exercise 1: CSV to dictionary" |
1334 | 1330 | ] |
1335 | 1331 | }, |
1336 | 1332 | { |
|
1340 | 1336 | "tags": [] |
1341 | 1337 | }, |
1342 | 1338 | "source": [ |
| 1339 | + "**Difficulty: 🌶️**\n", |
| 1340 | + "\n", |
1343 | 1341 | "Write a function that reads a CSV file and returns a dictionary.\n", |
1344 | 1342 | "\n", |
1345 | 1343 | "- The dictionary keys are in the first **column**\n", |
|
1404 | 1402 | " Returns:\n", |
1405 | 1403 | " - A dictionary with each row represented as a key, value pair\n", |
1406 | 1404 | " \"\"\"\n", |
1407 | | - " pass" |
| 1405 | + " return" |
1408 | 1406 | ] |
1409 | 1407 | }, |
1410 | 1408 | { |
|
1414 | 1412 | "tags": [] |
1415 | 1413 | }, |
1416 | 1414 | "source": [ |
1417 | | - "### Exercise 2: Counting words 🌶️" |
| 1415 | + "### Exercise 2: Counting words" |
1418 | 1416 | ] |
1419 | 1417 | }, |
1420 | 1418 | { |
|
1424 | 1422 | "tags": [] |
1425 | 1423 | }, |
1426 | 1424 | "source": [ |
| 1425 | + "**Difficulty: 🌶️**\n", |
| 1426 | + "\n", |
1427 | 1427 | "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", |
1428 | 1428 | "\n", |
1429 | 1429 | "For example, for the file\n", |
|
1443 | 1443 | " The file is available as the parameter <code>input_file</code> of <code>solution_exercise2</code> function\n", |
1444 | 1444 | " </li>\n", |
1445 | 1445 | " <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 into it'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 into its words.\n", |
1447 | 1447 | " </li>\n", |
1448 | 1448 | " </ul>\n", |
1449 | 1449 | "</div>\n", |
|
1472 | 1472 | " Returns:\n", |
1473 | 1473 | " - The number of words in the file\n", |
1474 | 1474 | " \"\"\"\n", |
1475 | | - " pass" |
| 1475 | + " return" |
1476 | 1476 | ] |
1477 | 1477 | }, |
1478 | 1478 | { |
|
1482 | 1482 | "tags": [] |
1483 | 1483 | }, |
1484 | 1484 | "source": [ |
1485 | | - "### Exercise 3: Letter statistics 🌶️🌶️" |
| 1485 | + "### Exercise 3: Letter statistics" |
1486 | 1486 | ] |
1487 | 1487 | }, |
1488 | 1488 | { |
|
1492 | 1492 | "tags": [] |
1493 | 1493 | }, |
1494 | 1494 | "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", |
1496 | 1498 | "\n", |
1497 | 1499 | "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", |
1498 | 1500 | "\n", |
|
1522 | 1524 | "metadata": {}, |
1523 | 1525 | "outputs": [], |
1524 | 1526 | "source": [ |
1525 | | - "%%ipytest input_output\n", |
| 1527 | + "%%ipytest\n", |
1526 | 1528 | "\n", |
1527 | 1529 | "import pathlib as pl\n", |
1528 | 1530 | "import string\n", |
|
1538 | 1540 | " Returns:\n", |
1539 | 1541 | " - A dictionary with a count of each letter\n", |
1540 | 1542 | " \"\"\"\n", |
1541 | | - " pass" |
| 1543 | + " return" |
1542 | 1544 | ] |
1543 | 1545 | }, |
1544 | 1546 | { |
|
1548 | 1550 | "tags": [] |
1549 | 1551 | }, |
1550 | 1552 | "source": [ |
1551 | | - "### Exercise 4: Translating words 🌶️🌶️" |
| 1553 | + "### Exercise 4: Translating words" |
1552 | 1554 | ] |
1553 | 1555 | }, |
1554 | 1556 | { |
|
1558 | 1560 | "tags": [] |
1559 | 1561 | }, |
1560 | 1562 | "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", |
1562 | 1566 | "\n", |
1563 | 1567 | "For example, given the `english.txt` file:\n", |
1564 | 1568 | "\n", |
|
1600 | 1604 | "source": [ |
1601 | 1605 | "%%ipytest\n", |
1602 | 1606 | "\n", |
| 1607 | + "import csv\n", |
1603 | 1608 | "import pathlib as pl\n", |
1604 | 1609 | "\n", |
1605 | 1610 | "def solution_exercise4(english: pl.Path, dictionary: pl.Path) -> list[(str, str)]:\n", |
|
1615 | 1620 | " Returns:\n", |
1616 | 1621 | " - A list of tuples with the english / italian words\n", |
1617 | 1622 | " \"\"\"\n", |
1618 | | - "\n", |
1619 | | - " pass" |
| 1623 | + " return" |
1620 | 1624 | ] |
1621 | 1625 | }, |
1622 | 1626 | { |
|
1626 | 1630 | "tags": [] |
1627 | 1631 | }, |
1628 | 1632 | "source": [ |
1629 | | - "### Exercise 5: Binary format 🌶️🌶️🌶️" |
| 1633 | + "### Exercise 5: Binary format" |
1630 | 1634 | ] |
1631 | 1635 | }, |
1632 | 1636 | { |
|
1636 | 1640 | "tags": [] |
1637 | 1641 | }, |
1638 | 1642 | "source": [ |
| 1643 | + "**Difficulty: 🌶️🌶️🌶️**\n", |
1639 | 1644 | "\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", |
1641 | 1646 | "Write a function that reads the file and returns **only** the secret message as a string.\n", |
1642 | 1647 | "\n", |
1643 | 1648 | "\n", |
|
1678 | 1683 | " Returns:\n", |
1679 | 1684 | " - The secret message\n", |
1680 | 1685 | " \"\"\"\n", |
1681 | | - " pass" |
| 1686 | + " return" |
1682 | 1687 | ] |
1683 | 1688 | } |
1684 | 1689 | ], |
1685 | 1690 | "metadata": { |
1686 | 1691 | "celltoolbar": "Slideshow", |
1687 | 1692 | "kernelspec": { |
1688 | | - "display_name": "Python 3", |
| 1693 | + "display_name": "Python 3 (ipykernel)", |
1689 | 1694 | "language": "python", |
1690 | 1695 | "name": "python3" |
1691 | 1696 | }, |
|
1699 | 1704 | "name": "python", |
1700 | 1705 | "nbconvert_exporter": "python", |
1701 | 1706 | "pygments_lexer": "ipython3", |
1702 | | - "version": "3.12.4" |
| 1707 | + "version": "3.13.5" |
1703 | 1708 | } |
1704 | 1709 | }, |
1705 | 1710 | "nbformat": 4, |
|
0 commit comments