0% found this document useful (0 votes)
4 views26 pages

Python Sleep Course Slides

The document explains how to use Python's time.sleep() function to add time delays in code, including a practical example of building an uptime bot that checks the status of a URL at regular intervals. It also outlines next steps such as sending email notifications and deploying the script as a Flask app. Overall, it serves as a tutorial for implementing time delays and creating a simple monitoring tool.

Uploaded by

musicsaif123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Python Sleep Course Slides

The document explains how to use Python's time.sleep() function to add time delays in code, including a practical example of building an uptime bot that checks the status of a URL at regular intervals. It also outlines next steps such as sending email notifications and deploying the script as a Flask app. Overall, it serves as a tutorial for implementing time delays and creating a simple monitoring tool.

Uploaded by

musicsaif123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python sleep() : How to Add Time Delays to Your Code

Table of Contents
Use [Link]()
Build an Uptime Bot
Next Steps
Relax
Table of Contents
 1. Use [Link]()
2. Build an Uptime Bot
3. Next Steps
Take a Break
import time
[Link](3)
Take a Break
import time
[Link](3)
Take a Break
Were those really 3 seconds?
Take a Break
Were those really 3 seconds?

Note: Time for a test!


Take a Break
Were those really 3 seconds?

Note: Time for a test!

 Next Up: Terminal test time


Take a Break
import time
[Link](3)

Were those really 3 seconds?

Note: Time for a test!


Time It With timeit
import time
[Link](3)

python3 -m timeit -n 3 "import time; [Link](3)"


Time It With timeit
Default n is 1_000_000
You could sleep for half a year
Time It With timeit
Default n is 1_000_000
You could sleep for half a year

 Next Up: Build an Uptime Bot


Table of Contents
1. Use [Link]()
 2. Build an Uptime Bot
3. Next Steps
Uptime Bot
# uptime_bot.py
import time
import [Link]

def uptime_bot(url):
while True:
try:
[Link](url)
except Exception as e:
print(f"{e}: for {url}")
else:
print(f"{url} is up")
[Link](60)

if __name__ == "__main__":
url = "[Link]
uptime_bot(url)
Uptime Bot

 Next Up: Limit Bot Runs


Uptime Bot
# uptime_bot.py
import time
import [Link]

def uptime_bot(url):
while True:
try:
[Link](url)
except Exception as e:
print(f"{e}: for {url}")
else:
print(f"{url} is up")
[Link](60)

if __name__ == "__main__":
url = "[Link]
uptime_bot(url)
Tame Your Bot
# uptime_bot.py
import time
import [Link]

def uptime_bot(url, retries=3):


fails = 0
while fails < retries:
try:
[Link](url)
except Exception as e:
fails += 1
[Link](60)
# Send an Email

if __name__ == "__main__":
url = "[Link]
uptime_bot(url)
Table of Contents
1. Use [Link]()
2. Build an Uptime Bot
 3. Next Steps
Next Steps
Send Email Notifications
Deploy Your Script
Sending Emails With Python
Python Web Applications: Deploy Your Script as a Flask App
Next Steps
Send Email Notifications
Deploy Your Script
Next Steps
Send Email Notifications
Deploy Your Script

 Next Up: Summary


Python sleep() : How to Add Time Delays to Your Code
Table of Contents
Used [Link]()
Built an Uptime Bot
Learned about Next Steps
Relaxed and refreshed your Brain
Python sleep() : How to Add Time Delays to Your Code
More Use Cases in the Tutorial

You might also like