{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Diffing Files\n", "\n", "This reading contains the code used in the instructional videos from [**Diffing Files**Opens in a new tab](https://www.coursera.org/learn/introduction-git-github/lecture/tnlzg/diffing-files) \n", "\n", "## Introduction\n", "\n", "This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n", "\n", "You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n", "\n", "```bash\n", "\n", "cat rearrange1.py \n", "```\n", "\n", "**Code output:**\n", "\n", "#!/usr/bin/env python3\n", "\n", "import re\n", "\n", "def rearrange\\_name(name):\n", "\n", "    result = re.search(r\"^(\\[\\\\w .\\]\\*), (\\[\\\\w .\\]\\*)$\", name)\n", "\n", "    if result == None:\n", "\n", "        return name\n", "\n", "    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n", "\n", "user@ubuntu:~$ cat rearrange2.py \n", "\n", "#!/usr/bin/env python3\n", "\n", "import re\n", "\n", "def rearrange\\_name(name):\n", "\n", "    result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n", "\n", "    if result == None:\n", "\n", "        return name\n", "\n", "    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n", "\n", "1\n", "\n", "```bash\n", "cat rearrange2.py \n", "```\n", "\n", "**Code output:**\n", "\n", "#!/usr/bin/env python3\n", "\n", "import re\n", "\n", "def rearrange\\_name(name):\n", "\n", "    result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n", "\n", "    if result == None:\n", "\n", "        return name\n", "\n", "    return \"{} {}\".format(result\\[2\\], result\\[1\\])\n", "\n", "\n", "```bash\n", "diff rearrange1.py rearrange2.py \n", "```\n", "\n", "**Code output:**\n", "\n", "6c6\n", "\n", "<     result = re.search(r\"^(\\[\\\\w .\\]\\*), (\\[\\\\w .\\]\\*)$\", name)\n", "\n", "\\---\n", "\n", "\\>     result = re.search(r\"^(\\[\\\\w .-\\]\\*), (\\[\\\\w .-\\]\\*)$\", name)\n", "\n", "\n", "```bash\n", "diff validations1.py validations2.py \n", "```\n", "\n", "**Code output:**\n", "\n", "5c5,6\n", "\n", "< assert (type(username) == str), \"username must be a string\"\n", "\n", "\\--\n", "\n", "\\> if type(username != str: \n", "\n", "\\>     raise TypeError(\"username must be a string\"\n", "\n", "11a13,15\n", "\n", "\\>     return False\n", "\n", "\\> # Usernames can't begin with a number\n", "\n", "\\> if username\\[0\\].isnumeric():\n", "\n", "\n", "```bash\n", "diff \\-u validations1.py validations2.py \n", "```\n", "\n", "**Code output:**\n", "\n", "\\--- validations1.py 2019-06-06 14:28:49.639209499 +0200\n", "\n", "+++ validations2.py 2019-06-06 14:30:48.019360890 +0200\n", "\n", "@@ -2,7 +2,8 @@\n", "\n", " def validate\\_user(username, minlen):\n", "\n", "\\-    assert type(username) == str, \"username must be a string\"\n", "\n", "+    if type(username) != str:\n", "\n", "+        raise TypeError(\"username must be a string\")\n", "\n", "     if minlen < 1:\n", "\n", "         raise ValueError(\"minlen must be at least 1\")\n", "\n", "@@ -10,5 +11,8 @@\n", "\n", "         return False\n", "\n", "     if not username.isalnum():\n", "\n", "         return False\n", "\n", "+    # Usernames can't begin with a number\n", "\n", "+    if username\\[0\\].isnumeric():\n", "\n", "+        return False\n", "\n", "     return True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "This reading contains the code used in the instructional videos from [**Applying Changes** Opens in a new tab](https://www.coursera.org/learn/introduction-git-github/lecture/qIzdp/applying-changes).\n", "\n", "## Introduction\n", "\n", "This follow-along reading is organized to match the content in the video that follows. It contains the same code shown in the next video. These code blocks will provide you with the opportunity to see how the code is written and can be used as a reference as you work through the course. \n", "\n", "You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\n", "\n", "\n", "\n", "cat cpu\\_usage.py \n", "\n", "\n", "\n", "cat cpu\\_usage.py \n", "\n", "**Code output:**\n", "\n", "#!/usr/bin/env python3\n", "\n", "import psutil\n", "\n", "def check\\_cpu\\_usage(percent):\n", "\n", "    usage = psutil.cpu\\_percent()\n", "\n", "    return usage < percent\n", "\n", "if not check\\_cpu\\_usage(75):\n", "\n", "    print(\"ERROR! CPU is overloaded\")\n", "\n", "else:\n", "\n", "    print(\"Everything ok\")\n", "\n", "1\n", "\n", "cat cpu\\_usage.diff \n", "\n", "\n", "\n", "**Code output:**\n", "\n", "\\--- cpu\\_usage.py 2019-06-23 08:16:04.666457429 -0700\n", "\n", "+++ cpu\\_usage\\_fixed.py 2019-06-23 08:15:37.534370071 -0700\n", "\n", "@@ -2,7 +2,8 @@\n", "\n", " import psutil\n", "\n", " def check\\_cpu\\_usage(percent):\n", "\n", "\\-    usage = psutil.cpu\\_percent()\n", "\n", "+    usage = psutil.cpu\\_percent(1)\n", "\n", "+    print(\"DEBUG: usage: {}\".format(usage))\n", "\n", "     return usage < percent\n", "\n", " if not check\\_cpu\\_usage(75):\n", "\n", "1\n", "\n", "patch cpu\\_usage.py < cpu\\_usage.diff \n", "\n", "\n", "\n", "**Code output:**\n", "\n", "patching file cpu\\_usage.py\n", "\n", "1\n", "\n", "cat cpu\\_usage.py \n", "\n", "\n", "\n", "**Code output:**\n", "\n", "#!/usr/bin/env python3\n", "\n", "import psutil\n", "\n", "def check\\_cpu\\_usage(percent):\n", "\n", "    usage = psutil.cpu\\_percent(1)\n", "\n", "    print(\"DEBUG: usage: {}\".format(usage))\n", "\n", "    return usage < percent\n", "\n", "if not check\\_cpu\\_usage(75):\n", "\n", "    print(\"ERROR! CPU is overloaded\")\n", "\n", "else:\n", "\n", "    print(\"Everything ok\")" ] } ], "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.13.1" } }, "nbformat": 4, "nbformat_minor": 4 }