113 Posts Tagged ‘python’

(All tags.)


A Problem with Duplicated Mutable Constants

Hey! You’re just like me!

Here’s a small problem I’ve seen where several modules share versions of the same “constant” variable. It came up in the context of a Django project with multiple settings files, but it could happen in different contexts.

Read more...

Python Type Hints - How to Split Types by Python Version

Tweezers, check. sys.version_info, check.

The typing module continues to evolve, with new features in every Python version. This can make it tricky if you’re trying to type code that supports multiple Python versions. To help write such code, Mypy identifies version checks using sys.version_info and reads the appropriate branch.

Read more...

How to Fix a Python “SyntaxError: invalid character” Caused by Curly Quotes

Take a break, enjoy this field.

Here’s an innocent enough looking Python file:

Read more...

Make Simple Mocks With SimpleNamespace

Wild mock objects appeared!

When testing Python code you may need a mock object. That’s okay! But what’s the best way to build a simple mock?

Read more...

Book-Driven Development from “Boost Your Django DX”

Take my PR’s, thx

On Monday I released my new book “Boost Your Django DX”. It covers many tools and practices that are useful for developing Django projects.

Read more...

Removing Python 3.6 Support from My Packages

Bye Python 3.6.

Python 3.6 reached its end of life on the 23rd December. As its release manager put on the Python forum, it has gracefully “ridden off into the sunset”.

Read more...

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...