127 Posts Tagged ‘python’ (Page 3)

(All tags.)


How to Make an Immutable Dict in Python

Immutaturtle.

Python’s built-in collection types come in mutable and immutable flavours, but one is conspicuously missing:

Read more...

Python Type Hints - How to Handle Optional Imports

Import, or import not, there is no try.

This post is not about importing typing.Optional, but instead imports that are themselves optional. Libraries often have optional dependencies, and the code should work whether or not the import is there. A common pattern to solve this to catch ImportError and replace the module with None:

Read more...

Set up a Gunicorn Configuration File, and Test It

Gnoo-icorn?

If you use Gunicorn, it’s likely you have a configuration file. This is a Python module that contains settings as module-level variables. Here’s an example with some essential settings:

Read more...

Preorder My New Book: Boost Your Django DX

Your author, hard at work.

Developer Experience (DX) is a catch-all term for anything that can improve your development workflow. Such improvements can help you write better code, faster, with fewer bugs.

Read more...

Python Type Hints - How to Type a Descriptor

Now invoking The Scroll of Descriptors.

The descriptor protocol allow us to completely customize attribute access. Python’s documentation describes the protocol with types involved described with words. Let’s look at how we can write those as type hints.

Read more...

A Python Script Template with Sub-commands (and Type Hints)

Now there are two of them!

Earlier this week I shared my Python script template. Here’s an extended version with sub-command support, and an example script.

Read more...

How to Create a Transparent Attribute Alias in Python

“You don’t know my secret alias,” said the hedgehog.

When dealing with evolvng API’s, it may be useful to rename an attribute in a class, but keep the old name around for backwards compatibility. This would mean making one attribute an alias for another. In this post we’ll look at two ways to achieve this.

Read more...

Three more uses for functools.partial() in Django

I remain partial to a plant-scroll.

I remain convinced that Python’s functools.partial() is underappreciated. Following my previous post, here are three more ways to use partial() with Django.

Read more...

The Many Ways to Exit in Python

Show me the exit

It’s fundamentally useful to exit your program when it’s done. Here are five(!) ways to do so in Python.

Read more...

A Python Script Template, with and without Type Hints and Async

Read on for a “cat trick”!

Python is great for writing scripts for the command line. In this post we’ll look at my script template, an example script, and some alternative versions (without type hints, and in async flavour).

Read more...