Recent Posts (Page 2)

Does Python support semicolons?

Are semicolons just for dragons?

Many languages follow the syntax of C, and use semicolons to indicate the end of a statement. For example, in JavaScript:


Why does Python log a warning for “invalid decimal literal”?

Where did this warning come from?

Take this function:


Introducing django-upgrade, a tool for upgrading your Django projects

Giddy-up, we’re going to the future!

Django deprecates a small list of features with every feature release, requiring us to update our projects, which can be monotonous. Today I’m announcing a new tool I’ve created, django-upgrade, that automates some of this drudgery for us all.


I converted my Lambda@Edge Function to CloudFront Functions

How to function in the clouds.

When Lambda@Edge first came out, I added it to my blog’s CloudFront distribution in order to add security headers. Then, when Lambda@Edge added Python support, I converted my function from JavaScript to Python.


Three Cheers for blacken-docs

Blacken all ye docs.

Black is the de facto standard code formatter for Python, and these days I use it on all my projects. blacken-docs is a tool that also allows you to apply Black to code samples in your docs. I recently rolled it out on my projects to great effect.


This Blog Is Now a Pythonic Pelican-Powered Publication

The pelican takes flight...

I started this blog in 2014 using the popular Jekyll. Whilst it served me well, I’ve wanted to migrate to a Python-based tool for a while now, for a few reasons:


Python Type Hints - Use Cases for the types Module

Some kind of tool for working with types.

Writing type hints gives us some familiarity with the typing module. But Python also includes the similarly-named types module, which can also come in handy. Let’s look at the history of these two modules, some use cases of types, and one way in which it’s not so useful.


Python Type Hints - How to Work with Regular Expressions

Searching again for a needle in a haystack.

Python’s re module lets us search both str and bytes strings with regular expressions (regexes). Our type checker can ensure we call re functions with the correct types, thanks to some parametrized classes.


Python Type Hints - How to Vary Return Type Based on an Argument

So many ropes, so many overloads.

Here’s a recipe that combines typing.Literal with @overload to define a function that switches its return type based on the value of an argument.


Python Type Hints - How to Avoid “The Boolean Trap”

Well that looks like a finger trap.

“The Boolean Trap” is a programming anti-pattern where a boolean argument switches behaviour, leading to confusion. In this post we’ll look at the trap in more detail, and several ways to avoid it in Python, with added safety from type hints.