Updated M module1.ipynb

This commit is contained in:
Yavuz Sava 2025-02-23 15:30:22 +03:00
parent cf92d8fd47
commit e204462b08

View File

@ -2,11 +2,16 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"jp-MarkdownHeadingCollapsed": true,
"jupyter": {
"source_hidden": true
}
},
"source": [ "source": [
"# Diffing Files\n", "# Diffing Files\n",
"\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><path d=\"M4.5 17c-.412 0-.766-.147-1.06-.44A1.445 1.445 0 013 15.5v-11c0-.412.147-.766.44-1.06.294-.293.648-.44 1.06-.44h4.75c.213 0 .39.071.534.214a.72.72 0 01.216.532c0 .21-.072.39-.216.535a.72.72 0 01-.534.219H4.5v11h11v-4.75c0-.213.072-.39.214-.534a.72.72 0 01.532-.216c.21 0 .39.072.535.216a.72.72 0 01.219.534v4.75c0 .412-.147.766-.44 1.06-.294.293-.647.44-1.06.44h-11zm11-11.438L8.583 12.48a.681.681 0 01-.52.219.758.758 0 01-.521-.24.729.729 0 010-1.062L14.438 4.5H12.75a.728.728 0 01-.534-.214.72.72 0 01-.216-.532c0-.21.072-.39.216-.535A.72.72 0 0112.75 3h3.5c.212 0 .39.072.534.216A.726.726 0 0117 3.75v3.5c0 .213-.072.39-.214.534a.72.72 0 01-.532.216.734.734 0 01-.535-.216.72.72 0 01-.219-.534V5.562z\" fill=\"currentColor\" data-darkreader-inline-fill=\"\" style=\"--darkreader-inline-fill: currentColor;\"></path></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/tnlzg/diffing-files) \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", "\n",
"## Introduction\n", "## Introduction\n",
"\n", "\n",
@ -157,11 +162,15 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"jupyter": {
"source_hidden": true
}
},
"source": [ "source": [
"# Applying Changes\n",
"\n", "\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",
"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><path d=\"M4.5 17c-.412 0-.766-.147-1.06-.44A1.445 1.445 0 013 15.5v-11c0-.412.147-.766.44-1.06.294-.293.648-.44 1.06-.44h4.75c.213 0 .39.071.534.214a.72.72 0 01.216.532c0 .21-.072.39-.216.535a.72.72 0 01-.534.219H4.5v11h11v-4.75c0-.213.072-.39.214-.534a.72.72 0 01.532-.216c.21 0 .39.072.535.216a.72.72 0 01.219.534v4.75c0 .412-.147.766-.44 1.06-.294.293-.647.44-1.06.44h-11zm11-11.438L8.583 12.48a.681.681 0 01-.52.219.758.758 0 01-.521-.24.729.729 0 010-1.062L14.438 4.5H12.75a.728.728 0 01-.534-.214.72.72 0 01-.216-.532c0-.21.072-.39.216-.535A.72.72 0 0112.75 3h3.5c.212 0 .39.072.534.216A.726.726 0 0117 3.75v3.5c0 .213-.072.39-.214.534a.72.72 0 01-.532.216.734.734 0 01-.535-.216.72.72 0 01-.219-.534V5.562z\" fill=\"currentColor\" data-darkreader-inline-fill=\"\" style=\"--darkreader-inline-fill: currentColor;\"></path></svg>](https://www.coursera.org/learn/introduction-git-github/lecture/qIzdp/applying-changes).\n",
"\n", "\n",
"## Introduction\n", "## Introduction\n",
"\n", "\n",
@ -170,9 +179,9 @@
"You can follow along in the reading as the instructor discusses the code or review the code after watching the video.\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", "\n",
"\n", "```\n",
"cat cpu\\_usage.py \n", "cat cpu\\_usage.py \n",
"\n", "```\n",
"\n", "\n",
"\n", "\n",
"cat cpu\\_usage.py \n", "cat cpu\\_usage.py \n",
@ -197,10 +206,10 @@
"\n", "\n",
"    print(\"Everything ok\")\n", "    print(\"Everything ok\")\n",
"\n", "\n",
"1\n",
"\n", "\n",
"```\n",
"cat cpu\\_usage.diff \n", "cat cpu\\_usage.diff \n",
"\n", "```\n",
"\n", "\n",
"\n", "\n",
"**Code output:**\n", "**Code output:**\n",
@ -225,20 +234,20 @@
"\n", "\n",
" if not check\\_cpu\\_usage(75):\n", " if not check\\_cpu\\_usage(75):\n",
"\n", "\n",
"1\n",
"\n", "\n",
"```\n",
"patch cpu\\_usage.py < cpu\\_usage.diff \n", "patch cpu\\_usage.py < cpu\\_usage.diff \n",
"\n", "```\n",
"\n", "\n",
"\n", "\n",
"**Code output:**\n", "**Code output:**\n",
"\n", "\n",
"patching file cpu\\_usage.py\n", "patching file cpu\\_usage.py\n",
"\n", "\n",
"1\n",
"\n", "\n",
"```\n",
"cat cpu\\_usage.py \n", "cat cpu\\_usage.py \n",
"\n", "```\n",
"\n", "\n",
"\n", "\n",
"**Code output:**\n", "**Code output:**\n",
@ -263,6 +272,264 @@
"\n", "\n",
"    print(\"Everything ok\")" "    print(\"Everything ok\")"
] ]
},
{
"cell_type": "markdown",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"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",
"```\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",
"#!/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",
"\n",
"\n",
"\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",
"\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",
"\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",
"\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",
"\n",
"```\n",
"./disk\\_usage\\_fixed.py \n",
"```\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok\n",
"\n",
"diff \\-u disk\\_usage\\_original.py disk\\_usage\\_fixed.py > disk\\_usage.diff\n",
"\n",
"cat disk\\_usage.diff \n",
"\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",
"```\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",
"\n",
"./disk\\_usage.py \n",
"\n",
"\n",
"\n",
"**Code output:**\n",
"\n",
"Everything ok"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
} }
], ],
"metadata": { "metadata": {