Introduction_to_Git_and_GitHub/module1.ipynb
2025-02-24 01:06:58 +03:00

655 lines
19 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"jupyter": {
"source_hidden": true
}
},
"source": [
"# Diffing Files\n",
"\n",
"This reading contains the code used in the instructional videos from [**Diffing Files**<svg aria-labelledby=\"cds-react-aria402160580-:rim:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria402160580-:rim:\"><title id=\"cds-react-aria402160580-:rim:-title\">Opens in a new tab</title></svg>](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": {
"jupyter": {
"source_hidden": true
}
},
"source": [
"# Applying Changes\n",
"\n",
"This reading contains the code used in the instructional videos from [**Applying Changes** <svg aria-labelledby=\"cds-react-aria2448342354-:r9g:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria2448342354-:r9g:\"><title id=\"cds-react-aria2448342354-:r9g:-title\">Opens in a new tab</title></svg>](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",
"\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",
"\n",
"```\n",
"patch cpu\\_usage.py < cpu\\_usage.diff \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"patching file cpu\\_usage.py\n",
"\n",
"\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\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practical Application of diff and patch\n",
"\n",
"This reading contains the code used in the instructional videos from [**Practical application of diff and patch**<svg aria-labelledby=\"cds-react-aria8760104591-:r2f:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:r2f:\"><title id=\"cds-react-aria8760104591-:r2f:-title\">Opens in a new tab</title></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/IkuqC/practical-application-of-diff-and-patch). \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",
"```bash\n",
"cp disk\\_usage.py disk\\_usage\\_original.py \n",
"\n",
"cp disk\\_usage.py disk\\_usage\\_fixed.py \n",
"```\n",
"\n",
"\n",
"## **File with code**\n",
"\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    return 1\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"return 0\n",
"\n",
"./disk\\_usage\\_fixed.py \n",
"\n",
"#this throws an error\n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"File \"./disk\\_usage\\_fixed.py\", line 19\n",
"\n",
"    return 1\n",
"\n",
"    ^\n",
"\n",
"SyntaxError: 'return' outside function\n",
"\n",
"## File with code\n",
"\n",
"The instructor adds import sys at the beginning, then change return 1 to sys.exit(1) and return 0 to sys.exit(0).\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"import sys\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    sys.exit(1)\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"sys.exit(0)\n",
"\n",
"./disk\\_usage\\_fixed.py\n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"ERROR: Not enough disk space\n",
"\n",
"## File with code\n",
"\n",
"Instructor changed change check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10) in previous file to check\\_disk\\_usage(\"/\", 2, 10).\n",
"```bash\n",
"#!/usr/bin/env python3\n",
"\n",
"import shutil\n",
"\n",
"import sys\n",
"\n",
"def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"    \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"    du = shutil.disk\\_usage(disk)\n",
"\n",
"    # Calculate the percentage of free space\n",
"\n",
"    percent\\_free = 100 \\* du.free / du.total\n",
"\n",
"    # Calculate how many free gigabytes\n",
"\n",
"    gigabytes\\_free = du.free / 2\\*\\*30\n",
"\n",
"    if percent\\_free < min\\_percent or gigabytes\\_free < min\\_absolute:\n",
"\n",
"        return False\n",
"\n",
"    return True\n",
"\n",
"# Check for at least 2 GB and 10% free\n",
"\n",
"if not check\\_disk\\_usage(\"/\", 2, 10):\n",
"\n",
"    print(\"ERROR: Not enough disk space\")\n",
"\n",
"    sys.exit(1)\n",
"\n",
"print(\"Everything ok\")\n",
"\n",
"sys.exit(0)\n",
"\n",
"./disk\\_usage\\_fixed.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok\n",
"\n",
"```bash\n",
"diff \\-u disk\\_usage\\_original.py disk\\_usage\\_fixed.py > disk\\_usage.diff\n",
"\n",
"cat disk\\_usage.diff \n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"\\--- disk\\_usage\\_original.py 2019-06-22 15:13:38.591579963 -0700\n",
"\n",
"+++ disk\\_usage\\_fixed.py 2019-06-22 15:41:35.013023839 -0700\n",
"\n",
"@@ -1,6 +1,7 @@\n",
"\n",
" #!/usr/bin/env python3\n",
"\n",
" import shutil\n",
"\n",
"+import sys\n",
"\n",
" def check\\_disk\\_usage(disk, min\\_absolute, min\\_percent):\n",
"\n",
"     \"\"\"Returns True if there is enough free disk space, false otherwise.\"\"\"\n",
"\n",
"@@ -14,9 +15,9 @@\n",
"\n",
"     return True\n",
"\n",
" # Check for at least 2 GB and 10% free\n",
"\n",
"\\-if not check\\_disk\\_usage(\"/\", 2\\*2\\*\\*30, 10):\n",
"\n",
"+if not check\\_disk\\_usage(\"/\", 2, 10):\n",
"\n",
"     print(\"ERROR: Not enough disk space\")\n",
"\n",
"\\-    return 1\n",
"\n",
"+    sys.exit(1)\n",
"\n",
" print(\"Everything ok\")\n",
"\n",
"\\-return 0\n",
"\n",
"+sys.exit(0)\n",
"\n",
"```bash\n",
"patch disk\\_usage.py < disk\\_usage.diff \n",
"\n",
"patching file disk\\_usage.py\n",
"```\n",
"\n",
"**Code output:**\n",
"\n",
"patching file disk\\_usage.py\n",
"```bash\n",
"./disk\\_usage.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Study guide: diff and patch\n",
"\n",
"## The diff command\n",
"\n",
"You use the diff command to find the differences between two files. On its own, its a bit hard to use; instead, use diff -u to find lines that differ in two files:\n",
"\n",
"## Using diff -u\n",
"\n",
"You use the diff -u command to compare two files, line by line, and have the differing lines compared side-by-side in the same output. For an example of what this looks like, see below:\n",
"\n",
"```bash\n",
"~$ cat menu1.txt \n",
"\n",
"Menu1:\n",
"\n",
"Apples\n",
"\n",
"Bananas\n",
"\n",
"Oranges\n",
"\n",
"Pears\n",
"\n",
"~$ cat menu2.txt \n",
"\n",
"Menu:\n",
"\n",
"Apples\n",
"\n",
"Bananas\n",
"\n",
"Grapes\n",
"\n",
"Strawberries\n",
"\n",
"~$ diff \\-u menu1.txt menu2.txt \n",
"\n",
"\\--- menu1.txt   2019\\-12-16 18:46:13.794879924 +0900\n",
"\n",
"+++ menu2.txt   2019\\-12-16 18:46:42.090995670 +0900\n",
"\n",
"@@ \\-1,6 +1,6 @@\n",
"\n",
"\\-Menu1:\n",
"\n",
"+Menu:\n",
"\n",
" Apples\n",
"\n",
" Bananas\n",
"\n",
"\\-Oranges\n",
"\n",
"\\-Pears\n",
"\n",
"+Grapes\n",
"\n",
"+Strawberries\n",
"```\n",
"\n",
"## The patch command\n",
"\n",
"The patch command is useful for applying file differences. See the example below, which compares two files. The comparison is saved as a .diff file, which is then patched to the original file!\n",
"\n",
"```bash\n",
"~$ cat hello\\_world.txt \n",
"\n",
"Hello World\n",
"\n",
"~$ cat hello\\_world\\_long.txt \n",
"\n",
"Hello World\n",
"\n",
"It's a wonderful day!\n",
"\n",
"~$ diff -u hello\\_world.txt hello\\_world\\_long.txt \n",
"\n",
"\\--- hello\\_world.txt     2019-12-16 19:24:12.556102821 +0900\n",
"\n",
"+++ hello\\_world\\_long.txt        2019-12-16 19:24:38.944207773 +0900\n",
"\n",
"@@ -1 +1,3 @@\n",
"\n",
" Hello World\n",
"\n",
"+\n",
"\n",
"+It's a wonderful day!\n",
"\n",
"~$ diff \\-u hello\\_world.txt hello\\_world\\_long.txt > hello\\_world.diff\n",
"\n",
"~$ patch hello\\_world.txt < hello\\_world.diff \n",
"\n",
"patching file hello\\_world.txt\n",
"\n",
"~$ cat hello\\_world.txt \n",
"\n",
"Hello World\n",
"\n",
"It's a wonderful day!\n",
"```\n",
"\n",
"## Resources for more information\n",
"\n",
"There are other interesting patch and diff commands such as patch -p1 and diff -r . For more information on these commands, check out the following resources:\n",
"\n",
"- [http://man7.org/linux/man-pages/man1/diff.1.html<svg aria-labelledby=\"cds-react-aria8760104591-:rgs:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:rgs:\"><title id=\"cds-react-aria8760104591-:rgs:-title\">Opens in a new tab</title></svg>](http://man7.org/linux/man-pages/man1/diff.1.html)\n",
"- [http://man7.org/linux/man-pages/man1/patch.1.html<svg aria-labelledby=\"cds-react-aria8760104591-:rgu:-title\" fill=\"none\" focusable=\"false\" height=\"16\" role=\"img\" viewBox=\"0 0 20 20\" width=\"16\" class=\"css-8blerm\" id=\"cds-react-aria8760104591-:rgu:\"><title id=\"cds-react-aria8760104591-:rgu:-title\">Opens in a new tab</title></svg>](http://man7.org/linux/man-pages/man1/patch.1.html)"
]
}
],
"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
}