Automation. It’s supposed to make work easier. So, why not help it along?
Truth be told, automating even simple tasks robustly takes time and a lot of dependency management which can become very complex.
Nevertheless, it can be very rewarding to automate some simple tasks. Even if it was more for fun.
For those that are Python fans already, you know how friendly Python is for interacting with just about anything.
From sending HTTP requests, interacting with APIs, to loading and managing databases.
Python is a great choice for automation.
Here are five simple things you can automate that can help your various workflows and projects.
Imagine you have been scrolling through the newsfeed of your favorite social media app for the last ten minutes. Suddenly, your Lynk active symbol turns yellow. You’re inactive and not in a meeting.
Your nosey boss comes over and taps you on the shoulder.
Surprised, you look up to see him glaring down at you. He has a bad habit of micro-managing every second of your time.
“Hey, uh, I want you to relax and all but how is that module coming along,” he asks. You grit your teeth and provide some sort of polite answer.
“Cool, well, I just want to make sure you are focusing. Janice, our VP, is really expecting us to meet our deadline.”
End of interaction.
If only Lynk didn’t call you out like that.
Wait, you can get around this. After a few moments of Googling, you find a script to move your mouse and start coding up a job to run every few minutes that will make sure your mouse is moving.
These movements of the mouse and the keyboard can help make it look like you are active at work, in the case of Skype or Lynk.
The automation movement uses Python’s PyAutoGUI module. To install the PyAutoGUI module, run the following command:
pip install pyautogui
Python controls and tracks the mouse using your screen’s coordinate system. For instance, if you use a 1920x1080 screen resolution, the coordinate system for your screen would look like this:
The following functions are available under the PyAutoGUI module:
size(): The size() function lets you know your screen resolution.
import pyautogui print(pyautogui.size())
Running this file (saved as .py extension), the Python code uses the size() function to give the x, y format of your screen resolution as output:
moveTo(): The moveTo() function in the PyAutoGUI module moves the mouse. The code below uses the function, which takes the coordinates of x and y, and an optional duration argument. The function uses a specified time duration argument to move your mouse pointer from a current position to the coordinates of x and y.
import pyautogui print(pyautogui.size()) pyautogui.moveTo(150,100, duration = 2)
Running this Python script magically moves your mouse pointer from its present position to coordinates (150, 100), taking 2 seconds to complete the process.
Other than some of these basics, you can also use functions, such as click and typewrite, for both click and type strings.
All of which can be used to help ensure that your boss doesn’t catch you slacking off any time soon.
Many sites frown on using automated crawlers and programs to log in to their websites. But, it’s still a great skill to have.
Selenium is a useful library that can be used by multiple languages and that can help automate UI QA, or even to scrape websites with a login.
Although learning Selenium can take some time, start small. Just build a tool that can log in to your favorite site.
To get started, it’s essential that you install Chrome Driver and the Selenium library for Python. Use the command below to add the Selenium library.:
pip install Selenium
To start, we need to import some selected modules from Python’s Selenium library. Add the command below:
From selenium import webdriver driver = webdriver.Chrome()
Now, to automate the login process, we start with a simple task like a login form for a website.
The task entails:
- Navigating to the website.
- Finding login fields (usually username and password).
- Filling in your login details.
- Submitting login details to get you through the login screen automatically.
Tip: You may decide to define a “method” (for frequent logins) to enable reuse in other tests.
The code lines would look similar to:
From selenium import webdriver driver = webdriver.Chrome() def login(url,username,password,submit_button): driver.get(url) driver.find_element_by_id(username).send_keys("username") driver.find_element_by_id(password).send_keys("password") driver.find_element_by_id(password).(submit_button).click()
As you will notice, you need to get the elements you want to interact with. This is arguably one of the harder parts of creating a web-scraper or automated script that logs in to sites.
There are several ways to detect the elements of a web application to find the login fields.
Common ways of finding elements include ID (like the example above), CSS selectors, name, and XPaths.
Overall, watching Selenium automatically click buttons and log in to your favorite sites is mesmerizing. Things just start moving and working without your interaction.
Almost like magic!
Creating automatic backup files can be very useful if you perform regular backups.
You may be conversant with ZIP files (files with the .zip extension). ZIP files can contain several files with their compressed contents. And, as ZIP files can also hold many folders and subfolders, it becomes a handy way to backup files by packaging them into one.
A single ZIP file, called an archive file, can be created automatically using Python functions in the zipfile module.
You can also open (or extract) ZIP files using Python. The script is shared below.
This script was taken from the book Automate the Boring Stuff with Python (in case you want even more ideas).
import zipfile, os def backup(folder) folder = os.path.abspath(folder) offset=1 while True: zip_name=os.path.basename(folder)+'_'+str(offset)+'.zip' if not os.path.exists(zip_name): break offset+=1 print("Successfully created file " %s" %(zip_name)) backupzip=zipfile.ZipFile(zip_name,'w') for foldername,subfolders,filenames in os.walk(folder): backupzip.write(foldername) backupzip.close() print("done")
Another one-off idea that we recently saw and thought was an interesting way of automating tasks you might do quite often, is using a script to post multiple videos to Reddit.
Posting YouTube videos in Reddit threads can also be automated. The use of PRAW, a Python wrapper that enables you to scrape data, can offer much more to your Reddit experience.
To get started, install PRAW using pip.
The script below automatically posts YouTube videos to Reddit threads.
import praw video_url=str(input("Your video url:")) post_msg = str(input("Your post message")) reddit=praw.Reddit(client_id="***************", client_secret="************", user_agent="**********", username="************", password="***********") subreddit_list =["Python","PytonProjects","learningprogramming"] for i in subreddit_list: subreddit=reddit.subreddit(i) print("Posting video to" + i) subreddit.submit(post_msg, url=video_url) print("Done")
Automating tasks can be a lot of fun. Watching your program automatically log in to your favorite site, or load hundreds of files into a database, can make you feel as if you have re-designed the wheel.
And, there are a lot of great libraries out there that can help you automate tasks easily. Excel reports, emails, and other tasks can be simplified in a few lines of code.
Passing The System Design Interview For Software Engineers
Hadoop Vs Relational Databases
How Algorithms Can Become Unethical and Biased
How To Improve Your Data Driven Strategy
How To Develop Robust Algorithms
4 Must Have Skills For Data Scientists
SQL Best Practices — Designing An ETL Video