Automating Tasks Using Python For The Lean Entrepreneur

As you may know, many powerful and smart pieces of software are written in Python. Based on that, how I approach things (and my clients too) is: Just leverage these tools!

Here is an overview to get you started.

Getting started with Python the software

You can get started doing that in 3 steps:

  • Install Python on your computer.
  • Learn how to use PIP, the installation manager for add-on libraries. You generally need it to install software written in Python.
  • Identify a new software you want to try or use, and install it using PIP. And boom!... You can start leveraging that tool for your fun and profit.

If version 3.6 of Python is not installed on your computer, you may install it using the right Software Package Management tool (APT, Yum, Homebrew, Chocolatey...) For the alternative installation methods, such as using a Windows installer that you can download, visit the Python official website.

Note that Python is constantly being improved by its development team ; as of this writing, version 3.7 is in beta phase.

Let's talk about using PIP

PIP (or pip) is simply the Python add-ons installer program.

You can use PIP to install libraries and tools called modules or packages in Python jargon. The installation is typically done by automatically getting the module's source code from a web service called the Python Index.

To use PIP on Linux or MacOS, if you know the name of the module you want to install:

pip install <name_of_module>

On Windows:

pip.exe install <name_of_module>

Okay, but some examples please

Let's see examples of using PIP to add a library or tool to your Python installation.

  • The popular HTTP library Requests:
pip install requests
  • The HTTPie tool:
pip install httpie
  • The image manipulation library Pillow:
pip install pillow

Python comes with a Command Line Interface (CLI)

The Python program is usable in CLI or interactive mode. Which means that you could already be getting benefits, instantly after installing it, to do quick tasks like calculations or simple data manipulation.

First, you have to start the Python interpreter program (python or python.exe). You get a console in which you can write code that is interpreted interactively, which by the way is important for quickly experimenting with the tool and its features.

It would look like this:

python

>>> # (You are now in the Python interpreter.)
>>> 1 + 1
2
>>> x = 2
>>> y = 3
>>> x * y
6
>>> for x in range(5):
....    print(x)
0
1
2
3
4

Calling a custom script

Another usage is to run what is called a Python script: your code is written in a '.py' file, already tested (at least minimally), and would be interpreted as a whole to produce a result. Assuming you have a script file in your current folder, called "myscript.py", you run it using:

python myscript.py

Calling the main script of a module

A third trick is to run code from a module that is already available on the system, to produce a result without even knowing where exactly that code lives. A module brings a set of functionalities and you can use those from the computer's command line directly. That is if the module in question has exposed itself as a CLI program. You have to know such modules and check their documentation to know how to call them, but the general idea is that this way of calling Python code is possible by using the -m switch like this:

python -m <name_of_module>

This is a handy feature and will save you time in some circumstances. Since there are several modules inside Python by default (or that you may add yourself later), which expose their services via the "CLI" mode, it is like if we can use several "mini CLI programs" via Python. Let's see 2 examples to illustrate this.

1. To use the pydoc module in CLI mode:

python -m pydoc
pydoc - the Python documentation tool

pydoc <name> ...

Show text documentation on something.  <name> may be the name of a
Python keyword, topic, function, module, or package, or a dotted
reference to a class or function within a module or module in a
package.  If <name> contains a '\', it is used as the path to a
Python source file to document. If name is 'keywords', 'topics',
or 'modules', a listing of these things is displayed.

...

2. To use the venv module in CLI mode:

python -m venv -h

usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

...

And to create a virtual environment called "my_dev_env", you pass that name as argument: python -m venv my_dev_env.

Example modules you can start using for automating your work

The urllib3 module

urllib3 is the recommended low-level HTTP client library. It is actually used under the hood by the requests library, for handling some of the low-level details of the network connection.

Once the module is installed using pip, let's make an HTTP request to a web page or website:

import urllib3

http = urllib3.PoolManager()

r = http.request('GET', 'http://httpbin.org/')
print(r.status)
print(r)

If you are curious, what is done with these lines of code is that we create an object who's purpose is to handle the HTTP connection and its internal mechanisms for us ; we use it to send the request and we get a response from the remote server.

Note that normally, you will not stop there, since you have to do something with the HTTP response, such as parse it and extract data that you need.

As another example, here is a relatively advanced code that can be used to check if an API endpoint is responding:

import urllib3

API_ENDPOINT = 'http://example.com/api'

http = urllib3.PoolManager()
try:
    r = http.request('GET', API_ENDPOINT, retries=False, timeout=3.0)
    if r.status == 200:
        return True
    else:
        return False
except urllib3.exceptions.ConnectTimeoutError:
    return False

We return True if things are fine, else we return False.

Sometimes, instead of using urllib3, you would use the Requests library (which actually uses urllib3 internally), depending on your use case and context.

The json module

This is the JSON format manipulation module, needed nowadays when interacting with most HTTP APIs. Since it's what we call a standard module, there is nothing to install! It is already part of the default Python installation (in Python 3.x at least).

We can use its provided functions, dumps and loads, for our JSON data conversion tasks.

The dumps() function takes a Python data structure and returns it as a JSON string:

import json

data = [{'firtname': 'Kamon', 'lastname': 'Ayeva'}]

json_string = json.dumps(data)
print(json_string)

The loads() function takes a JSON string, for example the one we got in the example (json_string), and returns it as a Python data structure:

data2 = json.loads(json_string)
print(data2)

Here we converted back the JSON string.

The Pillow module

Once Pillow is installed with pip, using code like the following, it is easy to convert a group of images in JPEG format to PDF.

from PIL import Image

IMAGES = ['image1.jpg',
          'image2.jpg',
          'image3.jpg',
          ]

def convert_jpg2pdf(img_filename):
    print(f'Start converting {img_filename}')
    img = Image.open(img_filename)
    new_filename = img_filename.replace('.jpg', '.pdf')
    img.save(new_filename)
    res = f'Converted to {new_filename}'
    return res

for filename in IMAGES:
    result = convert_jpg2pdf(filename)
    print(result)

Where to go from here?

This was a quick overview to showcase how to leverage Python for more productivity or when you are at a stage in your project where you need to automate tasks, manipulate data, or experiment with ideas.

To get the complete picture and continue learning about these techniques and their advantages, check my free report about How To Leverage Automation For Your Projects.

Also see my other related posts on automation cases / experiments or Python solutions:

If you are interested in joining a group of entrepreneurs I dedicate my time to help, contact me or check my profile on Fiverr and see if you want to delegate a first task for evaluation purpose before committing to a larger scope of collaboration.

comments powered by Disqus

Need help for your project?

I can contribute to your project, working on a specific task, or doing all the coding based on your specifications using a web framework / CMS.