I recently converted this blog to Pelican, a Python powered static site generator.
On the way I added a few customizations.
One customization is a Jinja template filter to truncate a post’s HTML as a summary, using Python’s
Here’s how I wrote it.
pip install will flag a warning saying “The candidate selected for download or install is a yanked version”.
For example, if we install attrs version 21.1.0:
Many languages follow the syntax of C, and use semicolons to indicate the end of a statement.
Take this function:
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.
Writing type hints gives us some familiarity with the
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.
re module lets us search both
bytes strings with regular expressions (regexes).
Our type checker can ensure we call
re functions with the correct types, thanks to some parametrized classes.
Here’s a recipe that combines
@overload to define a function that switches its return type based on the value of an argument.
“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.
To put it tautologically, type hints normally specify the types of variables.
But when a variable can only contain a limited set of literal values, we can use
typing.Literal for its type.
This allows the type checker to make extra inferences, giving our code an increased level of safety.