Troubleshooting_And_Debuggi.../module1.ipynb
2025-05-20 14:07:30 +03:00

206 lines
6.4 KiB
Plaintext
Raw Permalink 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": {},
"source": [
"# Understanding the Problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reading: ReadingReview: Intermittently failing script\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, allow you to practice running it, and can be used as a reference to refer back to. \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",
"/meeting\\_reminder$ ./meeting\\_reminder.sh \n",
"\n",
"```bash\n",
"#!/bin/bash\n",
"\n",
"meeting\\_info=$(zenity \\--forms \\\\\n",
"\n",
"    \\--title 'Meeting' \\--text 'Reminder information' \\\\\n",
"\n",
"    \\--add\\-calendar 'Date' \\--add\\-entry 'Title' \\\\\n",
"\n",
"    \\--add\\-entry 'Emails' \\\\\n",
"\n",
"    2>/dev/null)\n",
"\n",
"if \\[\\[ \\-n \"$meeting\\_info\" \\]\\]; then\n",
"\n",
"    python3 send\\_reminders.py \"$meeting\\_info\"\n",
"\n",
"fi\n",
"```\n",
"\n",
"```bash\n",
"def main():\n",
"\n",
"    if len(sys.argv) < 2:\n",
"\n",
"        return usage()\n",
"\n",
"    try:\n",
"\n",
"        date, title, emails = sys.argv\\[1\\].split('|')\n",
"\n",
"        message = message\\_template(date, title)\n",
"\n",
"        send\\_message(message, emails)\n",
"\n",
"        print(\"Successfully sent reminders to:\", emails)\n",
"\n",
"    except Exception as e:\n",
"\n",
"        print(\"Failure to send email\", file=sys.stderr)\n",
"\n",
"    except Exception as e:\n",
"\n",
"       print(\"Failure to send email: {}\".format(e), file=sys.stderr)\n",
"\n",
"```\n",
"\n",
"```bash\n",
"#!/bin/bash\n",
"\n",
"meeting\\_info=$(zenity \\--forms \\\\\n",
"\n",
"    \\--title 'Meeting' \\--text 'Reminder information' \\\\\n",
"\n",
"    \\--add\\-calendar 'Date' \\--add\\-entry 'Title' \\\\\n",
"\n",
"    \\--add\\-entry 'Emails' \\\\\n",
"\n",
"    \\--forms\\-date\\-format='%Y\\-%m\\-%d' \\\\\n",
"\n",
"    2>/dev/null)\n",
"\n",
"echo $meeting\\_info\n",
"\n",
"if \\[\\[ \\-n \"$meeting\\_info\" \\]\\]; then\n",
"\n",
"    python3 send\\_reminders.py \"$meeting\\_info\"\n",
"\n",
"fi\n",
"```\n",
"\n",
"```bash\n",
"def dow(date):\n",
"\n",
"    dateobj = datetime.datetime.strptime(date, r\"%Y-%m-%d\")\n",
"\n",
"    return dateobj.strftime(\"%A\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Binary Searching a Problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're curious about how linear and binary search look in code, here are a couple of implementations in Python:\n",
"```python\n",
"def linear_search(list, key):\n",
" \"\"\"If key is in the list returns its position in the list,\n",
" otherwise returns -1.\"\"\"\n",
" for i, item in enumerate(list):\n",
" if item == key:\n",
" return i\n",
" return -1\n",
"```\n",
"\n",
"```python\n",
"def binary\\_search(list, key):\n",
"\n",
"    \"\"\"Returns the position of key in the list if found, -1 otherwise.\n",
"\n",
"    List must be sorted.\n",
"\n",
"    \"\"\"\n",
"\n",
"    left = 0\n",
"\n",
"    right = len(list) - 1\n",
"\n",
"    while left <= right:\n",
"\n",
"        middle = (left + right) // 2\n",
"\n",
"        if list\\[middle\\] == key:\n",
"\n",
"            return middle\n",
"\n",
"        if list\\[middle\\] > key:\n",
"\n",
"            right = middle - 1\n",
"\n",
"        if list\\[middle\\] < key:\n",
"\n",
"            left = middle + 1\n",
"\n",
"    return \\-1\n",
"\n",
"```\n",
"\n",
"Don't worry if this seems complex! Understanding this code isnt required for understanding how to use binary search in troubleshooting."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Review: Finding invalid data\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, allow you to practice running it, and can be used as a reference to refer back to. \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",
"/import\\_data$ cat contacts.csv | ./import.py --server test\n",
"\n",
"/import\\_data$ wc -l contacts.csv \n",
"\n",
"/import\\_data$ head -15 contacts.csv \n",
"\n",
"/import\\_data$ tail -20 contacts.csv \n",
"\n",
"/import\\_data$ head -50 contacts.csv | ./import.py \\--server test\n",
"\n",
"/import\\_data$ head -50 contacts.csv | head -25 | ./import.py --server test\n",
"\n",
"/import\\_data$ head -50 contacts.csv | tail -25 | ./import.py --server test\n",
"\n",
"/import\\_data$ head -50 contacts.csv | tail -25 | head -13 | ./import.py --server test\n",
"\n",
"/import\\_data$ head -50 contacts.csv | tail -25 | tail -12 | head -6 | ./import.py --server test\n",
"\n",
"/import\\_data$ head -50 contacts.csv | tail -25 | tail -12 | head -6 | head -3 \n",
"\n",
"/import\\_data$ cat contacts.csv | ./import.py --server test\n",
"```"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}