Articles about Python

Python Logo

Python is one of the most versatile programming languages in the world. With Python, you can control electronics, build web applications and perform machine learning.

In this page, you can find articles on Python references, coding and application setup.

Ensuring that your Supervisor subprocesses can run your Python applications properly behind your http proxy on Ubuntu Server 14.0.4

I had been using Supervisor to run my Python application for quite a while on a Ubuntu Server 14.0.4 box.

There was this OAuth feature that I had implemented on my Python Flask application to allow my users to sign in with their social account.

After completing the OAuth feature and ensuring that it worked fine on my development environment, I deployed the feature on my Ubuntu Server 14.0.4 instance.

However, my Python application encountered a HTTP request timeout error when it attempted to contact the OAuth server to authenticate my user login.

It turned out that there was no HTTP and HTTPS proxy settings available for my Python application to use when it tried to contact the OAuth server which is sitting somewhere in the Internet.

This post documents three ways which I had considered for propagating HTTP and HTTPS proxy settings to my Python application via the http_proxy and https_proxy environment variables.

Supervisor configurations to ensure that my Python Flask application releases binded port(s) during a supervisor restart

We use Supervisor to help keep our Python based applications running. One of our applications was built on the Python Flask framework to provide a RESTful api to connecting clients.

With continuous integration in place, we need to restart all our Supervisor managed applications whenever there is a change being merged to the master branch in our Git repository.

This post documents the Supervisor configurations to ensure that my Python Flask application releases any port that it had binded to when Jenkins send the command to restart the Supervisor and the processes that it manages.

How to look for unittest.TestCase subclasses defined in random Python scripts and run them in one shot

To ensure robustness of our Python application, members of the team had written unit test cases to assert that the various code units of the application are working as intended. Unit test cases that are meant to test the same program component are placed together as functions of the same class and contained in a Python script.

As the Python application grows, the number of test scripts grew as well. In an attempt to perform automated unit testing with Jenkins, the first thing that I did was to write a Python script that will look for all the test cases in the application directory and run them at one go.

This post describes the Python script in detail.

How to look for classes defined in Python 3 files dynamically

There are times when we need to write Python codes that extends the functionality of others, especially when we are writing a framework that allows for custom extension. To allow my Python program to be extended by others via the template design pattern, the first exploration task that I did was to find out how to dynamically look for classes defined in arbitrary Python files.

This post documents a way to look for classes defined in Python 3 files dynamically.

How to traverse all folders and files within a folder dynamically in Python 3

I had this requirement where I need to be able to look into a folder and pick up any configuration files for my Python 3 application. In order to achieve that, I first set an exploratory task to get the Python 3 code for traversing all folders and files within a folder.

For this exploratory task, I had created a script that will traverse the folders and files that are contained within a folder and print out their full paths.

How to get the directory path of a Python 3 script from within itself

One of the best way to reference files in Python scripts is to based the file path on the directory where the Python script resides in. This will allow us to always reference our files correctly no matter where our Python script is being executed.

This post documents the Python 3 code that I often used to get the directory path of a Python 3 script from within itself.