Posts tagged ‘python’

(All tags.)


Truncating my blog posts with Python’s HTMLParser

Not a bowl of beautiful soup.

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 HTMLParser class. Here’s how I wrote it.

Read more...

How to Fix Pip “Yanked Version” Warnings

Are you yanking my chain?

Sometimes 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:

Read more...

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:

Read more...

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

Where did this warning come from?

Take this function:

Read more...

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.

Read more...

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.

Read more...

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.

Read more...

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.

Read more...

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.

Read more...

Python Type Hints - How to Use typing.Literal

This scraper is apparently the kind of tool a typographer would use.

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.

Read more...