{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python 101\n", "\n", "## Part IV.\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "from helpers import *\n", "BASE = './data/'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Warming up: Deus ex Python\n", "\n", "### 1. Basic stats\n", "\n", "Create a function which computes the mean, the minimum, and the maximum value for a list of numbers!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lon = [random.random() for _ in range(30)]\n", "\n", "stat(lon)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### 2. Running mean\n", "\n", "Write a function which computes the running mean (window size: 3) for a list of numbers!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "running_mean(lon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Mismatching filenames\n", "\n", "You've downloaded a series complete with subtitles but the video and \n", "subtitle filenames don't match! Write a function which renames the \n", "mismatching subtitles!\n", "\n", "hint(s) - useful functions:\n", "\n", "- `download(_name, _season, _episodes, _mismatch)`\n", "- `string.lower()` (built-in)\n", "- `find_episode_number(filename)`\n", "- `rename_subtitle(original, new, target_dir)`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## File I/O\n", "\n", "### Reading from a file...\n", "...is actually really easy:\n", "\n", "- we need a filename" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "filename = BASE + 'text.txt'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- and a mode" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mode = 'r' # r stands for reading" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- and we have to open the file for reading" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_file = open(filename, mode)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A) We can iterate over on an opened file's lines directly:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for line in my_file:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python reads files like we read: start from the beginning - from the first line till the last one. Once he read all the lines, that's it. No more lines are left to read. To read a line from the same file, it has to __`seek`__ that position in the file before it can read it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_file.seek(0, 0) # help(file.seek)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### B) Or we can read every line into a list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lines_as_list = my_file.readlines()\n", "print(lines_as_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### C) Or read the whole file as string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_file.seek(0, 0)\n", "lines_as_string = my_file.read()\n", "print(lines_as_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do it either way... BUT! \n", "__DO NOT FORGET TO CLOSE IT__ once you finished working with it!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_file.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pretty easy, huh? What about..\n", "### Writing into a file?\n", "\n", "We need a filename, and a mode." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mode = 'w' # as you can guess, w stands for writing ;)\n", "my_file = open(filename, mode) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can write into the file directly:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_file.write('You take the red pill, you stay in Wonderland, '\n", " 'and I show you how deep the rabbit hole goes...')\n", "my_file.close() # again, don't forget to close the file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is more! Do you feel cumbersome to open and close the file? \n", "__Good news:__ You do not have to worry about at all! " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mode = 'r' \n", "with open(filename, mode) as my_file: \n", " for line in my_file.readlines(): \n", " print(line)\n", "# aaaaand it's closed ;)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can we add content to existing files?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Yes, we can!\n", "mode = 'a' # a stands for append\n", "with open(filename, mode) as my_file:\n", " my_file.write('Remember, all I\\'m offering is the truth, nothing more...')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Let's do some...\n", "\n", "\n", "
\n", "\n", "### Cool library of the week: moviepy - part II.\n", "- Create gifs from videos in a few lines" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import youtube_dl\n", "from moviepy.editor import VideoFileClip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "options = {'format': 'worstvideo', \n", " 'outtmpl': '%(id)s.%(ext)s'}\n", "with youtube_dl.YoutubeDL(options) as ydl:\n", " ydl.download([\"https://www.youtube.com/watch?v=RP8uhXuS2n8\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(VideoFileClip(\"RP8uhXuS2n8.webm\")\n", " .subclip((2.3),(3.7))\n", " .write_gif(\"whoa.gif\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can read more about gif creation here, and about youtube download options here.\n", "\n", "---\n", "\n", "## Let's see how how deep the rabbit hole goes! a.k.a \n", "## It's your turn - write the missing code snippets!\n", "\n", "#### 1. Write a specified matrix into a csv file called `matrix.csv` inside `BASE` directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = [[random.random() for row in range(10)] for line in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Write our fake \"download\" function\n", "With similar functionality as the one we used previously." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Merge the matching rows\n", "\n", "You have several entries from the same entities. Merge the corresponding field by adding the values from the separate rows together. Save the merged data to `\"merged.csv\"`. The contents of the `matching.csv` file are like this:\n", "\n", "id | name | val1 | val2\n", "---|---------|------|------\n", "1 | Neo | 5 | 44\n", "1 | Trinity | 10 | 32\n", "\n", "- Read the data from the `\"matching.csv\"`\n", "- Add the numerical values together (`val1` and `val2`) in the rows with matching ID values\n", "- Concatenate the string values in the `name` column and separate them with `' & '`\n", "\n", "\n", "For eg. After merging, the first row should look like this: \n", "\n", "id | name | val1 | val2\n", "---|----------------|------|------\n", "1 | Neo & Trinity | 15 | 76\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. Write a word counting function...\n", "...which reads a textfile and counts every word. return the top n words with their counts. \n", "Parameters: filename, n\n", "\n", "Hint(s):\n", "- use the `\"string\".split()` function \n", " eg: `print(\"a b c\".split())` results `['a', 'b', 'c']`\n", "- punctuation does not matter\n", "- see `help(sorted)`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5. Write a file encryptor function\n", "Which uses the `encrypt` function from the helpers.py to encrypt a file. Save the encrypted file (with the `originalfilename_encrypted.extension` name). \n", "Parameters: filename, strength (use this value in the encrypt function)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6. Write a decryptor function\n", "To decrypt the previously encrypted file, and save it decrypted (with the `originalfilename_decrypted.extension` name). \n", "Parameters: filename, strength" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Further exercises" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [random.random() for _ in range(30)]\n", "sorted_numbers = sorted(numbers) # sorts a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Compute the median\n", "\n", "Compute the median of the `sorted_numbers` sorted list of numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Compute the median\n", "\n", "**Create a function** which computes the median of a sorted list of numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(median(sorted_numbers))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Compute the median\n", "\n", "**Create a function** which computes the median of an **unsorted** list of numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(median(numbers))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. Compute the moving median\n", "\n", "**Create a function** which computes the **moving median** of a list of numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(median(numbers))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5. Compute the mode\n", "\n", "**Create a function** which computes the **[mode](https://en.wikipedia.org/wiki/Mode_(statistics))** of a list of numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mode(numbers))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus: Sort a list\n", "\n", "**Create a function** which sorts an unsorted list of numbers. \n", "The pseudo code for the **bubble-sort** can be find [here](https://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation).\n", "\n", "Bubble sort is a sorting algorithm that continuously steps through a list and swaps items next to each other until they appear in the correct order.\n", "\n", "

\"Bubble-sort-example-300px.gif\"
By Swfung8 @ Wikipedia

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def bubble_sort(numbers):\n", " \n", " return numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check if your implementation is correct:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert sorted(numbers) == bubble_sort(numbers), 'Error, sorting mismatch!'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 1 }