Latest entries in category 'Python'https://www.b-list.org/weblog/categories/python/2023-12-24T21:08:26.132721-06:00James BennettKnow your Python container types2023-12-24T19:07:54-06:002023-12-24T21:08:26.132721-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-24:/weblog/2023/dec/24/python-container-types/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Python contains multitudes</h2>
<p>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 …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/24/python-container-types/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Compare strings the right way2023-12-23T19:14:06-06:002023-12-23T21:14:40.116268-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-23:/weblog/2023/dec/23/compare-python-strings/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Unicode’s unique complexity</h2>
<p>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 …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/23/compare-python-strings/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Set cookies the right way2023-12-22T17:29:56-06:002023-12-22T19:30:26.197730-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-22:/weblog/2023/dec/22/set-django-cookies/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Cookies in the cookie jar</h2>
<p>Django’s request and response objects, and their attributes and methods, make dealing with cookies easy. You can read from <a href="https://docs.djangoproject.com/en/5.0/ref/request-response/#django.http.HttpRequest.COOKIES">the <code>request.COOKIES</code> dictionary</a> to get a cookie, and …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/22/set-django-cookies/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Don't use Python's property2023-12-21T20:31:48-06:002023-12-21T22:32:15.969397-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-21:/weblog/2023/dec/21/dont-use-python-property/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Attributing the problem</h2>
<p>Suppose you’re writing Java and you write a class with an attribute :</p>
<div class="codehilite"><pre><span></span><code><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">MyClass</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>And then later on you realize that <code>value …</code></p>
<p><a href="https://www.b-list.org/weblog/2023/dec/21/dont-use-python-property/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Use Django's system checks2023-12-20T22:40:37-06:002023-12-21T00:41:11.511187-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-20:/weblog/2023/dec/20/django-system-check/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Check it out</h2>
<p>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 <code>django.contrib</code> — …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/20/django-system-check/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Show Python deprecation warnings2023-12-19T17:47:49-06:002023-12-19T19:48:14.297303-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-19:/weblog/2023/dec/19/show-python-deprecation-warnings/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Let this be a warning</h2>
<p>Python provides the ability to issue a <em>warning</em> as a step below raising an exception; warnings are issued by calling <a href="https://docs.python.org/3/library/warnings.html#warnings.warn">the <code>warnings.warn()</code> function</a>, which at minimum …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/19/show-python-deprecation-warnings/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Running async tests in Python2023-12-18T23:18:13-06:002023-12-19T01:21:34.517257-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-18:/weblog/2023/dec/18/async-python-tests/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>A-sync-ing feeling</h2>
<p><a href="/weblog/2022/aug/16/async/">Async Python</a> 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 …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/18/async-python-tests/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Don't use class methods on Django models2023-12-17T16:13:11-06:002023-12-17T18:13:34.527386-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-17:/weblog/2023/dec/17/django-model-classmethod/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Being methodical about Python</h2>
<p>Python classes support three basic types of methods:</p>
<ul>
<li><em>Instance methods</em>, which are what you get by default when writing a <code>def</code> statement inside a class body. These are …</li></ul>
<p><a href="https://www.b-list.org/weblog/2023/dec/17/django-model-classmethod/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Say what you mean in a regex2023-12-16T18:54:30-06:002023-12-16T20:55:05.637580-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-16:/weblog/2023/dec/16/python-regex-unicode-digits/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>An <span class="caps">URL</span>-y warning</h2>
<p>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 …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/16/python-regex-unicode-digits/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Python packaging: use the "src"2023-12-15T17:39:57-06:002023-12-15T19:40:18.607130-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-15:/weblog/2023/dec/15/python-packaging-src-layout/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>A lurking problem</h2>
<p>Imagine you write a Python library named, say, <code>foo</code>. And you diligently set up the configuration to package it for distribution (which is not that hard; you can …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/15/python-packaging-src-layout/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Database functions in Django2023-12-14T19:09:04-06:002023-12-14T21:09:34.565253-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-14:/weblog/2023/dec/14/django-database-functions/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Functionally a database</h2>
<p>On top of the basic query syntax we’re all used to, <span class="caps">SQL</span> databases tend to have a large number of built-in functions — some of which are standardized, some …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/14/django-database-functions/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Database views in Django2023-12-13T18:55:36-06:002023-12-13T20:56:04.063019-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-13:/weblog/2023/dec/13/django-database-views/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>A view to a database</h2>
<p>Most databases support creating and working with <em>views</em>, which, if you’ve never encountered them before, are like a virtual table — instead of being defined by …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/13/django-database-views/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Django's three types of model inheritance2023-12-12T22:32:49-06:002023-12-13T00:33:14.979680-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-12:/weblog/2023/dec/12/django-model-inheritance/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Inheritance and its discontents</h2>
<p>People can, and do, debate whether inheritance in object-oriented programming languages is a thing that ought to exist. There are even debates about what “inheritance” ought to <em>mean …</em></p>
<p><a href="https://www.b-list.org/weblog/2023/dec/12/django-model-inheritance/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Raise the right exceptions2023-12-11T19:10:55-06:002023-12-11T21:11:22.529363-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-11:/weblog/2023/dec/11/python-exceptions/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Let’s have an argument</h2>
<p>Suppose you write a function like this:</p>
<div class="codehilite"><pre><span></span><code><span class="n">def</span><span class="w"> </span><span class="n">divide</span><span class="p">(</span><span class="n">dividend</span><span class="p">,</span><span class="w"> </span><span class="n">divisor</span><span class="p">)</span><span class="o">:</span>
<span class="w"> </span><span class="s2">"</span><span class="se">""</span>
<span class="s2"> Divide ``dividend`` by ``divisor`` and return the result.</span>
<span class="s2"> </span><span class="se">""</span><span class="s2">"</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">dividend …</span></code></pre></div>
<p><a href="https://www.b-list.org/weblog/2023/dec/11/python-exceptions/">Read full entry</a></p>
https://www.b-list.org/about/copyright/Test your documentation2023-12-10T15:47:45-06:002023-12-10T17:50:55.359742-06:00James Bennetthttps://www.b-list.org/tag:www.b-list.org,2023-12-10:/weblog/2023/dec/10/python-doctest/<p>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 <a href="/weblog/2023/dec/03/python-enums/">the first post for an introduction</a>.</p>
<h2>Consider a docstring</h2>
<p>Suppose you’re writing a Python function and, as you’re supposed to do, you give it a docstring, and you even provide some examples of how the function is supposed …</p>
<p><a href="https://www.b-list.org/weblog/2023/dec/10/python-doctest/">Read full entry</a></p>
https://www.b-list.org/about/copyright/