Automate Your Boring Tasks with Python
In many critical areas, you can automate the completion of repetitive chores in an efficient and effective manner by using a computer language such as Python. When you are just starting out, it's vital to understand the fundamentals of Python via coding examples. However, if you want to improve your Python skills, you should concentrate on constructing things and automating real-world tasks.
This article focuses on fundamentals of Python and talks about how you can automate your boring stuff using Python.
Introduction to Python
Python is a flexible programming language that is simple to learn and use. This is mostly attributable to the fact that it is similar to the other well-known high-level programming languages and is licensed under an open-source model. It was first developed in 1991 by Guido van Rossum, and the Python Software Foundation later improved it.
Python's syntax is simple and straightforward, making it easier to read and comprehend than the syntax of other computer languages. Readability of the code was a primary design goal, and the structure of the language makes it possible for programmers to convey their thoughts with fewer lines of code.
Python programming language is both object-oriented and procedural. It is dynamically typed, therefore variable types aren't mandatory to be declared, however you can add type hints to your variables using Python 3. One simple example of the same is declaring vehicle = 10
which means, vehicle may be String, int, or whatever.
The fact that it is open-source also means that a wide array of tools, libraries, frameworks, and support are accessible for it. As a result of this, Python is a well-liked programming language for carrying out simple automations. Just like Python 2 earlier, Python 3 is now widely accepted and used by programmers.
Language Fundamentals
Python 3 has evolutionary and modest language changes. Python 3 is the updated print()
statement from Python 2, is as below:
print "Hello World" // python 2
print("Hello World") // python 3
The character encoding of strings changed drastically between the two language versions. Character encoding is the process of storing characters in bytes in a computer language.
Python 3's implementation of the Unicode standard allows it to handle the English, Arabic, and Greek alphabets, as well as emojis, mathematical expressions, etc. By the time Python 3 was released, the vast majority of contemporary programming languages already supported Unicode.
Typing is supported in Python 3, however it is not required. Therefore, even though Python 3 code may be written in the same way as Python 2 code, developers can leverage type hints to build code that is more readable, efficient, and helpful.
Automation Examples using Python
Python is a powerful language using which you can automate pretty much every mundane or complex task with some effort.
All you need to get started is Python installed on your machine and the appropriate libraries installed on your computer. For the brevity of this article, the usability and simplicity to automate repetitive tasks is demonstrated using Python.
Generate Secure Passwords
Did you know that Python can help you generate random passwords, that too without a fancy library or software installation?
Let's look at the code below:
import secrets
import string
character_choices = string.ascii_letters + string.digits + string.punctuation
print(''.join([secrets.choice(character_choices) for i in range(16)]))
This line of code prints a random password which you can actually use to secure your accounts! The range
decides the length of the generated string while the choice
decides the random combination of individual characters, 16 times chosen - that is printed as your password string.
Taking Screenshots
Have you ever come across scenarios where you need to take multiple screenshots to complete a task? Certainly, you may require to take screenshots while automating testing scenarios if not anything else. Let's look at how you can automate capturing screenshots using Python.
For the same, you'll require Selenium Webdriver, which will help you perform testing and automation in your web browser for this example. To install the Selenium Webdriver in this case, run the below command in your terminal before proceeding further:
pip install selenium
Once installed, you can begin writing your Python script as below:
1. from selenium import webdriver
2. from time import sleep
3. browser = webdriver.Chrome()
4. browser.get("https://blog.sentry.io")
5. sleep(1)
6. browser.get_screenshot_as_file(SentryBlog.png”)
7. browser.quit()
The above script helps you capture a screenshot of the Sentry.io blog page. Let's understand how this works.
webdriver
is imported from the selenium module, which helps you start your browser and make use of APIs to interact with browser web components.
sleep
function is imported from Python's time
module, which allows you to specify execution halt in seconds with sleep(1)
. The next line 3 and line 4 specifies the web browser and opens a web URL in the specified browser.
The Selenium Webdriver that you are using, by default halts till the website loads. However, some advanced websites may require sleep()
for pausing the code execution to complete the website loading.
Finally, the get_screenshot_as_file()
method captures a screenshot of the webpage visible. Then, the browser needs to be closed and the browser.quit()
line does the same for you.
Automated screenshots might make it easier to identify flaws and speed up the process of manually identifying and fixing them. Most significantly, it is expandable to the same extent as the application you are testing without the need for more testers. A direct consequence of all of the foregoing is that automatic screenshots save money and time.
Track the Price of your Favorite E-commerce Product
Have you ever tried to purchase your favorite products at the lowest possible price? You can track the price of your favorite products on the e-commerce platforms with a simple Python script.
To achieve the same, you need to make API calls using a simple Python script that will periodically check the price of your specified product on the e-commerce website and email you regarding the same.
Let’s have a look at how to achieve this:
For the brevity of this example, we are going to call BeautifulSoup
library to make the network calls along with other Python libraries to perform the task. This example showcases querying a product from Amazon’s website.
First of all, declare the necessary modules as imports:
import requests
from bs4 import BeautifulSoup as soup
import time
import smtplib
import getpass
Now you’re prepared to take your product’s URL as an input to receive updates about:
producturl = input("Enter the website URL of the product that you want to purchase:\n")
Then, you need to define functions that will do different tasks for you as shown below:
import requests
from bs4 import BeautifulSoup as soup
import time
import smtplib
import getpass
Now you’re prepared to take your product’s URL as an input to receive updates about:
producturl = input("Enter the website URL of the product that you want to purchase:\n")
Then, you need to define functions that will do different tasks for you as shown below:
def get_product_price():
title = soup.find(id="productTitle").get_text().strip()
try:
price = soup.find(
id="priceblock_ourprice_row").get_text().strip()[:20].replace( '$','').replace(' ','').replace('Price:','').replace('\n','').replace( '\xa0','').replace(',', '').replace('Fu', '')
except:
try:
price = soup.find(id="priceblock_dealprice").get_text().strip()[:20].replace( '$', '').replace(' ', '').replace('Price:', '').replace('\n', '').replace('\xa0','').replace(',','').replace('Fu', '')
except:
try:
price = soup.find(
id="priceblock_ourprice").get_text().strip()[:20].replace(
'$','').replace(' ', '').replace('Price:', '').replace('\n',
'').replace('\xa0','').replace(',', '').replace('Fu', '')
except:
price = soup.find(id="priceblock_ourprice_lbl").get_text()
.strip()[:20].replace('$', '').replace(' ', '').replace('Price:',
'').replace('\n','').replace('\xa0','').replace(',','').replace('Fu', '')
The above code checks the price of the product by parsing the elements in the HTML page using the HTML ids defined in each sub-block. As the code execution moves further, it checks for other prices mentioned on the page such as Deal Price, and Our Price.
You can automate email delivery using Python. Python's smtplib library lets you send emails through SMTP. You'll need a Gmail account; and you will need an App Password. You can find instructions on creating an App Password here.
First, you need to connect to Gmail SMTP.
In this example, you are using mail_id
and password
to ask for the email and password respectively, and smtplib
to build a connection and send emails. Once the appropriate built-in modules are imported at the beginning, following phases establish variables. Gmail needs HOST and PORT, constants that are capitalized.
The password and Gmail username are kept in the username variable. For password entry, use getpass
. It requests a password but doesn't repeat it. The script utilizes SMTP_SSL()
to secure SMTP connection.
def send_mail(mail_id, title, password):
server_mail = "smtp.gmail.com"
port = 587
server = smtplib.SMTP(server_mail, port)
server.ehlo()
server.starttls()
server.login(mail_id, password)
subject = "The price of the product is updated!"
body = f"Price of {title} is now below the minimum amount. Click on the link below to buy the product now:\n\n" + producturl
message = f'Subject:{subject}\n\n {body}'
server.sendmail(mail_id, mail_id, message)
server.quit()
Once the authentication is performed using the login()
method, you'll be able to send email with the sendmail
method. Here, it is important to appropriately close your open connection with the quit()
method.
You have defined the functions to fetch the price and execute email delivery when the price goes below a minimum value. Now you need to trigger this script on a periodic basis, i.e. every 60 minutes or so. You can modify and apply your own interval in the script. Here’s how to do it:
while 1:
get_product_price()
# checks at an interval of 1 hour (specified in seconds)
time.sleep(3600.00)
Here, while 1
acts as a true condition, always executing the code within the block, which executes the get_product_price()
method and then halts the execution for next 60 minutes and then executes the code again periodically.
Where to Go From Here
Python can be used to automate a wide range of other processes. There are a lot of jobs that you conduct on a daily basis, such as transferring information from one document to another or multiplying a figure by a hundred. Using the right set of tools and an understanding of scripting conventions, these jobs are easily automatable with Python. It gives you the opportunity to exercise their creativity and come up with one-of-a-kind automated solutions to the mundane tasks they confront on a daily basis.
To explore further, you can check out NASA's Code website, where they have open sourced a lot of Python projects to learn from. This popular GitHub repository is one of the goldmines of Python automation scripts, which consistently receives contributions from various contributors. You can also search more about the topic on GitHub.