Python – More List Methods and Recipes

Remove items from a List and more.

“Happiness is a perfume you cannot pour on others without getting some on yourself.” ― Ralph Waldo Emerson

1. Introduction

In a previous article, we covered some python list methods related to adding items. Let us now continue the review of more list methods.

Continue reading “Python – More List Methods and Recipes”

Python List Methods: Adding Items

Check out this refresher on ways to add items to python lists

“Share your knowledge. It is a way to achieve immortality.” ― Dalai Lama XIV

1. Introduction

Python offers the list data type which is similar to an array in C/C++ or Javascript or a List in Java. Of course, there are differences between the list in python and these languages. Sigh, wouldn’t it have been nice if all the common languages offered such data types with the same methods – same name, parameters, everything? Then you could have concentrated on mastering one and you would have it all. At the very least, this could have been done for common programming entities such as strings, list/array, characters, integer and float numbers, booleans, etc.

Continue reading “Python List Methods: Adding Items”

How to Avoid Deadlock when Calling External Command from Python

Use pipes from multiple threads for bidirectional communication.

“The truth will set you free, but first it will piss you off.” ― Joe Klaas, Twelve Steps to Happiness

1. Introduction

Invoking an external process from python and interacting with it can be quite tricky. This is especially true if the interaction is duplex i.e. involving both reading and writing to it. Such an interaction can cause deadlocks since both processes can end up waiting for output from the other. One way to avoid such deadlocks is to separate the reader and writer parts to different threads. We demonstrate such an approach in this article.

Continue reading “How to Avoid Deadlock when Calling External Command from Python”

Run External Command in Python Using Popen

Use subprocess.Popen for more control over the child process

“The world is a book and those who do not travel read only one page.” ― Augustine of Hippo

1. Introduction

In a previous article, we looked at calling an external command in python using That method allows you to accomplish several tasks during the invocation:

Continue reading “Run External Command in Python Using Popen”

How to Call an External Command in Python

Learn about the various aspects of the subprocess module.

“I hate quotations. Tell me what you know.” ― Ralph Waldo Emerson, The Essays of Ralph Waldo Emerson

1. Introduction

Do you want to run an external command from inside python? Maybe you want to startup a daemon process (no input or output), or perhaps invoke a command and capture its output?

Continue reading “How to Call an External Command in Python”

Python itertools – compress, dropwhile, takewhile, groupby

Boost your python expertise with these examples from itertools.

“Never put off till tomorrow what may be done day after tomorrow just as well.” ― Mark Twain

1. Introduction

We are continuing with our series on python itertools. In this article, we cover compress, dropwhile, takewhile and groupby.

Continue reading “Python itertools – compress, dropwhile, takewhile, groupby”

How to Check Whether a File Exists in Python?

A review of various methods in python to check for file existence.

“Knowledge is a treasure, but practice is the key to it.” ― Lao Tzu

1. Introduction

Python offers several alternative ways of checking whether a file exists. Each of these ways come with several quirks. Examine each and pick the one that suits you.

Continue reading “How to Check Whether a File Exists in Python?”

Python itertools – count, cycle and chain

A gentle introduction to python itertools

“Laughter is poison to fear.” ― George R.R. Martin, A Game of Thrones

1. Introduction

Python provides a module called itertools which, as the name suggests, provides a bunch of conveniences for dealing with iterations and looping. While you could spend your entire python career without ever having to touch this module, trust me when I say your life will be enriched if you at least know about what is available in itertools.

Continue reading “Python itertools – count, cycle and chain”

Updating a File from Multiple Threads in Python

Use locking to properly share resources across multiple threads

“You’re not obligated to win. You’re obligated to keep trying. To the best you can do everyday.” ― Jason Mraz

1. Introduction

Resource management in multi-threaded programs is a tricky situation. Say, for example, you have a counter variable which is read and modified by multiple threads. If you do not exercise proper care, it is possible that inconsistent values are read and/or propagated. This leads to subtle and hard-to-debug errors.

Continue reading “Updating a File from Multiple Threads in Python”

Python Threading Tutorial

A beginner’s guide to threading in python

“The truth is like salt. Men want to taste a little, but too much makes everyone sick.” ― Joe Abercrombie, The Heroes

1. Introduction

Let us learn about python threading. Threading refers to executing a task in a separate thread of control, freeing up the main thread for other activities. It is very useful for executing time-consuming tasks without holding up the rest of the program. Typical applications include servers which handle each request in a thread and programs with a user-interface which execute complex tasks in a background thread.

Continue reading “Python Threading Tutorial”