django-planet

Blog: James Bennett

web https://www.b-list.org/
Author James Bennett

Know your Python container types

Dec. 25, 2023 » James Bennett » [Archived Version]

This is the last of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Python contains multitudes There are a lot of container types available in the Python standard library, and it can be confusing sometimes to keep track of them all. So since it’s … Read full entry

Read More

Compare strings the right way

Dec. 24, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Unicode’s unique complexity It is the year 2023 — almost 2024! — and hopefully you’re using a programming language that is fully Unicode-aware. Python is; its string type is a sequence of … Read full entry

Read More

Set cookies the right way

Dec. 22, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Cookies in the cookie jar Django’s request and response objects, and their attributes and methods, make dealing with cookies easy. You can read from the request.COOKIES dictionary to get a cookie, and … Read full entry

Read More

Don't use Python's property

Dec. 22, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Attributing the problem Suppose you’re writing Java and you write a class with an attribute : public class MyClass { public int value; } And then later on you realize that value … Read full entry

Read More

Use Django's system checks

Dec. 21, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Check it out While you can do very minimal Django setups, more typical use cases tend to involve a mix of applications — your own, some third-party, and some from django.contrib — … Read full entry

Read More

Show Python deprecation warnings

Dec. 19, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Let this be a warning Python provides the ability to issue a warning as a step below raising an exception; warnings are issued by calling the warnings.warn() function, which at minimum … Read full entry

Read More

Running async tests in Python

Dec. 19, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. A-sync-ing feeling Async Python can be useful in the right situation, but one of the tricky things about it is that it requires a bit more effort to run than normal synchronous … Read full entry

Read More

Don't use class methods on Django models

Dec. 17, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. Being methodical about Python Python classes support three basic types of methods: Instance methods, which are what you get by default when writing a def statement inside a class body. These are … Read full entry

Read More

Say what you mean in a regex

Dec. 17, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. An URL-y warning Suppose you’re writing a blog in Django, and you get to the point where you’re setting up the URLs for the entries. Django has two ways to write … Read full entry

Read More

Python packaging: use the "src"

Dec. 15, 2023 » James Bennett » [Archived Version]

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction. A lurking problem Imagine you write a Python library named, say, foo. And you diligently set up the configuration to package it for distribution (which is not that hard; you can … Read full entry

Read More