The

Python Bites

series

This blog series is all about Python! The posts cover various topics around the language from fundamentals to the usage of specific libraries.

Some of the posts are going to be longer while others will be on the shorter side (bite-sized). At some point they will also hopefully be accompanied by YouTube videos so you can choose your preferred format to consume the information.

If you have an idea for a topic that you’d like me to write about you can contact me on Twitter.

Three Dots in Python, What is the Ellipsis Object? Janne Kemppainen |

You might have stumbled on the Ellipsis object (…) in Python and wondered what it is used for. It was originally introduced to be used in the Numeric Python package for matrix slicing but nothing stops you from using it for other purposes too.

Environment Variables in Python Janne Kemppainen |

Environment variables are key-value pairs that are defined in your shell environment outside of the Python executable. You can access them in Python with the os.environ mapping object.

Environment variables are really useful for passing secret information or other configurations that you don’t want to pass as command line arguments. A good use case for example is a containerized application running in Docker or Kubernetes where you can configure the application using environment variables.

Building URLs in Python Janne Kemppainen |

Building URLs is really common in applications and APIs because most of the applications tend to be pretty interconnected. But how should we do it in Python? Here’s my take on the subject.

Create Your Own Python Packages

From programming to publishing

Janne Kemppainen |

When programming in Python you’re used to installing packages from the Python Package Index (PyPI). But how do the packages end up there? How can I make my Python code installable? Let’s find out!

Create Simple Python GUI Applications with Gooey Janne Kemppainen |

This time I’m going to show you how to create simple GUI applications in Python using a package called Gooey. It lets you turn (almost) any command line Python application into a GUI application with one line.

Chained Comparisons in Python Janne Kemppainen |

Can you chain comparison operations in Python? Yes you can, each comparison is evaluated pairwise so you can chain together as many of them you want.

It can be easy to forget to use basic features like this if you come from a language that doesn’t support chained comparisons or if you’ve never seen them used in the wild. I’ve been using Python professionally for years but I have to admit that I still didn’t really know about this feature until recently.

Dynamic Attributes in Python Janne Kemppainen |

When you define an object in Python you usually give it some attributes that hold the necessary pieces of information in a place that makes sense. However, Python does not limit the use of attributes to the set that were described at object creation time.

Return Many Values as Attributes in Python Janne Kemppainen |

When you need to return complex data from a function you typically think of two options:

  1. put the values in a dictionary
  2. create a new object/class

The first option is simple to implement but you need to access the individual values by their keys. The second option allows you to access data via attributes and do custom calculations behind the scenes, but then you need to implement yet another class.

Is there something in Python that could give us easy attribute access without having to bother with custom classes?

What is the Switch-Case Equivalent in Python? Janne Kemppainen |

Historically, the Python syntax hasn’t had a switch-case statement. In 2006 Guido van Rossum, the original author of Python, proposed different alternatives for the switch-case syntax in PEP 3103 but they all seemed to have some problems and the idea didn’t gain enough popular support. The proposal was therefore rejected. Python version 3.10 changes this.

Show a Progress Bar in Python Janne Kemppainen |

Python is a great language for writing command line scripts. When you need to run long running processes it is polite to indicate the overall progress to your user. You don’t want the user to think that your script has hanged and terminate the execution after a minute. Luckily, adding a progress indicator is really easy!

Python File Operations Janne Kemppainen |

Knowing how to handle files in your programming language of choice is an essential skill for any developer. After reading this post you should be comfortable doing file operations in Python.

Use StatsD to Measure Your Python App Metrics Janne Kemppainen |

If you’re a fan of DevOps, then you should also be enthusiastic about collecting telemetry from your production applications. StatsD is one solution that you could use to collect metrics in Python.

Run Selected Python Unit Tests from the Command Line Janne Kemppainen |

It doesn’t always make sense to run the full suite of tests when you’re developing a part of a program. So how can you run only a portion of your Python unit tests on the command line?

Combine Multiple Filter Conditions in Python Janne Kemppainen |

This problem occurred when I was trying to think of an alternative way for returning the first item from a list that matches given constraints. Using if statements inside a for loop is the obvious way to comb through the items but the built-in filter() function provides an interesting alternative.