<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <updated>2026-11-29T22:16:33.489719+00:00</updated>
  
  <author>
    <name>Alex Chan</name>
    <email>alex@alexwlchan.net</email>
  </author>
  
  <link href="https://alexwlchan.net/notes/atom.xml" rel="self" type="application/atom+xml" />
  <link href="https://alexwlchan.net/notes/" rel="alternate" type="text/html" />
  <id>https://alexwlchan.net/notes/</id>
  <title type="html">alexwlchan’s notes</title>
  <subtitle>Alex Chan’s notes</subtitle>

<entry>
  <title type="html">Royal Mail has a separate redirection service for domestic abuse</title>
  <link
    href="https://alexwlchan.net/notes/2026/royal-mail-domestic-abuse/"
    rel="alternate"
    type="text/html"
    title="Royal Mail has a separate redirection service for domestic abuse"
  />
  <published>2026-11-29T08:34:37+00:00</published>
  <updated>2026-11-29T08:34:37+00:00</updated>

  <id>https://alexwlchan.net/notes/2026/royal-mail-domestic-abuse/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/royal-mail-domestic-abuse/">
    <![CDATA[<p><strong>Using that service imposes “additional safeguards” on your data and forwarding address.</strong></p><p>I’m moving house soon, and as part of the prep work, I’m <a href="https://www.royalmail.com/personal/receiving-mail/redirection">redirecting my mail</a>.
This is a service where Royal Mail will redirect any post sent to your old address to your new address, giving you time to update everybody with your new home.</p>
<p>As part of the online setup process, Royal Mail asked if my move was related to domestic abuse.
I answered “no”, but I was curious what would happen if I said “yes”.
What’s the alternative flow?</p>
<p>Their website is vague on the topic, probably intentionally so.
From the landing page for mail redirections:</p>
<blockquote>
<p>If you’re moving home as a result of domestic abuse, please call 03457 777 888 to apply. […]</p>
<p>Do NOT apply for online or at the Post Office.</p>
<p>Please let the Customer Service Agent know you require a Sensitive Redirection. This will allow us to set up the Redirection with additional safeguards.</p>
</blockquote>
<p>I imagine that if I clicked “yes” in the online form, I’d have been blocked from continuing my online application, and directed to call this number.</p>
<p>The charity Surviving Economic Abuse has a detailed page about <a href="https://survivingeconomicabuse.org/i-need-help/de-linking-from-the-abuser/royal-mail-post/">economic abuse and mail</a>.
It explains that using the sensitive redirection service means Royal Mail won’t send a confirmation letter to your old address, so the abuser won’t be alerted that somebody set up a redirect.
I hope/imagine it also creates additional safeguards on the Royal Mail side, so the customer file would need special permissions to access.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/royal-mail-domestic-abuse/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="The world around us" />

    <summary type="html">Using that service imposes &#34;additional safeguards&#34; on your data and forwarding address.</summary>
</entry><entry>
  <title type="html">Using bzCLI to exclude directories from Backblaze</title>
  <link
    href="https://alexwlchan.net/notes/2026/exclude-backblaze-dirs/"
    rel="alternate"
    type="text/html"
    title="Using bzCLI to exclude directories from Backblaze"
  />
  <published>2026-11-18T21:38:04+00:00</published>
  <updated>2026-11-18T21:38:04+00:00</updated>

  <id>https://alexwlchan.net/notes/2026/exclude-backblaze-dirs/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/exclude-backblaze-dirs/">
    <![CDATA[<p><strong>Create a JSON file with the <code>bzdirfilter_add</code> setting, then pass that to <code>bzcli</code>.</strong></p><p>This is useful if you want to programmatically define exclusions, and seems better supported than my previous approach of manually editing Backblaze’s XML config files.
There are two steps:</p>
<ol>
<li><p>Create a JSON file with a path to the directory you want to exclude, like:</p>
<pre class="lng-json"><code><span class="c1">// exclude.json</span>
<span class="p">{</span>
  "settings"<span class="p">:</span> <span class="p">{</span>
    "bzdirfilter_add"<span class="p">:</span> <span class="p">[</span>
      <span class="p">{</span>
        "dir"<span class="p">:</span> <span class="s2">"/Users/alexwlchan/directory/to/exclude/"</span><span class="p">,</span>
        "whichfiles"<span class="p">:</span> <span class="s2">"none"</span>
      <span class="p">}</span>
    <span class="p">]</span>
  <span class="p">}</span>
<span class="p">}</span></code></pre>
</li>
<li><p>Run the <code>bzcli</code> command:</p>
<pre class="lng-console"><code><span class="gp">$</span><span class="w"> </span>bzcli<span class="w"> </span>configure<span class="w"> </span>-j<span class="w"> </span>exclude.json</code></pre>
<p>The excluded directory should appear in the list of exclusions in the GUI.</p>
</li>
</ol>
<p>The <code>bzcli</code> command is bundled in the Backblaze app; see <a href="https://www.backblaze.com/computer-backup/docs/how-to-use-the-bzcli">Backblaze’s documentation</a> for details.
It was introduced <a href="https://www.backblaze.com/blog/advanced-installer-and-backblaze-command-line-interface-cli-more-control-for-it/">in February</a>, so you may need to update the app to get it.</p>
<p>I looked for this command after finding that Backblaze was <a href="https://alexwlchan.net/notes/2026/backblaze-churn/">spending a lot of time</a> backing up my Python virtual environments, which are ephemeral and not worth saving.
I could easily recreate them on demand if necessary, and I’d never think to restore them from backup.
(See also: the <code>target</code> directory in Rust, or <code>node_modules</code> in JavaScript.)</p>
<p>I have a fish function that I use to create virtual environments, and I’ve <a href="https://alexwlchan.net/projects/scripts/commits/a233bacd563f527b916bd9488914350d6dcf473d/">updated it</a> to automatically exclude them from Backblaze backups.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/exclude-backblaze-dirs/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">Create a JSON file with the `bzdirfilter_add` setting, then pass that to `bzcli`.</summary>
</entry><entry>
  <title type="html">Finding the target of an HTTP redirect with the Python standard library</title>
  <link
    href="https://alexwlchan.net/notes/2026/check-url-redirects/"
    rel="alternate"
    type="text/html"
    title="Finding the target of an HTTP redirect with the Python standard library"
  />
  <published>2026-11-17T07:54:57+00:00</published>
  <updated>2026-11-17T07:54:57+00:00</updated>

  <id>https://alexwlchan.net/notes/2026/check-url-redirects/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/check-url-redirects/">
    <![CDATA[<p><strong>Create an <code>HTTPRedirectHandler</code> that doesn’t follow redirects, then inspect the headers of the “error” you receive from a redirect.</strong></p><p>I use the <a href="https://alexwlchan.net/2026/python-http-with-the-stdlib/">Python standard library</a> to do HTTP requests rather than relying on third-party packages.
One gap in my toolbox was the ability to inspect the redirect location of a request.
I have a lot of redirects on this site, and I want tests to check that they’re working correctly.</p>
<p>I’ve now written a function that fetches a URL, checks it got an HTTP 3xx redirect, and returns the URL it redirects to.
Here are the key steps:</p>
<ol>
<li>Define an <a href="https://docs.python.org/3/library/urllib.request.html#httpredirecthandler-objects"><code>HTTPRedirectHandler</code></a> that overrides the <code>redirect_request</code> method to always return <code>None</code>, so redirects are never followed.</li>
<li>Pass that handler to <a href="https://docs.python.org/3/library/urllib.request.html#urllib.request.build_opener"><code>urllib.request.build_opener</code></a>.</li>
<li>Open the target URL.
This function throws an <code>HTTPError</code> if it gets a non-2xx response, which we can catch and inspect.</li>
<li>An <code>HTTPError</code> can also be treated as an HTTP response, so we check it has the desired 3xx status code and look for the HTTP <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Location">Location header</a>.
(We have to close this, or we’ll have a dangling open file.)</li>
</ol>
<p>Here’s the complete function:</p>
<pre class="lng-python"><code><span class="kn">from</span> <span class="n">contextlib</span> <span class="kn">import</span> <span class="n">closing</span>
<span class="kn">from</span> <span class="n">email</span><span class="p">.</span><span class="n">message</span> <span class="kn">import</span> <span class="n">Message</span>
<span class="kn">import</span> <span class="n">ssl</span>
<span class="kn">from</span> <span class="n">typing</span> <span class="kn">import</span> <span class="n">IO</span>
<span class="kn">from</span> <span class="n">urllib</span><span class="p">.</span><span class="n">error</span> <span class="kn">import</span> <span class="n">HTTPError</span>
<span class="kn">import</span> <span class="n">urllib</span><span class="p">.</span><span class="n">request</span>
<span class="kn">from</span> <span class="n">urllib</span><span class="p">.</span><span class="n">request</span> <span class="kn">import</span> <span class="n">HTTPRedirectHandler</span><span class="p">,</span> <span class="n">HTTPSHandler</span><span class="p">,</span> <span class="n">Request</span>

<span class="kn">import</span> <span class="n">certifi</span>


<span class="k">def</span> <span class="n">get_redirect_target</span><span class="p">(</span><span class="n">url</span><span class="p">:</span> str<span class="p">)</span> <span class="o">-&gt;</span> str<span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    Fetch a URL, and return the URL it redirects to.</span>

<span class="sd">    This throws an error if the URL does not respond with a response</span>
<span class="sd">    that includes a Location header.</span>
<span class="sd">    """</span>

    <span class="k">class</span> <span class="n">NoRedirectHandler</span><span class="p">(</span>HTTPRedirectHandler<span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        A redirect handler that never follows redirects.</span>
<span class="sd">        """</span>

        <span class="k">def</span> <span class="n">redirect_request</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            req<span class="p">:</span> Request<span class="p">,</span>
            fp<span class="p">:</span> IO<span class="p">[</span>bytes<span class="p">],</span>
            code<span class="p">:</span> int<span class="p">,</span>
            msg<span class="p">:</span> str<span class="p">,</span>
            headers<span class="p">:</span> Message<span class="p">,</span>
            newurl<span class="p">:</span> str<span class="p">,</span>
        <span class="p">)</span> <span class="o">-&gt;</span> urllib<span class="o">.</span>request<span class="o">.</span>Request <span class="o">|</span> <span class="kc">None</span><span class="p">:</span>
            <span class="sd">"""</span>
<span class="sd">            Return None so we don't follow the redirect.</span>
<span class="sd">            """</span>
            <span class="k">return</span> <span class="kc">None</span>

    <span class="n">https_handler</span> <span class="o">=</span> HTTPSHandler<span class="p">(</span>
        context<span class="o">=</span>ssl<span class="o">.</span>create_default_context<span class="p">(</span>cafile<span class="o">=</span>certifi<span class="o">.</span>where<span class="p">())</span>
    <span class="p">)</span>

    <span class="n">opener</span> <span class="o">=</span> urllib<span class="o">.</span>request<span class="o">.</span>build_opener<span class="p">(</span>NoRedirectHandler<span class="p">,</span> https_handler<span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">with</span> opener<span class="o">.</span>open<span class="p">(</span>url<span class="p">):</span>
            <span class="k">raise</span> RuntimeError<span class="p">(</span><span class="s2">"url did not respond with a redirect"</span><span class="p">)</span>
    <span class="k">except</span> HTTPError <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
        <span class="k">with</span> closing<span class="p">(</span>err<span class="p">):</span>
            <span class="k">if</span> <span class="mi">300</span> <span class="o">&lt;=</span> err<span class="o">.</span>code <span class="o">&lt;</span> <span class="mi">400</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">return</span> err<span class="o">.</span>headers<span class="p">[</span><span class="s2">"location"</span><span class="p">]</span>
                <span class="k">except</span> KeyError<span class="p">:</span>
                    <span class="k">raise</span> RuntimeError<span class="p">(</span><span class="s2">"url did not respond with a Location header"</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span>


<span class="k">def</span> <span class="n">test_redirects_from_http_to_https</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    Check my website redirects from http to https.</span>
<span class="sd">    """</span>
    <span class="k">assert</span> get_redirect_target<span class="p">(</span><span class="s2">"http://alexwlchan.net/"</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"https://alexwlchan.net/"</span></code></pre>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/check-url-redirects/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Python" />

    <summary type="html">Create an `HTTPRedirectHandler` that doesn&#39;t follow redirects, then inspect the headers of the &#34;error&#34; you receive from a redirect.</summary>
</entry><entry>
  <title type="html">Remember to use “Charge to Full Now” before going out</title>
  <link
    href="https://alexwlchan.net/notes/2026/battery-full/"
    rel="alternate"
    type="text/html"
    title="Remember to use &#34;Charge to Full Now&#34; before going out"
  />
  <published>2026-11-01T21:38:39+00:00</published>
  <updated>2026-11-01T21:38:39+00:00</updated>

  <id>https://alexwlchan.net/notes/2026/battery-full/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/battery-full/">
    <![CDATA[<p><strong>On the default settings, modern Macs will only charge to 80% unless you tell them otherwise.</strong></p><p>To preserve long-term battery health, modern Macs don’t always charge to 100% when you plug them in.
They’ll stop at 80% if they expect to be plugged in for a long time:</p>
<blockquote>
<p>Optimised Battery Charging is designed to reduce the wear on your battery and improve its lifespan by reducing the time that your Mac spends fully charged. It uses on-device machine learning to learn your daily charging routine so that it can delay charging past 80% in certain situations, such as when it predicts that your Mac will be connected to power for an extended period of time.</p>
</blockquote>
<p>I use my work laptop as a “desktop laptop” – it’s almost always plugged in at my desk, and day trips away from my desk are exceptional.
However good the machine learning algorithm is, it can’t know that this particular Tuesday is when I’m leaving the house.</p>
<p>If I know I’m going out, I can explicitly tell my Mac it should charge itself to 100%:</p>
<blockquote>
<p>When charging past 80% is delayed by this feature, “Charging On Hold” appears in the battery status menu. If you need your Mac to be charged to 100% sooner, choose “Charge to Full Now” from the menu.</p>
</blockquote>
<p>Charging a battery goes faster when it’s mostly empty than when it’s mostly full, so if this is important, it’s worth doing the night before the trip, to get the most out of that extra capacity.</p>
<p>(Quotes from Apple Support doc <a href="https://support.apple.com/en-gb/102338">About Optimised Battery Charging and Charge Limit on Mac
</a>)</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/battery-full/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="macOS" />

    <summary type="html">On the default settings, modern Macs will only charge to 80% unless you tell them otherwise.</summary>
</entry><entry>
  <title type="html">What is WS11 1DB?</title>
  <link
    href="https://alexwlchan.net/notes/2026/stripe-post-code/"
    rel="alternate"
    type="text/html"
    title="What is WS11 1DB?"
  />
  <published>2026-09-08T22:13:17+01:00</published>
  <updated>2026-09-08T22:13:17+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/stripe-post-code/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/stripe-post-code/">
    <![CDATA[<p><strong>Stripe’s placeholder postcode is an industrial park just north of Birmingham.</strong></p><p>I was filling in my credit card details on a website this evening:</p>
<p><img alt="A credit card details form. There’s a field for card number (with placeholder 1234 1234 1234 1234), postal code (WS11 1DB) and mobile number (07400 123456)." class="screenshot" sizes="(max-width:377px)100vw,377px" src="https://alexwlchan.net/images/2026/stripe-placeholders_1x.png" srcset="/images/2026/stripe-placeholders_1x.png 377w,/images/2026/stripe-placeholders_2x.png 754w" width="377"/></p>
<p>I got curious about the placeholders.
They seem to be consistently used across instances of this form; not specific to the single instance I was seeing.</p>
<p>Two of them seem straightforward, where somebody has used sequential digits to indicate a dummy value:</p>
<ul>
<li><p><code>1234 1234 1234 1234</code> isn’t a real credit card number.
The final character of valid credit card numbers is a check digit, calculated using <a href="https://en.wikipedia.org/wiki/Luhn_algorithm">Luhn’s algorithm</a>, used to detect mistakes and typos.
This card has the wrong check digit, so we know it isn’t real.</p>
</li>
<li><p><code class="nowrap">07400 123456</code> could be a real UK phone number.
Ofcom publish <a href="https://www.ofcom.org.uk/phones-and-broadband/phone-numbers/numbering-data">numbering data</a>, and every phone number starting <code>07400</code> belongs to “Hutchison 3G UK Ltd”, more commonly known as “Three”.
They can choose which numbers to allocate within this range, and given it’s a popular placeholder in online forms, I wonder if this number is deliberately unused.
(No, I didn’t dial it to find out.)</p>
</li>
</ul>
<p>But what about the placeholder post code?
What is WS11 1DB?</p>
<p>It’s <a href="https://www.openstreetmap.org/search?query=WS11+1DB&amp;zoom=6&amp;minlon=-15.183105468750002&amp;minlat=47.84265762816538&amp;maxlon=8.327636718750002&amp;maxlat=60.930432202923335#map=16/52.68854/-2.02157">a road in Cannock</a>, just north-west of Birmingham.
I thought maybe it would be a pseudo-address, like the <a href="https://en.wikipedia.org/wiki/Corporation_Trust_Center">building in Delaware</a> which is the registered office of thousands of businesses – or perhaps the address of a Stripe office – but no, it looks like it’s just a regular postcode.</p>
<p>You can find local businesses that list it as their address, like <a href="https://www.directspecs.co.uk/pages/terms-conditions#:~:text=WS11%201DB">an optician</a>, <a href="https://cannockcid.co.uk/#:~:text=WS11%201DB">a ceiling insulation firm</a>, and <a href="https://www.metalline.co.uk/#:~:text=WS11%201DB">a metal fabricator</a> – nothing to do with online payments.
(Most of the businesses at the postcode don’t even have an online presence except Companies House and listing aggregators.)</p>
<p>Occasionally the postcode appears in <a href="https://www.pearlbay.dev/open-gardens-test/">other</a> <a href="https://bookhamresidents.org.uk/site_files/9767/upload_files/BRA%20June%202025%20newsletter%20-%20web%20version(1).pdf?dl=1">places</a>, but it’s only instructing people how to use a form.
“Fill in your real postcode, not WS11 1DB.”</p>
<p>I can’t find any information about why this was chosen as the placeholder, and now I’m intensely curious.
Does Stripe have a secret link to Cannock?
Does this code provide some useful UI property?
Was it A/B tested and found to be easily recognised as a placeholder?</p>
<p>If you know what’s going on, please <a href="https://alexwlchan.net/contact/">get in touch</a>.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/stripe-post-code/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="The world around us" />

    <summary type="html">Stripe&#39;s placeholder postcode is an industrial park just north of Birmingham.</summary>
</entry><entry>
  <title type="html">Blocking referrers with Caddy</title>
  <link
    href="https://alexwlchan.net/notes/2026/blocking-hacker-news/"
    rel="alternate"
    type="text/html"
    title="Blocking referrers with Caddy"
  />
  <published>2026-09-02T22:27:35+01:00</published>
  <updated>2026-09-02T22:27:35+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/blocking-hacker-news/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/blocking-hacker-news/">
    <![CDATA[<p><strong>Create a matcher on the <code>Referer</code> header, followed by a hard-coded <code>respond</code> directive.</strong></p><p>Every so often, somebody tells me that I’ve hit the front page of <a href="https://en.wikipedia.org/wiki/Hacker_news">Hacker News</a>.
They always imagine I’ll be excited, but it just fills me with a sense of dread.
Orange site commenters are a stain on my inbox: arrogant, rude, and condescending.
I also get misgendered on the regular, a combination of ignorance and transphobia.
Fun!</p>
<p>I’ve finally done what I should have done years ago: block all requests from Hacker News.</p>
<p>The block works by looking for <code>news.ycombinator.com</code> in the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referer">Referer header</a>.
This is trivial to circumvent, but most commenters will give up if a page doesn’t load on the first click.
Even if some find their way to the site, it’ll tank the number of upvotes and minimise the number of readers who see the link.</p>
<p>Everybody wins: Hacker News commenters don’t have to read posts which are <em>so obviously beneath them</em>, and I don’t have to deal with their bile.</p>
<h2 id="how-it-works">How it works</h2>
<p>This is straightforward with my web server, which is Caddy.
I define a <a href="https://caddyserver.com/docs/caddyfile/matchers#header">request matcher</a> which checks the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referer">Referer header</a>, then use a <a href="https://caddyserver.com/docs/caddyfile/directives/respond"><code>respond</code> directive</a> to send a hard-coded error:</p>
<pre class="lng-caddy"><code><span class="n">alexwlchan.net</span> <span class="p">{</span>
	@hacker_news <span class="p">{</span>
		header Referer *ycombinator.com*
	<span class="p">}</span>
	respond @hacker_news "Forbidden" 403

	root * /home/alexwlchan/sites/alexwlchan.net/_out
	file_server
<span class="p">}</span></code></pre><p>Initially I used Caddy’s <a href="https://caddyserver.com/docs/caddyfile/directives/abort"><code>abort</code> directive</a>, which terminates the entire connection, but that made it a bit too easy to evade the block.
If the server drops the connection and the browser reloads, the browser discards the previous value of the <code>Referer</code> header and gets a successful response.
But if the server returns an error response and the browser reloads, it sends the same request and gets the same error.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/blocking-hacker-news/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Caddy" />

    <summary type="html">Create a matcher on the `Referer` header, followed by a hard-coded `respond` directive.</summary>
</entry><entry>
  <title type="html">How to type a Spanish question mark (¿) on a Mac</title>
  <link
    href="https://alexwlchan.net/notes/2026/spanish-question-mark/"
    rel="alternate"
    type="text/html"
    title="How to type a Spanish question mark (¿) on a Mac"
  />
  <published>2026-08-31T09:54:13+01:00</published>
  <updated>2026-08-31T09:54:13+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/spanish-question-mark/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/spanish-question-mark/">
    <![CDATA[<p><strong><code>Shift + Option + ?</code></strong></p><p>This question prompted by a suggestion from Merlin Mann’s <a href="https://github.com/merlinmann/wisdom/tree/master">Wisdom Project</a>:</p>
<blockquote>
<p>If you’re only tentatively committed to a calendar item—especially if the time of the event has not been mutually confirmed—title the event using Spanish-language questions marks. A future event like “¿Pick apples with Aunt Sue?” successfully blocks out the time while also affording a quickly scannable reminder about events that still need to either be formalized or deleted.</p>
</blockquote>
<p>I like this idea, but to use it I need a fast way to type an upside-down question mark.
I used the <a href="https://support.apple.com/en-in/guide/mac-help/mchlp1015/mac">Keyboard Viewer</a> to experiment with modifier keys until I found this combination.</p>
<p>This is likely specific to my British English keyboard layout; I imagine Spanish keyboards have a different layout that make this more convenient.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/spanish-question-mark/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">`Shift + Option + ?`</summary>
</entry><entry>
  <title type="html">Non-overlapping type comparisons and Python type checkers</title>
  <link
    href="https://alexwlchan.net/notes/2026/non-overlapping-types/"
    rel="alternate"
    type="text/html"
    title="Non-overlapping type comparisons and Python type checkers"
  />
  <published>2026-08-27T19:27:27+01:00</published>
  <updated>2026-08-27T19:27:27+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/non-overlapping-types/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/non-overlapping-types/">
    <![CDATA[<p><strong>Why do type checkers allow you to check if a <code>str</code> is a member of a list of <code>int</code>?</strong></p><p>I made a mistake in Python today where I checked if a collection of one type contained an element of a different type.
Here’s a minimal example:</p>
<pre class="lng-python"><code><span class="n">numbers</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>

print<span class="p">(</span><span class="s2">"one"</span> <span class="ow">in</span> numbers<span class="p">)</span>  <span class="c1"># False</span></code></pre>
<p>This is trivially false, right?
A list of integers can’t contain a string, and I was surprised my type checker (<a href="https://github.com/astral-sh/ty">ty</a>) didn’t warn me.
Then I did some reading, and I realised this isn’t quite as trivial as I thought.</p>
<p>When you use <a href="https://docs.python.org/3/reference/expressions.html#membership-test-details">the <code>in</code> keyword</a>, you go through several magic methods :</p>
<ul>
<li>For <code>item in collection</code>, Python calls <a href="https://docs.python.org/3/reference/datamodel.html#object.__contains__"><code>__contains__(self, item)</code></a> on the collection.</li>
<li>If an object doesn’t define <code>__contains__</code> but does define <code>__iter__</code>, Python iterates through the collection and looks for an element <code>x</code> where <code class="nowrap">x is item</code> or <code class="nowrap">x == item</code> is <code>True</code>.</li>
<li>When you call <code class="nowrap">x == y</code>, Python calls <a href="https://docs.python.org/3/reference/datamodel.html#object.__eq__"><code class="nowrap">__eq__(self, y)</code></a> on <code>x</code>.</li>
</ul>
<p>The types <code>int</code> and <code>str</code> include their subclasses, which can override these magic methods.
Usually they do the “obvious” thing and cross-type comparisons will report <code>False</code> – but it’s theoretically possible.</p>
<p>Cross-type comparisons are <em>usually</em> a mistake, and some type checkers will flag it, but not by default – <a href="https://mypy.readthedocs.io/en/stable/error_code_list2.html#check-that-comparisons-are-overlapping-comparison-overlap">mypy</a>, <a href="https://github.com/microsoft/pylance-release/blob/main/docs/diagnostics/reportUnnecessaryContains.md">Pylance</a> and Pyright only flag it in strict mode, and it’s <a href="https://github.com/astral-sh/ty/issues/576">not yet supported</a> in ty.
It’s possible to write Python where this is the correct and desired behaviour, however confusing it might appear.</p>
<h2 id="counterexamples">Counterexamples</h2>
<p>I wrote a couple of simple programs where I check if a <code>str</code> is in a <code>list[int]</code> and the membership test returns <code>True</code>.
I wouldn’t write anything this confusing in a real codebase, but I found it helpful to understand these magic methods.</p>
<p>Here’s a custom collection that overrides <code>__contains__</code>:</p>
<pre class="lng-python"><code><span class="n">WORD_MAP</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"one"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">"two"</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s2">"three"</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s2">"four"</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s2">"five"</span><span class="p">:</span> <span class="mi">5</span><span class="p">}</span>


<span class="k">class</span> <span class="n">NumberList</span><span class="p">(</span>list<span class="p">):</span>
    <span class="k">def</span> <span class="n">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">:</span> object<span class="p">)</span> <span class="o">-&gt;</span> bool<span class="p">:</span>
        <span class="c1"># Check if `item` is a number (for example, `1 in numbers`)</span>
        <span class="k">if</span> isinstance<span class="p">(</span>item<span class="p">,</span> int<span class="p">)</span> <span class="ow">and</span> super<span class="p">()</span><span class="o">.</span>__contains__<span class="p">(</span>item<span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Check if `item` is a string (for example, `"one" in numbers`)</span>
        <span class="k">if</span> isinstance<span class="p">(</span>item<span class="p">,</span> str<span class="p">)</span> <span class="ow">and</span> item <span class="ow">in</span> WORD_MAP<span class="p">:</span>
            <span class="k">return</span> super<span class="p">()</span><span class="o">.</span>__contains__<span class="p">(</span>WORD_MAP<span class="p">[</span>item<span class="p">])</span>

        <span class="k">return</span> <span class="kc">False</span>


<span class="n">numbers</span><span class="p">:</span> list<span class="p">[</span>int<span class="p">]</span> <span class="o">=</span> NumberList<span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>

print<span class="p">(</span><span class="mi">1</span> <span class="ow">in</span> numbers<span class="p">)</span>      <span class="c1"># True</span>
print<span class="p">(</span><span class="s2">"one"</span> <span class="ow">in</span> numbers<span class="p">)</span>  <span class="c1"># True</span>

print<span class="p">(</span><span class="mi">6</span> <span class="ow">in</span> numbers<span class="p">)</span>      <span class="c1"># False</span>
print<span class="p">(</span><span class="s2">"six"</span> <span class="ow">in</span> numbers<span class="p">)</span>  <span class="c1"># False</span></code></pre>
<p>Here’s another approach, where I subclass <code>int</code> and override the <code>__eq__</code> method:</p>
<pre class="lng-python"><code><span class="n">NUMBER_MAP</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="s2">"one"</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s2">"two"</span><span class="p">,</span> <span class="mi">3</span><span class="p">:</span> <span class="s2">"three"</span><span class="p">,</span> <span class="mi">4</span><span class="p">:</span> <span class="s2">"four"</span><span class="p">,</span> <span class="mi">5</span><span class="p">:</span> <span class="s2">"five"</span><span class="p">}</span>


<span class="k">class</span> <span class="n">Number</span><span class="p">(</span>int<span class="p">):</span>
    <span class="k">def</span> <span class="n">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">:</span> object<span class="p">)</span> <span class="o">-&gt;</span> bool<span class="p">:</span>
        <span class="c1"># Check if `other` is a number (for example, x == 1)</span>
        <span class="k">if</span> isinstance<span class="p">(</span>other<span class="p">,</span> int<span class="p">)</span> <span class="ow">and</span> super<span class="p">()</span><span class="o">.</span>__eq__<span class="p">(</span>other<span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Check if `other` is a string (for example, x == "one")</span>
        <span class="k">for</span> numeral<span class="p">,</span> word <span class="ow">in</span> NUMBER_MAP<span class="o">.</span>items<span class="p">():</span>
            <span class="k">if</span> super<span class="p">()</span><span class="o">.</span>__eq__<span class="p">(</span>numeral<span class="p">)</span> <span class="ow">and</span> other <span class="o">==</span> word<span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>

        <span class="k">return</span> <span class="kc">False</span>


<span class="n">numbers</span> <span class="o">=</span> <span class="p">[</span>Number<span class="p">(</span><span class="mi">1</span><span class="p">),</span> Number<span class="p">(</span><span class="mi">2</span><span class="p">),</span> Number<span class="p">(</span><span class="mi">3</span><span class="p">),</span> Number<span class="p">(</span><span class="mi">4</span><span class="p">),</span> Number<span class="p">(</span><span class="mi">5</span><span class="p">)]</span>

print<span class="p">(</span><span class="mi">1</span> <span class="ow">in</span> numbers<span class="p">)</span>      <span class="c1"># True</span>
print<span class="p">(</span><span class="s2">"one"</span> <span class="ow">in</span> numbers<span class="p">)</span>  <span class="c1"># True</span>

print<span class="p">(</span><span class="mi">6</span> <span class="ow">in</span> numbers<span class="p">)</span>      <span class="c1"># False</span>
print<span class="p">(</span><span class="s2">"six"</span> <span class="ow">in</span> numbers<span class="p">)</span>  <span class="c1"># False</span></code></pre>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/non-overlapping-types/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Python" />

    <summary type="html">Why do type checkers allow you to check if a `str` is a member of a list of `int`?</summary>
</entry><entry>
  <title type="html">Why does &lt;code&gt;t.Setenv&lt;/code&gt; panic after &lt;code&gt;t.Parallel&lt;/code&gt;?</title>
  <link
    href="https://alexwlchan.net/notes/2026/serial-tests-in-go/"
    rel="alternate"
    type="text/html"
    title="Why does `t.Setenv` panic after `t.Parallel`?"
  />
  <published>2026-08-23T21:23:53+01:00</published>
  <updated>2026-08-23T21:23:53+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/serial-tests-in-go/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/serial-tests-in-go/">
    <![CDATA[<p><strong>Go’s <code>testing</code> package tracks whether a test is parallel or mutates global state, and panics if you do both in the same test.</strong></p><p>Last week, I debugged a flaky test caused by state leaking from another test.
One test called <code>os.Setenv</code> with an environment variable that affected the behaviour of the other test.
Although the first test cleaned up after itself, the two tests could run in parallel, and if they ran at the same time the second test would fail.</p>
<p>In general, Go tests should use <a href="https://pkg.go.dev/testing#T.Setenv"><code>t.Setenv</code></a> instead of <code>os.Setenv</code>.
<code>t.Setenv</code> automatically restores the original value at the end of a test, and prevents parallel execution.
Switching to <code>t.Setenv</code> was enough to fix this specific flake, but while looking for similar mistakes, I learnt more about how Go handles parallel tests.</p>
<p>In some of our tests, the <code>os.Setenv</code> call happens deep inside non-test code and it’s awkward to change, so we use a different helper to prevent leaking state:</p>
<figure class="annotated_code"><pre class="lng-go"><code><div class="ln"><span class="lineno">23</span><span class="c1">// AssertNotParallel asserts that t has not been marked as parallel.</span></div>
<div class="ln"><span class="lineno">24</span><span class="c1">// It panics (via t.Setenv) if t.Parallel has already been called.</span></div>
<div class="ln"><span class="lineno">25</span><span class="c1">//</span></div>
<div class="ln"><span class="lineno">26</span><span class="c1">// Use this when a test modifies package-level globals or other shared</span></div>
<div class="ln"><span class="lineno">27</span><span class="c1">// state that would be unsafe to modify concurrently with other tests.</span></div>
<div class="ln"><span class="lineno">28</span><span class="kd">func</span> <span class="n">AssertNotParallel</span><span class="p">(</span><span class="n">t</span> testenv<span class="p">.</span>TB<span class="p">)</span> <span class="p">{</span></div>
<div class="ln"><span class="lineno">29</span>	t<span class="p">.</span>Helper<span class="p">()</span></div>
<div class="ln"><span class="lineno">30</span>	t<span class="p">.</span>Setenv<span class="p">(</span><span class="s">"ASSERT_NOT_PARALLEL_TEST"</span><span class="p">,</span> <span class="s">"1"</span><span class="p">)</span> <span class="c1">// panics if t.Parallel was called</span></div>
<div class="ln"><span class="lineno">31</span><span class="p">}</span></div></code></pre>
<figcaption>Lines 23–31 of <a href="https://github.com/tailscale/tailscale/blob/ed7d521caedb5c445db9641461169d6023333a27/tstest/tstest.go#L23-L31"><code>tstest/tstest.go</code></a> in the <a href="https://github.com/tailscale/tailscale/">tailscale/tailscale</a> repo. Copyright Tailscale Inc &amp; contributors, used under the BSD-3-Clause license. Here <code>testenv.TB</code> is a copy of <a href="https://pkg.go.dev/testing#TB"><code>testing.TB</code></a>, but without a <code>testing</code> dependency so it can be used in non-test code.</figcaption></figure><p>I was intrigued by the comment: why does <code>t.Setenv</code> panic if <a href="https://pkg.go.dev/testing#T.Parallel"><code>t.Parallel</code></a> has already been called?</p>
<p>Reading the <a href="https://github.com/golang/go/blob/c97cfcb37fced87a43a3dbab8983d6f76b8b84d1/src/testing/testing.go#L770">source code for the <code>testing</code> package</a> makes this clear.
The <code>testing.T</code> struct tracks parallel status with two private fields:</p>
<ul>
<li><code>isParallel</code> – set when you call <code>t.Parallel()</code>.</li>
<li><code>denyParallel</code> – set when calling operations that mutate global state, specifically <code>t.Chdir</code> and <code>t.Setenv</code>.</li>
</ul>
<p>These fields are mutually exclusive.</p>
<p>The functions <a href="https://github.com/golang/go/blob/c97cfcb37fced87a43a3dbab8983d6f76b8b84d1/src/testing/testing.go#L1914-L1982"><code>t.Parallel</code></a>, <a href="https://github.com/golang/go/blob/c97cfcb37fced87a43a3dbab8983d6f76b8b84d1/src/testing/testing.go#L2006-L2015"><code>t.Setenv</code></a> and <a href="https://github.com/golang/go/blob/c97cfcb37fced87a43a3dbab8983d6f76b8b84d1/src/testing/testing.go#L2017-L2026"><code>t.Chdir</code></a> inspect both flags, and panic if you try to use both in a single test:</p>
<pre class="lng-go"><code><span class="kn">package</span> <span class="n">main</span>

<span class="kn">import</span> <span class="p">(</span>
	<span class="s">"testing"</span>
<span class="p">)</span>

<span class="kd">func</span> <span class="n">TestCannotSetenvAfterParallel</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span>testing<span class="p">.</span>T<span class="p">)</span> <span class="p">{</span>
	t<span class="p">.</span>Parallel<span class="p">()</span>
	t<span class="p">.</span>Setenv<span class="p">(</span><span class="s">"COLOUR"</span><span class="p">,</span> <span class="s">"red"</span><span class="p">)</span>
	<span class="c1">// panics: "test using t.Setenv or t.Chdir can not use t.Parallel"</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="n">TestCannotParallelAfterSetenv</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span>testing<span class="p">.</span>T<span class="p">)</span> <span class="p">{</span>
	t<span class="p">.</span>Setenv<span class="p">(</span><span class="s">"COLOUR"</span><span class="p">,</span> <span class="s">"red"</span><span class="p">)</span>
	t<span class="p">.</span>Parallel<span class="p">()</span>
	<span class="c1">// panics: "test using t.Setenv or t.Chdir can not use t.Parallel"</span>
<span class="p">}</span></code></pre>
<p>Now I understand how <code>AssertNotParallel</code> works.
It triggers <code>t.Setenv</code>, which inspects <code>isParallel</code> and panics if the test is marked parallel.
The protection is also stronger than the doc comment implies – calling <code>AssertNotParallel</code> and <code>t.Setenv</code> sets <code>denyParallel</code>, which blocks any <em>subsequent</em> calls to <code>t.Parallel()</code>.</p>
<p>There’s currently <a href="https://github.com/golang/go/issues/78614">an open proposal</a> to add an explicit <code>t.Serial</code> method to the <code>testing</code> package, and I hope it’s accepted.
Mutating dummy environment variables is a hack that works, but a dedicated <code>t.Serial</code> method is a much clearer statement of intent.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/serial-tests-in-go/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Go" />
    <category term="Software testing" />

    <summary type="html">Go&#39;s `testing` package tracks whether a test is parallel or mutates global state, and panics if you do both in the same test.</summary>
</entry><entry>
  <title type="html">Use &lt;code&gt;Path.glob()&lt;/code&gt; and &lt;code&gt;Path.rglob()&lt;/code&gt; for typed versions of &lt;code&gt;glob.glob()&lt;/code&gt;</title>
  <link
    href="https://alexwlchan.net/notes/2026/path-glob/"
    rel="alternate"
    type="text/html"
    title="Use `Path.glob()` and `Path.rglob()` for typed versions of `glob.glob()`"
  />
  <published>2026-08-17T21:49:17+01:00</published>
  <updated>2026-08-17T21:49:17+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/path-glob/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/path-glob/">
    <![CDATA[<p><strong>These functions let you search a directory or recurse into it, and they return <code>Path</code> objects.</strong></p><p>Recently I’ve been getting a lot of use out of the <a href="https://docs.python.org/3/library/glob.html#module-glob"><code>glob</code> module</a>, for finding files that match a particular pattern:</p>
<pre class="lng-python"><code><span class="kn">from</span> <span class="n">glob</span> <span class="kn">import</span> <span class="n">glob</span>

<span class="n">top_level_html_files</span> <span class="o">=</span> glob<span class="p">(</span><span class="s2">"*.html"</span><span class="p">)</span>
<span class="n">all_html_files</span> <span class="o">=</span> glob<span class="p">(</span><span class="s2">"**/*.html"</span><span class="p">,</span> recursive<span class="o">=</span><span class="kc">True</span><span class="p">)</span></code></pre>
<p>But these functions return strings, and I’ve been trying to use <a href="https://docs.python.org/3/library/pathlib.html#module-pathlib"><code>Path</code> objects</a> as much as possible.</p>
<p>It turns out the <code>pathlib</code> module has <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob"><code>glob()</code></a> and <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob"><code>rglob()</code></a> functions that do sometrhing very similar, but they return <code>Path</code> objects:</p>
<pre class="lng-python"><code><span class="kn">from</span> <span class="n">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>

<span class="n">top_level_html_files</span> <span class="o">=</span> Path<span class="o">.</span>cwd<span class="p">()</span><span class="o">.</span>glob<span class="p">(</span><span class="s2">"*.html"</span><span class="p">)</span>
<span class="n">all_html_files</span> <span class="o">=</span> Path<span class="o">.</span>cwd<span class="p">()</span><span class="o">.</span>rglob<span class="p">(</span><span class="s2">"*.html"</span><span class="p">)</span></code></pre>
<p>This isn’t new – indeed, it’s mentioned in <a href="https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/">Trey Hunner’s 2018 blog post</a> that persuaded me to start using <code>pathlib</code> – but somehow I’d forgotten about it.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/path-glob/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Python" />

    <summary type="html">These functions let you search a directory or recurse into it, and they return `Path` objects.</summary>
</entry><entry>
  <title type="html">Curious clocks and colourful eyes</title>
  <link
    href="https://alexwlchan.net/notes/2026/clocks-and-eyes/"
    rel="alternate"
    type="text/html"
    title="Curious clocks and colourful eyes"
  />
  <published>2026-08-11T00:02:05+01:00</published>
  <updated>2026-08-11T00:02:05+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/clocks-and-eyes/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/clocks-and-eyes/">
    <![CDATA[<p><strong>There’s a cave-dwelling fish that doesn’t follow the light of the sun, and a reindeer that changes eye colour to find lichen.</strong></p><p>Via Tumblr, the Wikipedia article for the cave-dwelling fish <a href="https://en.wikipedia.org/wiki/Garra_andruzzii"><em>Garra andruzzii</em></a> contains a fascinating fact:</p>
<blockquote>
<p>This fish is the first animal discovered that does not adjust its biological clock with the light of the sun. It has an unusual internal clock, which measures the passage of time with an extremely long period (up to 47 hours). It is also completely blind to all light stimuli.</p>
</blockquote>
<p>The phrase “first animal discovered” suggests that there are other animals with a biological clock that isn’t based on sunlight – that includes other cave-dwelling creatures, but it also includes the <a href="https://polarjournal.net/observation-rest-and-digestion-reindeer-secrets-to-last/">Arctic reindeer</a>, who spend summer in perpetual light.</p>
<h2 id="the-eyes-don-t-have-it">The eyes don’t have it</h2>
<p>The references point to an <a href="https://web.archive.org/web/20111007190530/http://www.lswn.it/biologia/articoli/orologi_biologici_in_pesce_cieco_abitante_grotte_sotto_deserto_somalo">Italian journal article</a>.
I couldn’t find a copy of the article text, but a summary page explains (via Google Translate) that the blind eyes and blind clocks aren’t related:</p>
<blockquote>
<p>La perdita degli occhi non è tuttavia responsabile della cecità dell’orologio stesso. Le responsabili del fenotipo cieco dell’orologio in questo pesce sotterraneo sono le mutazioni a carico di due fotorecettori ampiamente espressi (le opsine).</p>
<p><em>The loss of eyes is not the cause of the clock’s own “blindness.” The factors responsible for the clock’s blind phenotype in this subterranean fish are mutations affecting two widely expressed photoreceptors (opsins).</em></p>
</blockquote>
<p>It makes sense to me that lacking eyes doesn’t mean a creature is disconnected from a 24-hour clock.
Plants don’t have eyes, but they do have <a href="https://www.botanic.cam.ac.uk/learning/trails/plant-rhythms/">internal rhythms</a>.
They’re still photosensitive and they adapt their processes to a 24-hour cycle.</p>
<p>I was also reminded of <a href="https://en.wikipedia.org/wiki/Olm">olms</a>.
When I visited Slovenia in 2023, I visited a vivarium in Postojna where they’re housed.
These salamander-like creatures are adapted to complete darkness and they don’t have eyes, but there was no mention of an atypical circadiam rhythm.
(A casual search suggests that they don’t have any <a href="https://cdnsciencepub.com/doi/10.1139/z00-084">daily rhythm</a>, rather than one of unusual length.)</p>
<h2 id="eyes-of-gold-and-eyes-of-blue">Eyes of gold and eyes of blue</h2>
<p>At the other end of the optical spectrum, the article about <a href="https://polarjournal.net/observation-rest-and-digestion-reindeer-secrets-to-last/">reindeer circadian rhythms</a> contains another remarkable fact:</p>
<blockquote>
<p>In winter, food is scarce and […] the reindeer’s golden eyes turn bright blue. “If the colour of light in the environment is predominantly blue, it makes sense for the eye to accentuate the blue colour to ensure that the reindeer’s photoreceptors maximise these wavelengths,” explains Nathaniel Dominy, an evolutionary biologist at Dartmouth University and first author of a study published in the review <em>i-Perception</em>.</p>
</blockquote>
<p>The article explains that certain lichens emit ultraviolet light, and turning their eyes blue allows reindeer to pick out the lichen from the snow.</p>
<p>Eyes are something we all take for granted, but if they didn’t exist, you’d only find them in the weirdest of sci-fi novels.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/clocks-and-eyes/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="The world around us" />

    <summary type="html">There&#39;s a cave-dwelling fish that doesn&#39;t follow the light of the sun, and a reindeer that changes eye colour to find lichen.</summary>
</entry><entry>
  <title type="html">Track which templates are used by Jinja2</title>
  <link
    href="https://alexwlchan.net/notes/2026/jinja2-dependency-tracking/"
    rel="alternate"
    type="text/html"
    title="Track which templates are used by Jinja2"
  />
  <published>2026-08-10T03:07:37+01:00</published>
  <updated>2026-08-10T03:07:37+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/jinja2-dependency-tracking/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/jinja2-dependency-tracking/">
    <![CDATA[<p><strong>Override the <code>get_template</code> method on the <code>Environment</code> and see which templates are summoned.</strong></p><p>I write my own static site generator, and I do incremental rebuilds <a href="https://alexwlchan.net/2026/watch-files-on-macos/#the-result">when a source file changes</a>.
Currently I rebuild the entire website whenever a template changes, which is a lot of redundant work.</p>
<p>I thought it might be useful to track which templates are used by each page, and only rebuild a page if it uses the changed template.
I use <a href="https://jinja.palletsprojects.com/en/stable/">Jinja</a> for templating, and I wrote this script to work out where I should intercept template calls:</p>
<pre class="lng-python"><code><span class="kn">from</span> <span class="n">jinja2</span> <span class="kn">import</span> <span class="n">DictLoader</span><span class="p">,</span> <span class="n">Environment</span>


<span class="k">class</span> <span class="n">PrintingDictLoader</span><span class="p">(</span>DictLoader<span class="p">):</span>
    <span class="k">def</span> <span class="n">get_source</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">env</span><span class="p">:</span> Environment<span class="p">,</span> <span class="n">template</span><span class="p">:</span> str<span class="p">):</span>
        print<span class="p">(</span><span class="sa">f</span><span class="s2">"Loader.get_source(</span><span class="si">{</span>template<span class="si">!r}</span><span class="s2">)"</span><span class="p">)</span>
        <span class="k">return</span> super<span class="p">()</span><span class="o">.</span>get_source<span class="p">(</span>env<span class="p">,</span> template<span class="p">)</span>


<span class="k">class</span> <span class="n">PrintingEnvironment</span><span class="p">(</span>Environment<span class="p">):</span>
    <span class="k">def</span> <span class="n">get_template</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> str<span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        print<span class="p">(</span><span class="sa">f</span><span class="s2">"Environment.get_template(</span><span class="si">{</span>name<span class="si">!r}</span><span class="s2">)"</span><span class="p">)</span>
        <span class="k">return</span> super<span class="p">()</span><span class="o">.</span>get_template<span class="p">(</span>name<span class="p">,</span> <span class="o">*</span>args<span class="p">,</span> <span class="o">**</span>kwargs<span class="p">)</span>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
    <span class="n">loader</span> <span class="o">=</span> PrintingDictLoader<span class="p">(</span>
        <span class="p">{</span>
            <span class="s2">"base.html"</span><span class="p">:</span> <span class="p">(</span>
                <span class="s2">"This is the base template"</span>
                <span class="s2">"{% block content %}{</span><span class="si">% e</span><span class="s2">ndblock %}"</span>
            <span class="p">),</span>
            <span class="s2">"article.html"</span><span class="p">:</span> <span class="p">(</span>
                <span class="s1">'{</span><span class="si">% e</span><span class="s1">xtends "base.html" %}'</span>
                <span class="s2">"{% block content %}"</span>
                <span class="s2">"This is article {{ title }}"</span>
                <span class="s2">"{</span><span class="si">% e</span><span class="s2">ndblock %}"</span>
            <span class="p">),</span>
        <span class="p">}</span>
    <span class="p">)</span>
    <span class="n">env</span> <span class="o">=</span> PrintingEnvironment<span class="p">(</span>loader<span class="o">=</span>loader<span class="p">)</span>

    env<span class="o">.</span>get_template<span class="p">(</span><span class="s2">"article.html"</span><span class="p">)</span><span class="o">.</span>render<span class="p">(</span>title<span class="o">=</span><span class="s2">"My first article"</span><span class="p">)</span>
    print<span class="p">(</span><span class="s2">"---"</span><span class="p">)</span>
    env<span class="o">.</span>get_template<span class="p">(</span><span class="s2">"article.html"</span><span class="p">)</span><span class="o">.</span>render<span class="p">(</span>title<span class="o">=</span><span class="s2">"My second article"</span><span class="p">)</span></code></pre>
<p>Here’s the output.
We can see the <code>get_template</code> method is called on both renders, but the loader method is only called once because the loaded template gets cached:</p>
<pre class="lng-console"><code><span class="gp">$</span><span class="w"> </span>python3<span class="w"> </span>print_templates.py
<span class="go">Environment.get_template('article.html')</span>
<span class="go">Loader.get_source('article.html')</span>
<span class="go">Environment.get_template('base.html')</span>
<span class="go">Loader.get_source('base.html')</span>
<span class="go">---</span>
<span class="go">Environment.get_template('article.html')</span>
<span class="go">Environment.get_template('base.html')</span></code></pre>
<p>I’m not pursuing this for now, because my template code is complicated enough already and I need to simplify it a bit before adding more complexity.
(Also, I spend less and less time editing templates, so I don’t feel the effect as much.)</p>
<p>I’m writing this note in case I revisit this idea later.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/jinja2-dependency-tracking/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Python" />

    <summary type="html">Override the `get_template` method on the `Environment` and see which templates are summoned.</summary>
</entry><entry>
  <title type="html">Archeologists distinguish between “sherds” and “shards”</title>
  <link
    href="https://alexwlchan.net/notes/2026/pottery-sherds/"
    rel="alternate"
    type="text/html"
    title="Archeologists distinguish between &#34;sherds&#34; and &#34;shards&#34;"
  />
  <published>2026-08-01T09:28:34+01:00</published>
  <updated>2026-08-01T09:28:34+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/pottery-sherds/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/pottery-sherds/">
    <![CDATA[<p><strong>A sherd is made of ceramic, while a shard is made of glass or metal.</strong></p><p>This paragraph in the introduction of Marianne Bellotti’s book <a href="https://alexwlchan.net/book-reviews/kill-it-with-fire/"><em>Kill it with Fire</em></a> caught my eye (emphasis mine):</p>
<blockquote>
<p>I never became the swashbuckling international aid worker I imagined myself to be, but I ended up finding my applied anthropology work applied in legacy modernizations.
Like <strong>pottery sherds</strong>, old computer programs are artifacts of human thought.
There’s so much you can tell about an organization’s past by looking at its code.</p>
</blockquote>
<p>I’d never heard the term “sherd” before – apparently it’s a word used by archeologists and historians to describe fragments of ceramic.
It’s a shortening of the word “potsherd”.
Although colloquial English would describe those as “shards”, specialists use that word to refer to fragments of glass or metal.</p>
<p>It’s not a word I expect to use, but it was a cool sidequest while reading about digital archeology.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/pottery-sherds/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Interesting words" />

    <summary type="html">A sherd is made of ceramic, while a shard is made of glass or metal.</summary>
</entry><entry>
  <title type="html">A single command to test all my changed Go packages</title>
  <link
    href="https://alexwlchan.net/notes/2026/go-changed-tests/"
    rel="alternate"
    type="text/html"
    title="A single command to test all my changed Go packages"
  />
  <published>2026-07-21T08:49:38+01:00</published>
  <updated>2026-07-21T08:49:38+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/go-changed-tests/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/go-changed-tests/">
    <![CDATA[<p><strong><code>go test $(git ls-files --modified | xargs dirname | uniq | xargs -I '{}' echo "./{}")</code></strong></p><p>I work in a Go monolith with many packages, and my patches often span multiple patches.
Before I push to CI, I like to test all the packages with changed files, but enumerating all those packages by hand is tedious.</p>
<p>I’ve automated this with a short shell script:</p>
<pre class="lng-shell"><code>go test <span class="k">$(</span>git ls-files --modified <span class="se">\</span>
  <span class="p">|</span> xargs dirname <span class="se">\</span>
  <span class="p">|</span> uniq <span class="se">\</span>
  <span class="p">|</span> xargs -I <span class="s1">'{}'</span> echo <span class="s2">"./{}"</span><span class="k">)</span></code></pre>
<p>Here’s how it works:</p>
<ul>
<li><code>git ls-files --modified</code> – print the path of every file with an unstaged change</li>
<li><code class="nowrap">xargs dirname</code> – print the list of folders/packages</li>
<li><code>uniq</code> – de-duplicate the list</li>
<li><code>xargs -I '{}' echo "./{}"</code> – prepend <code>./</code> to each path, so the folder can be passed to <code class="nowrap">go test</code></li>
</ul>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/go-changed-tests/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Go" />
    <category term="Software testing" />

    <summary type="html">&lt;code&gt;go test $(git ls-files --modified | xargs dirname | uniq | xargs -I &#39;{}&#39; echo &#34;./{}&#34;)&lt;/code&gt;
</summary>
</entry><entry>
  <title type="html">Disable the new message animations in WhatsApp</title>
  <link
    href="https://alexwlchan.net/notes/2026/disable-whatsapp-message-animations/"
    rel="alternate"
    type="text/html"
    title="Disable the new message animations in WhatsApp"
  />
  <published>2026-07-15T08:06:27+01:00</published>
  <updated>2026-07-15T08:06:27+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/disable-whatsapp-message-animations/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/disable-whatsapp-message-animations/">
    <![CDATA[<p><strong>Look for the toggle in Settings &gt; Animations &gt; Messages.</strong></p><p>A recent WhatsApp update has introduced animations for messages – when you receive a message, it expands into place rather than just appearing on the screen.
(It reminds me of the <a href="https://youtu.be/MZwn8iP4hJc?t=18">“Scale” effect for window minimisation</a> on macOS.)</p>
<p>I find it distracting and it makes one of my friends dizzy, so I found the setting to turn it off:</p>
<ol>
<li>Select my head in the lower right hand corner to open the settings</li>
<li>Select <strong>Appearance</strong></li>
<li>Select <strong>Animations</strong></li>
<li>Toggle off <strong>Messages</strong></li>
</ol>
<p>When toggled off, messages just appear at full size immediately, rather than animating in.</p>
<p>These steps are for the iPhone app.
I assume a similar toggle exists in the Android app, but I haven’t checked.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/disable-whatsapp-message-animations/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">Look for the toggle in Settings &gt; Animations &gt; Messages.</summary>
</entry><entry>
  <title type="html">Finding high-churn folders that bother Backblaze</title>
  <link
    href="https://alexwlchan.net/notes/2026/backblaze-churn/"
    rel="alternate"
    type="text/html"
    title="Finding high-churn folders that bother Backblaze"
  />
  <published>2026-07-07T20:50:49+01:00</published>
  <updated>2026-07-07T20:50:49+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/backblaze-churn/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/backblaze-churn/">
    <![CDATA[<p><strong>I looked at log files in <code>bzreports_lastfilestransmitted</code> to work out which folders were generating new content.</strong></p><p>I just got back to my personal computer after a day of work.
My Mac has been switched on all day, doing very little.
After a few minutes of idle web browsing, I noticed that <code>bztransmit</code> was using 100% of a CPU core, which I found confusing.</p>
<p><code>bztransmit</code> is the background process that uploads files to Backblaze, my offsite backup provider.
When I checked the Backblaze preferences, it claimed I had 169MB of remaining files.
How?!</p>
<p>Nothing I’d done in the last few minutes would generate that much new content, and my Mac had been running all day; ample time to upload anything I created this morning or earlier.
(Even then, 169MB feels like a lot – most of my work is in text, which is very light on storage.)</p>
<p>I wanted to see exactly what Backblaze was uploading, and this <a href="https://www.reddit.com/r/backblaze/comments/4luspc/comment/d3r3icg/">Reddit post by Brian Wilson</a> (Backblaze co-founder) was helpful:</p>
<blockquote>
<p><strong>How can I find which files Backblaze is currently uploading?</strong> […]</p>
<p>You can watch the logs in this location on your Mac: <code>/Library​/Backblaze.bzpkg​/bzdata​/bzlogs​/bzreports_lastfilestransmitted​/&lt;day_of_week&gt;.log</code> If you watch lines getting appended to those files, make your editor REALLY WIDE to see them super nicely formatted, they report the exact files being transmitted in real time to the Backblaze datacenter, plus a measurement of how fast they were transmitted.</p>
</blockquote>
<p>I found the folder he was describing, and the most recently edited file was called <code>07.log</code>.
(Perhaps it’s day of the month, rather than day of the week?)</p>
<p>The log contains a lot of information about the files being uploaded, but I was really only interested in the paths.
I wanted to see which folders were causing so many uploads.
I wrote a short Python script to search for all the paths in the file, truncate them to a certain depth, and print the most commonly-uploaded folders:</p>
<pre class="lng-python"><code><span class="ch">#!/usr/bin/env python3</span>

<span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="kn">import</span> <span class="n">os</span>
<span class="kn">from</span> <span class="n">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">import</span> <span class="n">re</span>
<span class="kn">import</span> <span class="n">sys</span>


<span class="c1"># PATH_RE is a regex that finds strings that look like paths in</span>
<span class="c1"># my home directory.</span>
<span class="n">PATH_RE</span> <span class="o">=</span> re<span class="o">.</span>compile<span class="p">(</span>os<span class="o">.</span>environ<span class="p">[</span><span class="s2">"HOME"</span><span class="p">]</span> <span class="o">+</span> <span class="sa">r</span><span class="s2">"/[A-Za-z0-9\.\-/_]+"</span><span class="p">)</span>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">log_path</span> <span class="o">=</span> sys<span class="o">.</span>argv<span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">max_depth</span> <span class="o">=</span> int<span class="p">(</span>sys<span class="o">.</span>argv<span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">except</span> <span class="p">(</span>IndexError<span class="p">,</span> ValueError<span class="p">):</span>
        sys<span class="o">.</span>exit<span class="p">(</span><span class="sa">f</span><span class="s2">"Usage: </span><span class="si">{</span><span class="vm">__file__</span><span class="si">}</span><span class="s2"> LOG_PATH MAX_DEPTH"</span><span class="p">)</span>

    <span class="c1"># Find all the paths in the Backblaze log file</span>
    <span class="k">with</span> open<span class="p">(</span>log_path<span class="p">)</span> <span class="k">as</span> <span class="n">in_file</span><span class="p">:</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="p">[</span>Path<span class="p">(</span>m<span class="o">.</span>group<span class="p">(</span><span class="mi">0</span><span class="p">))</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> PATH_RE<span class="o">.</span>finditer<span class="p">(</span>in_file<span class="o">.</span>read<span class="p">())]</span>

    <span class="c1"># Truncate all the paths to as deep as specified by the `max_depth` argument</span>
    <span class="n">folders</span> <span class="o">=</span> <span class="p">[</span>p<span class="o">.</span>parent <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> paths<span class="p">]</span>
    <span class="n">truncated_paths</span> <span class="o">=</span> <span class="p">[</span>Path<span class="p">(</span><span class="o">*</span>p<span class="o">.</span>parts<span class="p">[:</span>max_depth<span class="p">])</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> folders<span class="p">]</span>

    <span class="c1"># Print the paths, from most to least common</span>
    <span class="k">for</span> <span class="n">path</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> Counter<span class="p">(</span>truncated_paths<span class="p">)</span><span class="o">.</span>most_common<span class="p">():</span>
        print<span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span>count<span class="si">:</span><span class="s2">5d</span><span class="si">}</span><span class="se">\t</span><span class="si">{</span>path<span class="si">}</span><span class="s2">"</span><span class="p">)</span></code></pre>
<p>The script takes two arguments: the path to the log file, and the depth at which to truncate the folders.</p>
<p>I ran the script, which quickly showed one folder being uploaded far more than the rest:</p>
<pre class="lng-console wrap"><code><span class="gp">$</span><span class="w"> </span>python3<span class="w"> </span>find_backblaze.py<span class="w"> </span>/Library​/Backblaze.bzpkg​/bzdata​/bzlogs​/bzreports_lastfilestransmitted​/07.log<span class="w"> </span><span class="m">6</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>head<span class="w"> </span>-n<span class="w"> </span><span class="m">5</span>
<span class="go"> 2292	/Users/alexwlchan/Library/Containers/com.apple.Safari</span>
<span class="go">  997	/Users/alexwlchan/Library</span>
<span class="go">  321	/Users/alexwlchan/Library/Preferences</span>
<span class="go">  267	/Users/alexwlchan/Pictures</span>
<span class="go">  116	/Users/alexwlchan/Library/Photos/Libraries</span></code></pre>
<p>I don’t know exactly what’s in that <code>com.apple.Safari</code> container; a quick poke around shows some caches, some network data, some cookies.
It looks like a big collection of tiny files that get created as I use my web browser, and Backblaze is getting overwhelmed trying to upload them all.</p>
<p>I doubt I’ll ever want to restore anything from that folder, so I’ve excluded it from Backblaze.
Almost immediately, my remaining files dropped to zero and <code>bztransmit</code> calmed down.</p>
<p>There are probably other high-churn folders that give Backblaze a headache, so I expect I may run this script again soon.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/backblaze-churn/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">I looked at log files in `bzreports_lastfilestransmitted` to work out which folders were generating new content.</summary>
</entry><entry>
  <title type="html">Always-on SSH agent forwarding with my Git pushes</title>
  <link
    href="https://alexwlchan.net/notes/2026/ssh-config/"
    rel="alternate"
    type="text/html"
    title="Always-on SSH agent forwarding with my Git pushes"
  />
  <published>2026-07-04T23:39:17+01:00</published>
  <updated>2026-07-04T23:39:17+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/ssh-config/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/ssh-config/">
    <![CDATA[<p><strong>If I want to use <code>ssh -A</code> every time I do a Git push, I need to add the remote host to my <code>~/.ssh/config</code>.</strong></p><p>All the words and code for this website are in a Git repo.
The canonical copy of the repo is a bare repository on my Mac mini, which has a post-receive hook that builds a copy of the site and uploads the files to my web server.</p>
<p>If I’m working on my Mac mini, this works fine – the SSH key for the web server is in my local keychain, so when I push the post-receive hook can upload to the web server.</p>
<p>If I’m working on my laptop, I need some extra config – when I push to the Mac mini, the post-receive hook can’t get the SSH key from the Mac mini’s keychain, and publishing to the web server fails.</p>
<p>I’ve fixed this by adding an entry to <code>~/.ssh/config</code> on my laptop:</p>
<pre class="lng-text"><code>Host phaenna-mac-mini
    HostName phaenna-mac-mini
    User alexwlchan
    ForwardAgent yes</code></pre>
<p>This tells my laptop that any time it opens an SSH connection to <code>phaenna-mac-mini</code>, it should use SSH agent forwarding.
(The equivalent of running <code class="nowrap">ssh -A</code> for a manual connection.)</p>
<p>This means my laptop’s SSH keys get forwarded to the Mac mini, and they’re available to the post-receive hook.
My laptop can SSH to my web server, so the forwarded keys allow the post-receive hook to publish correctly.</p>
<p>This config includes any SSH connection, including those created by Git.
I thought maybe I’d need config in every repo, but the global SSH config file seems to be enough.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/ssh-config/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Git" />

    <summary type="html">If I want to use `ssh -A` every time I do a Git push, I need to add the remote host to my `~/.ssh/config`.</summary>
</entry><entry>
  <title type="html">Managing the caption of a photo with AppleScript (but not PhotoKit)</title>
  <link
    href="https://alexwlchan.net/notes/2026/photo-caption/"
    rel="alternate"
    type="text/html"
    title="Managing the caption of a photo with AppleScript (but not PhotoKit)"
  />
  <published>2026-06-28T23:26:15+01:00</published>
  <updated>2026-06-28T23:26:15+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/photo-caption/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/photo-caption/">
    <![CDATA[<p><strong>Get/set the <code>description</code> of the corresponding <code>media item</code> in the Photos app.</strong></p><p>In the macOS Photos app, you can set a “caption” for a photo (select <strong>Window</strong> &gt; <strong>Info</strong>, then look for the <strong>Caption</strong> field below a photo).</p>
<p>As far as I can tell, the only way to manage this caption programatically is using AppleScript – it’s not exposed to the PhotoKit APIs.</p>
<h2 id="get-the-description">Get the description</h2>
<p>Look at the <code>description</code> field of a <code class="nowrap">media item</code> from the Photos Suite.
Here’s a script that takes a <a href="https://alexwlchan.net/2023/managing-albums-in-photos/#local-identifiers-unambiguously-specify-photos-and-albums">local identifier</a> as an argument, and prints the caption (if set):</p>
<pre class="lng-applescript"><code><span class="c">#!/usr/bin/env osascript</span>

<span class="k">on</span> run <span class="n">argv</span>
  <span class="k">set</span> <span class="n">targetId</span> <span class="k">to</span> item <span class="mi">1</span> <span class="k">of</span> argv

  <span class="k">tell</span> application <span class="s2">"Photos"</span>
    <span class="k">set</span> <span class="n">targetMedia</span> <span class="k">to</span> media item id targetId
    <span class="k">set</span> <span class="n">desc</span> <span class="k">to</span> description <span class="k">of</span> targetMedia

    <span class="k">if</span> desc <span class="ow">is</span> <span class="kc">missing value</span> <span class="k">then</span>
      return <span class="s2">""</span>
    <span class="k">else</span>
      return desc
    <span class="k">end</span> <span class="k">if</span>
  <span class="k">end</span> <span class="k">tell</span>
<span class="k">end</span> run</code></pre>
<h2 id="set-the-description">Set the description</h2>
<p>Set the <code>description</code> field.
Here’s a script that takes a local identifier and new caption as arguments:</p>
<pre class="lng-applescript"><code><span class="c">#!/usr/bin/env osascript</span>

<span class="k">on</span> run <span class="n">argv</span>
  <span class="k">set</span> <span class="n">targetId</span> <span class="k">to</span> item <span class="mi">1</span> <span class="k">of</span> argv
  <span class="k">set</span> <span class="n">newCaption</span> <span class="k">to</span> item <span class="mi">2</span> <span class="k">of</span> argv

  <span class="k">tell</span> application <span class="s2">"Photos"</span>
    <span class="k">set</span> targetMedia <span class="k">to</span> media item id targetId
    <span class="k">set</span> description <span class="k">of</span> targetMedia <span class="k">to</span> newCaption
  <span class="k">end</span> <span class="k">tell</span>
<span class="k">end</span> run</code></pre>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/photo-caption/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Photo management" />
    <category term="AppleScript" />

    <summary type="html">Get/set the `description` of the corresponding `media item` in the Photos app.</summary>
</entry><entry>
  <title type="html">Goodhart’s and Campbell’s Law are different</title>
  <link
    href="https://alexwlchan.net/notes/2026/campbell-and-goodharts-law/"
    rel="alternate"
    type="text/html"
    title="Goodhart&#39;s and Campbell&#39;s Law are different"
  />
  <published>2026-06-14T07:52:31+01:00</published>
  <updated>2026-06-14T07:52:31+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/campbell-and-goodharts-law/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/campbell-and-goodharts-law/">
    <![CDATA[<p><strong>Goodhart describes the value of a measure, while Campbell highlights the social pressure and corruption caused by one.</strong></p><p><a href="https://en.wikipedia.org/wiki/Goodhart%27s_law">Goodhart’s law</a>:</p>
<blockquote>
<p>When a measure becomes a target, it ceases to be a good measure.</p>
</blockquote>
<p><a href="https://en.wikipedia.org/wiki/Campbell%27s_law">Campbell’s law</a>:</p>
<blockquote>
<p>The more any quantitative social indicator is used for social decision-making, the more subject it will be to corruption pressures and the more apt it will be to distort and corrupt the social processes it is intended to monitor.</p>
</blockquote>
<p>I often hear Goodhart’s law discussed in a tech industry context, but quoting <a href="https://bsky.app/profile/grimalkina.bsky.social/post/3mmzrd5dq2k2d">Cat Hicks</a>: “I think most of the time we mean Campbell’s law”.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/campbell-and-goodharts-law/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="The world around us" />

    <summary type="html">Goodhart describes the value of a measure, while Campbell highlights the social pressure and corruption caused by one.</summary>
</entry><entry>
  <title type="html">Notes from &lt;em&gt;The Cornishman&lt;/em&gt; No. 176 (Spring 2026)</title>
  <link
    href="https://alexwlchan.net/notes/2026/cornishman-176/"
    rel="alternate"
    type="text/html"
    title="Notes from _The Cornishman_ No. 176 (Spring 2026)"
  />
  <published>2026-06-07T13:44:59+01:00</published>
  <updated>2026-06-07T13:44:59+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/cornishman-176/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/cornishman-176/">
    <![CDATA[<p><strong>Sheep at the station, trains on the road, and cracks in the boiler.</strong></p><p>The <a href="https://gwsr.vticket.co.uk/product.php/1294/cornishman-gwsr">Cornishman magazine</a> is a magazine sent to members of the Gloucestershire Warwickshire Steam Railway (GWSR), a heritage railway in the Cotswolds.
My granddad used to volunteer with the railway and still gives me a copy after he’s read it; these are my favourite parts from a recent issue.</p>
<picture>
<source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/cornishman-176_1x.avif 250w, https://alexwlchan.net/images/2026/cornishman-176_2x.avif 500w, https://alexwlchan.net/images/2026/cornishman-176_3x.avif 750w" type="image/avif"/><source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/cornishman-176_1x.webp 250w, https://alexwlchan.net/images/2026/cornishman-176_2x.webp 500w, https://alexwlchan.net/images/2026/cornishman-176_3x.webp 750w" type="image/webp"/><source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/cornishman-176_1x.jpg 250w, https://alexwlchan.net/images/2026/cornishman-176_2x.jpg 500w, https://alexwlchan.net/images/2026/cornishman-176_3x.jpg 750w" type="image/jpeg"/><img alt="Cover of issue 176. A steam train on a clear winter day is approaching the camera, pulling half a dozen coaches in a variety of coours, with steam billowing from the chimney and engine." src="https://alexwlchan.net/images/2026/cornishman-176_1x.jpg" width="250"/>
</picture>
<p>This passage about Toddington, by Rose Phillips (page 22) reminded me of a similar incident described in <a href="https://alexwlchan.net/book-reviews/false-starts-near-misses-and-dangerous-goods/"><em>False Starts, Near Misses and Dangerous Goods</em></a>:</p>
<blockquote>
<p>
    One autumn day, luckily not a running day, a call was received to help round up some sheep which had found their way into the station.
    No grass-cutting needed that week!
    But after the sheep had been safely removed, the incident prompt`ed a careful check of the fences.
  </p>
<figure>
<picture>
<source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/sheep-at-toddington_1x.avif 500w, https://alexwlchan.net/images/2026/sheep-at-toddington_2x.avif 1000w, https://alexwlchan.net/images/2026/sheep-at-toddington_3x.avif 1500w" type="image/avif"/><source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/sheep-at-toddington_1x.webp 500w, https://alexwlchan.net/images/2026/sheep-at-toddington_2x.webp 1000w, https://alexwlchan.net/images/2026/sheep-at-toddington_3x.webp 1500w" type="image/webp"/><source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/sheep-at-toddington_1x.jpg 500w, https://alexwlchan.net/images/2026/sheep-at-toddington_2x.jpg 1000w, https://alexwlchan.net/images/2026/sheep-at-toddington_3x.jpg 1500w" type="image/jpeg"/><img alt="A small grassy area nextto a platform, with some picnic tables and three sheep grazing around then base of a tree." src="https://alexwlchan.net/images/2026/sheep-at-toddington_1x.jpg" width="500"/>
</picture>
<figcaption>
      Wooly helpers at Toddington. Photo by Peter Nash.
    </figcaption>
</figure>
</blockquote>
<p>Chris Bambridge’s account of “Construction &amp; Maintenance” (page 23) caught my eye, because it names a railway feature that I often see, but didn’t know the name of.</p>
<blockquote>
<p>
    We are also becoming proficient in making dagger boards for station canopies where these have rotted away.
    Painting the individual components takes several weeks and will when installed be food for a significant period of time.
    Several station canopies are to be addressed.
  </p>
<p>
<picture>
<source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/dagger-boards_1x.avif 250w, https://alexwlchan.net/images/2026/dagger-boards_2x.avif 500w, https://alexwlchan.net/images/2026/dagger-boards_3x.avif 750w" type="image/avif"/><source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/dagger-boards_1x.webp 250w, https://alexwlchan.net/images/2026/dagger-boards_2x.webp 500w, https://alexwlchan.net/images/2026/dagger-boards_3x.webp 750w" type="image/webp"/><source sizes="(max-width:250px)100vw,250px" srcset="https://alexwlchan.net/images/2026/dagger-boards_1x.jpg 250w, https://alexwlchan.net/images/2026/dagger-boards_2x.jpg 500w, https://alexwlchan.net/images/2026/dagger-boards_3x.jpg 750w" type="image/jpeg"/><img alt="A row of wooden boards which form a triangle point at the end, stacked in a workshop." src="https://alexwlchan.net/images/2026/dagger-boards_1x.jpg" width="250"/>
</picture>
</p>
</blockquote>
<p>On page 29, I was fascinated by Pete Mason’s explanation of how locomotive 35006 was transported to another railway for a gala event:</p>
<blockquote>
<p>One of the main risks of transporting a steam locomotive by road is the process of loading the loco onto the road transporter.
As the loco rises up the ramp, the weight is transferred off some wheelsets and onto others, greatly exceeding the loads that the springs are designed for.
To avoid damage, we insist that stop blocks are placed to limit the movement of the axleboxes so that the weight of the loco doesn’t cause the springs to move beyond their limit.
This transfers the weight of the loco directly to the wheels, without the springs exceeding their design load.
These blocks have to ben removed again as soon as the loco is unloaded so that the loco is once again sitting on its own suspension.</p>
<p>At Kidderminster, access to the site is very restricted and the loco was delivered the opposite way round to what was required.
Once the loco and tender had been coupled together, the loco was shunted onto the turntable.
The crew then used the manual handles to crank the loco round so that it faced the desired direction – the first time the loco had been on a turntable since the 1960s.</p>
</blockquote>
<p>The news was not so good for locomotive 7903, which John Cruxon explained on page 30:</p>
<blockquote>
<p>
    I am sure most volunteers and shareholders will have heard that 7903 has been withdrawn for overhaul following an unsuccessful boiler extension examination. [...]
  </p>
<p>
    To obtain that extension, the boiler had to undergo non-destructive testing of the firebox stays.
    These are the copper and steel rods, known as stays, that hold the inner copper firebox apart from the outer steel firebox.
    In normal service, when we wash out a boiler and test for broken stays, we tap them with a small hammer and listen for a clear ringing of "pinging" sound.
  </p>
<p>
    This test had been carried out and all stays appeared to be satisfactory.
    However, when the BES engineer carried out testing using electronic equipment, which sends a signal down each stay to confirm it is intact, five copper stays failed to register any indication, suggesting that they are broken.
  </p>
<p>
<picture>
<source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/boiler-7903_1x.avif 500w, https://alexwlchan.net/images/2026/boiler-7903_2x.avif 1000w" type="image/avif"/><source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/boiler-7903_1x.webp 500w, https://alexwlchan.net/images/2026/boiler-7903_2x.webp 1000w" type="image/webp"/><source sizes="(max-width:500px)100vw,500px" srcset="https://alexwlchan.net/images/2026/boiler-7903_1x.jpg 500w, https://alexwlchan.net/images/2026/boiler-7903_2x.jpg 1000w" type="image/jpeg"/><img alt="A large metal boiler made of an inner chamber and an outer skin, with thin rods visible connecting the two." src="https://alexwlchan.net/images/2026/boiler-7903_1x.jpg" width="500"/>
</picture>
</p>
</blockquote>
<p>This reminded me of the film <em>Oh, Mr Porter!</em> in which Will Hay plays a <a href="https://en.wikipedia.org/wiki/Wheel_tapper">wheeltapper</a> – using a hammer to test stays feels very similar.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/cornishman-176/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="The world around us" />

    <summary type="html">Sheep at the station, trains on the road, and cracks in the boiler.</summary>
</entry><entry>
  <title type="html">GitUp can’t diff text files larger than 8MB</title>
  <link
    href="https://alexwlchan.net/notes/2026/gitup-diff-8mb/"
    rel="alternate"
    type="text/html"
    title="GitUp can&#39;t diff text files larger than 8MB"
  />
  <published>2026-05-27T00:27:03+01:00</published>
  <updated>2026-05-27T00:27:03+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/gitup-diff-8mb/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/gitup-diff-8mb/">
    <![CDATA[<p><strong>Larger files don’t appear in the diff view; they’re just shown as a file icon.</strong></p><p>I use <a href="https://github.com/git-up/GitUp">GitUp</a> as my Git interface, and I was wondering why some of my text files were no longer showing a graphical diff in the interface.</p>
<p>I tried bisecting the files, thinking it might be some special characters that cause the file to be registering as a binary file, and discovered that 8,388,608 bytes is the magic number – it doesn’t produce diffs for files larger than that.
Note that
<math>
<mn>8,388,608</mn>
<mo>=</mo>
<mn>8</mn>
<mo>×</mo>
<msup>
<mn>2</mn>
<mn>20</mn>
</msup>
</math> – that is, 8MB.</p>
<p>A quick search shows other tools having bugs or edge cases at 8,388,608 bytes.</p>
<p>I’m not going to dive into the GitUp source code to work out what the issue is here; for now it’s enough to know I can fix my diffs by reducing the size of my source files.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/gitup-diff-8mb/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Git" />

    <summary type="html">Larger files don&#39;t appear in the diff view; they&#39;re just shown as a file icon.</summary>
</entry><entry>
  <title type="html">Format “human-readable” JSON with FracturedJSON &amp;rarr;</title>
  <link
    href="https://github.com/j-brooke/FracturedJson/wiki"
    rel="alternate"
    type="text/html"
    title="Format &#34;human-readable&#34; JSON with FracturedJSON"
  />
  <published>2026-05-15T08:56:07+01:00</published>
  <updated>2026-05-15T08:56:07+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/fractured-json/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/fractured-json/">
    <![CDATA[<p><strong>Short objects and arrays are compressed onto a single line; larger objects are displayed as a table. Available in a variety of languages.</strong></p><p>Here’s an interesting project that balances compact and human-readable JSON, automatically applying certain sensibilities that somebody might add if they were writing JSON by hand:</p>
<blockquote>
<p>FracturedJson is a family of utilities that format JSON data in a way that’s easy for humans to read, but fairly compact. Arrays and objects are written on single lines, as long as they’re neither too long nor too complex. When several such lines are similar in structure, they’re written with fields aligned like a table. Long arrays are written with multiple items per line across multiple lines.</p>
</blockquote>
<p>Here’s an example of the output it produces:</p>
<pre class="lng-json"><code><span class="p">{</span>
    "BasicObject"   <span class="p">:</span> <span class="p">{</span>
        "ModuleId"   <span class="p">:</span> <span class="s2">"armor"</span><span class="p">,</span>
        "Name"       <span class="p">:</span> <span class="s2">""</span><span class="p">,</span>
        "Locations"  <span class="p">:</span> <span class="p">[</span>
            <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">5</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">7</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">8</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span>  <span class="mi">9</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">10</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">11</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">13</span><span class="p">],</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">14</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">14</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">],</span>
            <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">9</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">8</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">7</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">6</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">5</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">4</span><span class="p">],</span>
            <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">4</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">5</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">6</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">7</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">8</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span> <span class="p">[</span> <span class="mi">8</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span>
            <span class="p">[</span> <span class="mi">7</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span> <span class="mi">6</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span> <span class="mi">5</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span> <span class="mi">4</span><span class="p">,</span>  <span class="mi">3</span><span class="p">],</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span>  <span class="mi">4</span><span class="p">],</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span>  <span class="mi">5</span><span class="p">],</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span>  <span class="mi">6</span><span class="p">],</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span>  <span class="mi">7</span><span class="p">],</span>
            <span class="p">[</span> <span class="mi">0</span><span class="p">,</span>  <span class="mi">8</span><span class="p">],</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span>  <span class="mi">8</span><span class="p">],</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span>  <span class="mi">7</span><span class="p">],</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span>  <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span>  <span class="mi">5</span><span class="p">],</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span>  <span class="mi">4</span><span class="p">]</span>
        <span class="p">],</span>
        "Orientation"<span class="p">:</span> <span class="s2">"Fore"</span><span class="p">,</span>
        "Seed"       <span class="p">:</span> <span class="mi">272691529</span>
    <span class="p">},</span>
    "SimilarArrays" <span class="p">:</span> <span class="p">{</span>
        "Katherine"<span class="p">:</span> <span class="p">[</span><span class="s2">"blue"</span><span class="p">,</span>       <span class="s2">"lightblue"</span><span class="p">,</span> <span class="s2">"black"</span>       <span class="p">],</span>
        "Logan"    <span class="p">:</span> <span class="p">[</span><span class="s2">"yellow"</span><span class="p">,</span>     <span class="s2">"blue"</span><span class="p">,</span>      <span class="s2">"black"</span><span class="p">,</span> <span class="s2">"red"</span><span class="p">],</span>
        "Erik"     <span class="p">:</span> <span class="p">[</span><span class="s2">"red"</span><span class="p">,</span>        <span class="s2">"purple"</span>                   <span class="p">],</span>
        "Jean"     <span class="p">:</span> <span class="p">[</span><span class="s2">"lightgreen"</span><span class="p">,</span> <span class="s2">"yellow"</span><span class="p">,</span>    <span class="s2">"black"</span>       <span class="p">]</span>
    <span class="p">},</span>
    "SimilarObjects"<span class="p">:</span> <span class="p">[</span>
        <span class="p">{</span> "type"<span class="p">:</span> <span class="s2">"turret"</span><span class="p">,</span>    "hp"<span class="p">:</span> <span class="mi">400</span><span class="p">,</span> "loc"<span class="p">:</span> <span class="p">{</span>"x"<span class="p">:</span> <span class="mi">47</span><span class="p">,</span> "y"<span class="p">:</span>  <span class="mi">-4</span><span class="p">},</span> "flags"<span class="p">:</span> <span class="s2">"S"</span>   <span class="p">},</span>
        <span class="p">{</span> "type"<span class="p">:</span> <span class="s2">"assassin"</span><span class="p">,</span>  "hp"<span class="p">:</span>  <span class="mi">80</span><span class="p">,</span> "loc"<span class="p">:</span> <span class="p">{</span>"x"<span class="p">:</span> <span class="mi">12</span><span class="p">,</span> "y"<span class="p">:</span>   <span class="mi">6</span><span class="p">},</span> "flags"<span class="p">:</span> <span class="s2">"Q"</span>   <span class="p">},</span>
        <span class="p">{</span> "type"<span class="p">:</span> <span class="s2">"berserker"</span><span class="p">,</span> "hp"<span class="p">:</span> <span class="mi">150</span><span class="p">,</span> "loc"<span class="p">:</span> <span class="p">{</span>"x"<span class="p">:</span>  <span class="mi">0</span><span class="p">,</span> "y"<span class="p">:</span>   <span class="mi">0</span><span class="p">}</span>                 <span class="p">},</span>
        <span class="p">{</span> "type"<span class="p">:</span> <span class="s2">"pittrap"</span><span class="p">,</span>              "loc"<span class="p">:</span> <span class="p">{</span>"x"<span class="p">:</span> <span class="mi">10</span><span class="p">,</span> "y"<span class="p">:</span> <span class="mi">-14</span><span class="p">},</span> "flags"<span class="p">:</span> <span class="s2">"S,I"</span> <span class="p">}</span>
    <span class="p">]</span>
<span class="p">}</span></code></pre>
<p>I haven’t tried the code at all, but noting so I can find the project again.</p>
<p>There are implementations in several languages, including .NET, JavaScript, Rust, and Python.
The Python libraries are thin wrappers around the .NET and Rust versions, or there’s a <a href="https://github.com/masaccio/compact-json">deprecated <code class="nowrap">compact-json</code> project</a> that’s written in pure Python.
It also reminds me of Tailscale’s <a href="https://github.com/tailscale/hujson">HuJSON library</a>, which lines up object keys in a similar way when you format your JSON.</p>
<p>For a while I’ve wanted something like this for <a href="https://alexwlchan.net/projects/javascript-data-files/">javascript-data-files</a>, and I had a brief attempt at it – but this is a much harder problem than I initially realised.
I don’t want this behaviour enough to add a dependency, and it’s more complicated than I want to implement or maintain on my own (even if I vendor an existing project).</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/fractured-json/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">Short objects and arrays are compressed onto a single line; larger objects are displayed as a table. Available in a variety of languages.</summary>
</entry><entry>
  <title type="html">Testing the width of a page on a mobile device using Playwright</title>
  <link
    href="https://alexwlchan.net/notes/2026/test-width-of-page/"
    rel="alternate"
    type="text/html"
    title="Testing the width of a page on a mobile device using Playwright"
  />
  <published>2026-05-06T19:27:20+01:00</published>
  <updated>2026-05-06T19:27:20+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/test-width-of-page/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/test-width-of-page/">
    <![CDATA[<p><strong>Create a new browser context with a narrow screen, then get <code>document.body.scrollWidth</code> to get the width of the displayed page.</strong></p><p>One perennial source of bugs on this site is incorrect page widths on mobile devices: something isn’t wrapping or cropping properly, and it forces the whole page to be too wide.</p>
<p>For a long time I’ve been playing whack-a-mole with these bugs, but after writing about <a href="https://alexwlchan.net/2026/playwright/">using Playwright</a>, I realised I could write a regression test.</p>
<p>Here’s the approximate test I came up with.
It creates a new browser context with a mobile screen size, opens the page I’m interested in, then runs some JavaScript on the page to measure the scroll width:</p>
<pre class="lng-python"><code><span class="k">def</span> <span class="n">test_page_is_right_size_on_narrow_screens</span><span class="p">(</span><span class="n">browser</span><span class="p">:</span> Browser<span class="p">,</span> <span class="n">base_url</span><span class="p">:</span> str<span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    Check that on narrow screens, pages size to fit the screen.</span>

<span class="sd">    This is a regression test for issues I've had in the past where</span>
<span class="sd">    some wide element breaks the page when the window is narrower than it.</span>
<span class="sd">    """</span>
    <span class="n">width</span> <span class="o">=</span> <span class="mi">350</span>
    <span class="n">height</span> <span class="o">=</span> <span class="mi">650</span>

    <span class="n">context</span> <span class="o">=</span> browser<span class="o">.</span>new_context<span class="p">(</span>viewport<span class="o">=</span><span class="p">{</span><span class="s2">"width"</span><span class="p">:</span> width<span class="p">,</span> <span class="s2">"height"</span><span class="p">:</span> height<span class="p">})</span>

    <span class="n">page</span> <span class="o">=</span> context<span class="o">.</span>new_page<span class="p">()</span>
    page<span class="o">.</span>goto<span class="p">(</span>base_url <span class="o">+</span> <span class="s2">"/computers-and-code/"</span><span class="p">)</span>

    <span class="n">scroll_width</span> <span class="o">=</span> page<span class="o">.</span>evaluate<span class="p">(</span><span class="s2">"document.body.scrollWidth"</span><span class="p">)</span>

    <span class="k">assert</span> scroll_width <span class="o">==</span> width</code></pre>
<p>I don’t think I’d want to write a regression test for every CSS change, but it’s cool I can do it for this class of especially annoying bugs.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/test-width-of-page/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Software testing" />
    <category term="Blogging about blogging" />

    <summary type="html">Create a new browser context with a narrow screen, then get `document.body.scrollWidth` to get the width of the displayed page.</summary>
</entry><entry>
  <title type="html">Disable AirPods charging notifications</title>
  <link
    href="https://alexwlchan.net/notes/2026/disable-airpods-charging-notifications/"
    rel="alternate"
    type="text/html"
    title="Disable AirPods charging notifications"
  />
  <published>2026-05-02T06:45:10+01:00</published>
  <updated>2026-05-02T06:45:10+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/disable-airpods-charging-notifications/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/disable-airpods-charging-notifications/">
    <![CDATA[<p><strong>In Settings &gt; Bluetooth, I can stop my iPhone telling me that my AirPods have charged overnight.</strong></p><p>Every morning, I wake up to a notification on my iPhone:</p>
<blockquote>
<p>Alex’s AirPods Pro 2 are fully charged.</p>
</blockquote>
<p>This is not a surprise.
This is not news.
This is something iOS could have worked out will always be the case, because I have a charging stand where I put my AirPods overnight.
My AirPods are <em>always</em> fully charged when I wake up.</p>
<p>I have, on multiple occasions, searched the “Notifications” area of the iPhone Settings app to find where I can disable this notification, to no avail.</p>
<p>Today, I got a respite: I found an <a href="https://support.apple.com/en-gb/119912">Apple Support document</a> that explains where to configure charging notifications:</p>
<blockquote>
<p>You can use your iPhone or iPad to […] notify you when charging is complete.</p>
<ol>
<li>Put your AirPods in your ears, and make sure they’re connected to your iPhone or iPad.</li>
<li>On your iPhone or iPad, go to Settings, select your AirPods, then tap Battery.</li>
<li>Turn on Charging Notifications.</li>
</ol>
</blockquote>
<p>I have no idea why these aren’t also available in the Notifications settings, but I can <em>finally</em> stop these pointless messages.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/disable-airpods-charging-notifications/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Computers and code" />

    <summary type="html">In Settings &gt; Bluetooth, I can stop my iPhone telling me that my AirPods have charged overnight.</summary>
</entry><entry>
  <title type="html">Start a Caddy server in a subprocess during a Python session</title>
  <link
    href="https://alexwlchan.net/notes/2026/caddy-in-pytest/"
    rel="alternate"
    type="text/html"
    title="Start a Caddy server in a subprocess during a Python session"
  />
  <published>2026-04-30T09:04:38+01:00</published>
  <updated>2026-04-30T09:04:38+01:00</updated>

  <id>https://alexwlchan.net/notes/2026/caddy-in-pytest/</id>

  <content type="html" xml:base="https://alexwlchan.net/notes/2026/caddy-in-pytest/">
    <![CDATA[<p><strong>Start the server with <code>subprocess.Popen</code>, poll until it’s available, yield the base URL, then clean up the process when you’re done.</strong></p><p>For local developemnt and testing of this site, I’ve started to run a Caddy server with my real config to be a better replica of my real setup.
I wanted to start a Caddy server in my automated tests, and shut it down when I’m done.</p>
<p>Here’s the fixture I came up with:</p>
<pre class="lng-python"><code><span class="kn">from</span> <span class="n">collections</span><span class="p">.</span><span class="n">abc</span> <span class="kn">import</span> <span class="n">Iterator</span>
<span class="kn">import</span> <span class="n">subprocess</span>
<span class="kn">from</span> <span class="n">subprocess</span> <span class="kn">import</span> <span class="n">PIPE</span>
<span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="n">urllib</span><span class="p">.</span><span class="n">error</span>
<span class="kn">import</span> <span class="n">urllib</span><span class="p">.</span><span class="n">request</span>

<span class="kn">import</span> <span class="n">pytest</span>


@pytest<span class="o">.</span>fixture<span class="p">(</span>scope<span class="o">=</span><span class="s2">"session"</span><span class="p">)</span>
<span class="k">def</span> <span class="n">caddy_server_url</span><span class="p">()</span> <span class="o">-&gt;</span> Iterator<span class="p">[</span>str<span class="p">]:</span>
    <span class="sd">"""</span>
<span class="sd">    Start an instance of Caddy running in the current directory, and</span>
<span class="sd">    return the base URL.</span>
<span class="sd">    """</span>
    <span class="n">port</span> <span class="o">=</span> <span class="mi">5858</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"caddy"</span><span class="p">,</span> <span class="s2">"file-server"</span><span class="p">,</span> <span class="s2">"--listen"</span><span class="p">,</span> <span class="sa">f</span><span class="s2">":</span><span class="si">{</span>port<span class="si">}</span><span class="s2">"</span><span class="p">]</span>

    <span class="k">with</span> subprocess<span class="o">.</span>Popen<span class="p">(</span>cmd<span class="p">,</span> stdout<span class="o">=</span>PIPE<span class="p">,</span> stderr<span class="o">=</span>PIPE<span class="p">)</span> <span class="k">as</span> <span class="n">proc</span><span class="p">:</span>
        <span class="n">url</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"http://localhost:</span><span class="si">{</span>port<span class="si">}</span><span class="s2">/"</span>

        <span class="c1"># Wait for up to a second waiting for the server to start.</span>
        <span class="c1">#</span>
        <span class="c1"># If we get a ConnectionRefusedError, the server hasn't started yet.</span>
        <span class="c1"># If we get an HTTPError or a 200 OK, we've connected to the server</span>
        <span class="c1"># and it's serving HTTP traffic, so it's started.</span>
        <span class="n">t0</span> <span class="o">=</span> time<span class="o">.</span>time<span class="p">()</span>
        <span class="k">while</span> time<span class="o">.</span>time<span class="p">()</span> <span class="o">-</span> t0 <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                urllib<span class="o">.</span>request<span class="o">.</span>urlopen<span class="p">(</span>url<span class="p">)</span>
            <span class="k">except</span> urllib<span class="o">.</span>error<span class="o">.</span>HTTPError<span class="p">:</span>
                <span class="k">break</span>
            <span class="k">except</span> urllib<span class="o">.</span>error<span class="o">.</span>URLError <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
                <span class="k">if</span> exc<span class="o">.</span>args <span class="ow">and</span> isinstance<span class="p">(</span>exc<span class="o">.</span>args<span class="p">[</span><span class="mi">0</span><span class="p">],</span> ConnectionRefusedError<span class="p">):</span>
                    <span class="k">pass</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">assert</span> <span class="ow">not</span> proc<span class="o">.</span>poll<span class="p">()</span>

        <span class="k">yield</span> url

        proc<span class="o">.</span>terminate<span class="p">()</span>
        proc<span class="o">.</span>wait<span class="p">(</span>timeout<span class="o">=</span><span class="mi">1</span><span class="p">)</span></code></pre>
<p>And here’s what a test looks like:</p>
<pre class="lng-python"><code><span class="k">def</span> <span class="n">test_can_start_web_server</span><span class="p">(</span><span class="n">caddy_server_url</span><span class="p">:</span> str<span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    Fetch a page from the running web server.</span>
<span class="sd">    """</span>
    <span class="n">resp</span> <span class="o">=</span> urllib<span class="o">.</span>request<span class="o">.</span>urlopen<span class="p">(</span>caddy_server_url <span class="o">+</span> <span class="s2">"example.py"</span><span class="p">)</span>
    <span class="k">assert</span> <span class="sa">b</span><span class="s2">"test_can_start_web_server"</span> <span class="ow">in</span> resp<span class="o">.</span>read<span class="p">()</span></code></pre>
<p>The fixture starts a file server with <code>subprocess</code>, then polls until the server is available.
On my Mac mini, Caddy takes ~0.01s to start – long enough I can’t start running tests immediately, fast enough that any fixed sleep would be inefficient (especially as it’ll be slower in CI).</p>
<p>At the end of the fixture, I call <code>proc.terminate()</code> and <code>proc.wait()</code> to clean everything up.
The <code>terminate()</code> sends a SIGTERM; the <code>wait()</code> blocks until the child process terminates.
The process shuts down quickly, but I do need to wait or I get warnings from pytest that I have an unterminated process.</p>
<p>The fixture is session-scoped so I only have to start/stop the server once across my test suite.</p>
<p>In my real codebase, this code is split across two functions – a function that starts the server, and a function that wraps it in a pytest fixture.
I reuse the server function in my <code>serve_site.py</code> script, which runs a local development server for the site.
I’m also pointing it at a <code>Caddyfile</code> with my site config, rather than running a bare <code>file_server</code>.</p>
<p>This is similar to <a href="https://til.simonwillison.net/pytest/subprocess-server">Simon Willison’s recipe</a>.</p>

    <p>[If the formatting of this post looks odd in your feed reader, <a href="https://alexwlchan.net/notes/2026/caddy-in-pytest/">visit the original article</a>]</p>
    ]]>
  </content>

    <category term="Python" />
    <category term="Caddy" />
    <category term="Software testing" />

    <summary type="html">Start the server with `subprocess.Popen`, poll until it&#39;s available, yield the base URL, then clean up the process when you&#39;re done.</summary>
</entry></feed>