Introduction_to_Git_and_GitHub/module1.ipynb
2025-02-23 11:14:46 +03:00

290 lines
9.7 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.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"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><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",
"\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** <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",
"## 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
}