module 4 start
This commit is contained in:
parent
f2eb6e863f
commit
585f516863
@ -492,16 +492,22 @@
|
||||
"log = \"July 31 07:51:48 mycomputer bad_process[12345]: ERROR Performing package upgrade\"\n",
|
||||
"regex = r\"\\[(\\d+)\\]\"\n",
|
||||
"result = re.search(regex, log)\n",
|
||||
"# This will match any string with a number enclosed in square brackets like [12345] or [67890] etc. \n",
|
||||
"# But if the string does not have such a pattern, it returns None. \n",
|
||||
"# We'll use this function later to extract PID from our log strings.\n",
|
||||
"result = re.search(regex, \"A completely different string that also has numbers [34567]\")\n",
|
||||
"result = re.search(regex, \"99 elephants in a [cage]\")\n",
|
||||
"def extract_pid(log_line):\n",
|
||||
" # This function takes as input a log line (string) and returns the number within square brackets if such a pattern exists in the string\n",
|
||||
" regex = r\"\\[(\\d+)\\]\"\n",
|
||||
" result = re.search(regex, log_line)\n",
|
||||
" if result is None:\n",
|
||||
" return \"\"\n",
|
||||
" return result[1]\n",
|
||||
" if result is None: \n",
|
||||
" return \"No PID found\" # If no match found, return this message.\n",
|
||||
" return result[1] # The result[1] will be the number within square brackets in string format. \n",
|
||||
"print(extract_pid(log))\n",
|
||||
"print(extract_pid(\"99 elephants in a [cage]\"))\n",
|
||||
"# It prints '12345' as there is a pattern [12345] in the given log line\n",
|
||||
"print(extract_pid(\"99 elephants in a [cage]\")) \n",
|
||||
"# It prints 'No PID found' as there are no numbers within square brackets in this string.\n",
|
||||
"# Output:\n",
|
||||
"# 12345"
|
||||
]
|
||||
|
@ -1,11 +1,63 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Reading data interactively"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"source": [
|
||||
"cat hello.py\n",
|
||||
"#!/usr/bin/env python3\n",
|
||||
"\n",
|
||||
"name = input(\"Please enter your name: \")\n",
|
||||
"print(\"Hello, \" + name)\n",
|
||||
"\n",
|
||||
"# ./hello.py \n",
|
||||
"# Please enter your name: Roger\n",
|
||||
"# #Output will be Hello, Roger"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# defining a function called 'to_seconds' which takes hours, minutes and seconds as\n",
|
||||
"# inputs and returns their total equivalent in seconds by multiplying each with its\n",
|
||||
"# corresponding factor (3600 for hours, 60 for minutes) and then summing them up.\n",
|
||||
"def to_seconds(hours, minutes, seconds):\n",
|
||||
" return hours*3600+minutes*60+seconds\n",
|
||||
"\n",
|
||||
"# Print a welcome message to the user\n",
|
||||
"print(\"Welcome to this time converter\")\n",
|
||||
"\n",
|
||||
"# set 'cont' variable to \"y\" initially for entering into while loop.\n",
|
||||
"cont = \"y\"\n",
|
||||
"\n",
|
||||
"# while condition is true (user wants to continue), keep asking the\n",
|
||||
"# number of hours, minutes and seconds from the user and print their total equivalent\n",
|
||||
"# in seconds by calling 'to_seconds' function. Ask if they want to do another conversion.\n",
|
||||
"# If not, break the loop.\n",
|
||||
"while(cont.lower() == \"y\"):\n",
|
||||
" hours = int(input(\"Enter the number of hours: \"))\n",
|
||||
" minutes = int(input(\"Enter the number of minutes: \"))\n",
|
||||
" seconds = int(input(\"Enter the number of seconds: \"))\n",
|
||||
"\n",
|
||||
" print(\"That's {} seconds\".format(to_seconds(hours, minutes, seconds)))\n",
|
||||
" print()\n",
|
||||
" cont = input(\"Do you want to do another conversion? [y to continue] \")\n",
|
||||
" \n",
|
||||
"# print a goodbye message when user decides to stop the program.\n",
|
||||
"print(\"Goodbye!\")\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
Loading…
Reference in New Issue
Block a user