Python life hacks for Everyday Use

Soham Biswas
The Startup
Published in
6 min readJun 6, 2020
Photo by David Clode on Unsplash

Python is an open-source, interpreted, high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. The inspiration behind it’s creation was to have an easy-to-read and powerful language that can gap the differences a shell scripting languages like Bash and compiled languages like C. Due to its open source and easy to understand nature various developers have contributed valuable libraries to the community.

Through this blog, I would like to introduce you to various such libraries which will make your everyday life easier. So without further ado, lets get started!

Here’s a quick overview of what I will be covering in this blog.

  • Wireless file transfer over LAN
  • Unique & random ID generation
  • Downloading Youtube videos
  • Converting video to audio
  • Resizing Images
  • Creating temporary emails
  • Generating Fake user information
  • Converting PDF files to images

Wireless file transfer over LAN

Have you ever been in a situation where you had to transfer files between your devices but you forgot to carry your cables? Well, don’t worry python has a solution for it. As long as your devices are connected to each other over a network, you can easily transfer files between them.

To achieve this we will initiate an http server to run on the device which has the file required for the transfer. To perform this we execute the following command in your terminal within the directory of the file, also make sure to note down your local IP of that particular device:

python3 -m http.server 8000

Here, 8000 refers to the port number at which you will be connecting in order to download that particular file.

Once you have the following message:

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …

Open up your browser at your receiving device and visit the following address:

http://<your-sender-local-ip>:8000
browser screenshot

Once you do that, you can simply click on your file and it will allow you to download it without any hassle.

Unique and random ID generation

Unique and random IDs can be generated in python with the help of Universal Unique Identifier library, also known as uuid. You can use this library in cryptography and hashing applications as well.

import uuid
print ("Random ID: ", uuid.uuid1())
# Output
#
# Random ID: 62760e74-02e3-11f8-b443-00163e920vdc

To know more about the uuid library, make sure to have a look at their offical docs.

Downloading Youtube videos

Downloading youtube videos has never been this easier before. With the help of PyTube one can easily download youtube video, audio or even playlists with a couple of lines of code. But first, in order to use PyTube we need to install it using a pip command:

pip3 install pytube3 --upgrade

Once you are done with the installation, you can use the following snippet to download the following:

# For downloading videos
from pytube import YouTube
YouTube('<video-url>').streams.get_highest_resolution().download()
# For downloading playlists
from pytube import Playlist
playlist = Playlist("<playlist-url>")
for video in playlist:
video.streams.get_highest_resolution().download()

To know more about the library make sure to visit it’s github page.

Converting Video to Audio

There are several libraries and techniques available in Python for the conversion of Video to Audio. One such library is Moviepy.

MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with Python 2.7+ and 3 (or only Python 3.4+ from v.1.0)

pip3 install moviepy

This module automatically installs FFmpeg. However, you might prompt to install in some cases.

import moviepy.editor as mp 

# Insert Local Video File Path
clip = mp.VideoFileClip(r"Video File")

# Insert Local Audio File Path
clip.audio.write_audiofile(r"Audio File")

Resizing images

There have been times when we require to resize our images for an application form or any other registration tasks. For this we can use the Pillow library which is an open source fork of the popular PIL image library in Python.

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

It can be easily installed by the following pip command:

pip3 install pillow

To resize existing image, the following code snippet can be used:

# Improting Image class from PIL module  
from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"path/for/image.png")
new_size = (300, 300)im = im.resize(new_size)
im.save() # saves the image in 300x300 resolution

To learn more about the library, visit thier documentation.

Create temporary emails

One of the most popular temporary email services is TempMail. Luckily, the same service also provides a python API as a library for users to use. One can use these temporary emails to login in sites which they think they will only visit once.

Installation for this can be easily done using pip:

pip3 install temp-mail

The following snippet can be used to initiate a temp email and view its recieved emails.

from tempmail import TempMailtm = TempMail()
email = tm.get_email_address() # v5gwnrnk7f@gnail.pw
print(tm.get_mailbox(email)) # list of emails

To know more visit their API documentation.

Generate Fake user information

Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

Faker allows you to generate fake yet believable user data. You can install faker using the following pip command:

pip3 install Faker

The following code snippet will introduce you to the Faker API:

from faker import Faker
fake = Faker()
fake.name()
# 'Lucy Cechtelar'
fake.address()
# '426 Jordy Lodge
# Cartwrightshire, SC 88120-6700'
fake.text()
# 'Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi
# beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt
# amet quidem. Iusto deleniti cum autem ad quia aperiam.
# A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui
# quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur
# voluptatem sit aliquam. Dolores voluptatum est.
# Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est.
# Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati.
# Et sint et. Ut ducimus quod nemo ab voluptatum.'

Faker can also be used to generate multiple fake & random user data using for loops:

# Generating fake names.
for _ in range(10):
print(fake.name())
# 'Adaline Reichel'
# 'Dr. Santa Prosacco DVM'
# 'Noemy Vandervort V'
# 'Lexi O'Conner'
# 'Gracie Weber'
# 'Roscoe Johns'
# 'Emmett Lebsack'
# 'Keegan Thiel'
# 'Wellington Koelpin II'
# 'Ms. Karley Kiehn V'

To know more about Faker, visit their github page.

Convert PDF files to Images

Converting PDF files to Images is very easy in Python. Thanks to the pdf2image library. This library is a python (3.5+) module that wraps pdftoppm and pdftocairo to convert PDF to a PIL Image object. Thus allowing you to further modify the Image to your needs.

It can also be installed easily with the help of pip:

pip3 install pdf2image

With the help of the following snippet you can easily convert a mutli-page PDF into images:

from pdf2image import convert_from_pathpages = convert_from_path('path/to/pdf/a.pdf', 500)
for page in pages:
page.save('p2ijpg', 'JPEG')

To know more about this library, make sure to visit its github page.

These were my handpicked everyday tasks which can be easily automated with the help of python. I use these life hacks myself and I would like to encourage others as well.

Thank you for reading, have a nice day!!

--

--