115 Posts Tagged ‘python’

(All tags.)


How to Patch Requests to Have a Default Timeout

Summon forth the armies, but with a timeout of five seconds.

Python’s requests package is very popular. Even if you don’t use it directly, it’s highly likely one of your dependencies does.

Read more...

Appearance on Podcast.__init__

Doot-dee-doot.

I joined host Tobias Macey on Podcast.__init__, in Episode 349, published this monday. The episode is titled “Improve Your Productivity By Investing In Developer Experience Design For Your Projects”. We covered various topics related to “developer experience” and general ways to improve it on any Python project.

Read more...

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