{"version":"https://jsonfeed.org/version/1.1","title":"Primary Unit","home_page_url":"https://www.robjwells.com/","feed_url":"https://www.robjwells.com/feed.json","description":"A blog by Rob Wells, mostly about computer stuff.","author":{"name":"Rob Wells","url":"https://www.robjwells.com/about","avatar":"https://www.robjwells.com/images/rob-scafell-pike.jpg"},"authors":[{"name":"Rob Wells","url":"https://www.robjwells.com/about","avatar":"https://www.robjwells.com/images/rob-scafell-pike.jpg"}],"items":[{"id":"https://www.robjwells.com/2022/12/rss-whitespace-and-hugo/","url":"https://www.robjwells.com/2022/12/rss-whitespace-and-hugo/","title":"RSS Whitespace and Hugo","date_published":"2022-12-24T13:09:00Z","content_html":"\u003cp\u003eOne thing I noticed about \u003ca href=\"/2022/12/sunrise-and-sunset-database/\"\u003eyesterday\u0026rsquo;s post\u003c/a\u003e was that the code formatting in\nthe RSS feed was off. This was due to building the site with \u003ca href=\"https://gohugo.io/hugo-pipes/minification/\"\u003eHugo\u0026rsquo;s \u003ccode\u003e--minify\u003c/code\u003e\noption\u003c/a\u003e, which \u0026ldquo;reached into\u0026rdquo; the \u003ccode\u003e\u0026lt;description\u0026gt;\u003c/code\u003e item for the post and\nsquished the whitespace without regard to whether it was in a \u003ccode\u003e\u0026lt;pre\u0026gt;\u003c/code\u003e block.\u003c/p\u003e\n\u003cp\u003eThis was a simple fix: in Hugo\u0026rsquo;s settings there are a bunch of \u003ca href=\"https://gohugo.io/getting-started/configuration/#configure-minify\"\u003eoptions for\n\u003ccode\u003eminify\u003c/code\u003e\u003c/a\u003e, one of which is \u003ccode\u003edisableXML\u003c/code\u003e. Add that to your config\nfile, rebuild, and you\u0026rsquo;re done:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-toml\" data-lang=\"toml\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e[\u003c/span\u003e\u003cspan class=\"nx\"\u003eminify\u003c/span\u003e\u003cspan class=\"p\"\u003e]\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"nx\"\u003edisableXML\u003c/span\u003e \u003cspan class=\"p\"\u003e=\u003c/span\u003e \u003cspan class=\"kc\"\u003etrue\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e"},{"id":"https://www.robjwells.com/2022/12/sunrise-and-sunset-database/","url":"https://www.robjwells.com/2022/12/sunrise-and-sunset-database/","title":"Sunrise and Sunset Database","date_published":"2022-12-23T20:00:00Z","content_html":"\u003cp\u003eA while ago, I was having trouble with the automatic light-dark theme switching on my laptop, as macOS tries to wait until an opportune moment when the computer is idle to make the change. That\u0026rsquo;s fair enough, as it does involve a moment when the system is not responsive. I was finding that if I kept working it would often be quite a while after sunset that the switch occurred.\u003c/p\u003e\n\u003cp\u003e\u003ca href=\"https://wiki.keyboardmaestro.com/action/Set_System_to_Dark_Mode\"\u003eKeyboard Maestro can set the system light-dark theme\u003c/a\u003e, so I thought I\u0026rsquo;d rig it up to periodically check whether sunrise or sunset had occurred, and then force the change rather than being polite. There\u0026rsquo;s a trade off but overall I prefer the automatic switching to take place closer to the sunrise or sunset time rather than the system waiting in an attempt not to inconvenience me briefly.\u003c/p\u003e\n\u003cp\u003eIt relies on a local SQLite database of sunrise and sunset times for my home, but when I first rigged it up, I \u0026ldquo;only\u0026rdquo; put in the times for 2021 and 2022. It\u0026rsquo;s now time to fill the database for the coming years, so what better time to have a peek at how it works?\u003c/p\u003e\n\u003cp\u003e\u003ca href=\"https://github.com/robjwells/sunrise-sunset\"\u003eYou can find the git repository here\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eFirst off, we need a data source. I didn\u0026rsquo;t include a scraper in repo originally, partly out of politeness, but partly also because I hoped to change the data source.\u003c/p\u003e\n\u003cp\u003eIn the UK, \u003ca href=\"http://astro.ukho.gov.uk/\"\u003eHM Nautical Almanac Office\u003c/a\u003e (NAO) publishes various rise, set and twilight times (for other celestial objects as well as the sun). After a few \u003ca href=\"http://astro.ukho.gov.uk/nao/miscellanea/birs2.html\"\u003efalse\u003c/a\u003e \u003ca href=\"http://astro.ukho.gov.uk/nao/miscellanea/UK_SRSS/uk_dec.html\"\u003estarts\u003c/a\u003e trying to find a big list of daily rise and set times for a long period at a time, you\u0026rsquo;ll come across the \u003ca href=\"http://astro.ukho.gov.uk/surfbin/runsurf_beta.cgi\"\u003eWebSurf 2.0\u003c/a\u003e application, which reassuringly has \u003ccode\u003e.cgi\u003c/code\u003e in the URL (and, naturally, you would hope is entirely written in Perl).\u003c/p\u003e\n\u003cp\u003eBut after clicking your way through, you’ll end up with a big plain-text listing with no markup (here, snipped horizontally and vertically):\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e    2023 Sunrise and Sunset times\n\n\n         January       February\n\n       Rise   Set     Rise   Set\n       h  m   h  m    h  m   h  m\n  1   08 06  16 02   07 39  16 50\n  2   08 06  16 03   07 38  16 51\n  3   08 06  16 04   07 36  16 53\n  4   08 06  16 05   07 35  16 55\n  5   08 06  16 07   07 33  16 57\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eNow while this is not impossible to deal with, and I of course hear \u003ca href=\"https://www.youtube.com/watch?v=sERiPuOQyvo\"\u003eJerusalem\u003c/a\u003e when thinking about the public servants who went through the trouble of making this data available, at the end of the day I am, in the words of the Tories, a shirker rather than a striver. So let\u0026rsquo;s find an easier way! (Also one that works for people outside Britain.)\u003c/p\u003e\n\u003cp\u003eThankfully, \u003cdel\u003e\u003ca href=\"https://www.whattimeisitrightnow.com/\"\u003ewhattimeisitrightnow.com\u003c/a\u003e\u003c/del\u003e \u003ca href=\"https://www.timeanddate.com/sun/uk/london\"\u003etimeanddate.com\u003c/a\u003e has us covered. Search for your preferred location and go to the \u0026ldquo;Sun and Moon\u0026rdquo; ▸ \u0026ldquo;Sunrise \u0026amp; Sunset\u0026rdquo; view. You\u0026rsquo;ll end up with something like \u003ccode\u003etimeanddate.com/sun/uk/london\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eThat will show you the rise and set times for the current month, but if you play with the dropdown above the table, you may notice that picking a different month or year is as simple as adding parameters to the URL, like so:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-bash\" data-lang=\"bash\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"s2\"\u003e\u0026#34;https://www.timeanddate.com/sun/uk/london?month=\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003emonth\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026amp;year=\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003eyear\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThankfully, too, this data is in the HTML that gets served up, with no JavaScript-dependent front-end framework or scraper-blocker in the way. That leads us naturally onto this simple shell script:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-bash\" data-lang=\"bash\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003eseq \u003cspan class=\"m\"\u003e2023\u003c/span\u003e \u003cspan class=\"m\"\u003e2030\u003c/span\u003e \u003cspan class=\"p\"\u003e|\u003c/span\u003e \u003cspan class=\"k\"\u003ewhile\u003c/span\u003e \u003cspan class=\"nb\"\u003eread\u003c/span\u003e year\u003cspan class=\"p\"\u003e;\u003c/span\u003e \u003cspan class=\"k\"\u003edo\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    seq \u003cspan class=\"m\"\u003e1\u003c/span\u003e \u003cspan class=\"m\"\u003e12\u003c/span\u003e \u003cspan class=\"p\"\u003e|\u003c/span\u003e \u003cspan class=\"k\"\u003ewhile\u003c/span\u003e \u003cspan class=\"nb\"\u003eread\u003c/span\u003e month\u003cspan class=\"p\"\u003e;\u003c/span\u003e \u003cspan class=\"k\"\u003edo\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e        curl --silent \u003cspan class=\"se\"\u003e\\\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"se\"\u003e\u003c/span\u003e            \u003cspan class=\"s2\"\u003e\u0026#34;https://www.timeanddate.com/sun/uk/london?month=\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003emonth\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026amp;year=\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003eyear\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;\u003c/span\u003e \u003cspan class=\"se\"\u003e\\\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"se\"\u003e\u003c/span\u003e            \u0026gt; \u003cspan class=\"s2\"\u003e\u0026#34;sunrise-\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003eyear\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e-\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003emonth\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e.html\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003edone\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edone\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThis just dumps each month from 2023 through 2030 into an individual HTML file in the current directory. From there we turn to \u003ca href=\"https://github.com/robjwells/sunrise-sunset/blob/main/process_html_tables.py\"\u003e\u003ccode\u003eprocess_html_tables.py\u003c/code\u003e\u003c/a\u003e. This is pretty straightforward and broken up into nice small parts, so you can read that at your leisure. It leans on Pandas to parse the HTML tables from the downloaded files and Pendulum to correctly handle dates with timezones, but there\u0026rsquo;s not much too it. Do check that you\u0026rsquo;re using the right timezone, though.\u003c/p\u003e\n\u003cp\u003eRunning that Python file will process all the scraped HTML files in the folder, and print an array of JSON objects, each containing a date, sunrise datetime and sunset datetime. This is an intermediate step that isn\u0026rsquo;t strictly necessary, but you might want that data available in a plain-text file.\u003c/p\u003e\n\u003cp\u003eBut it also means it\u0026rsquo;s trivial to create a SQLite database file with \u003ca href=\"https://sqlite-utils.datasette.io/en/stable/\"\u003e\u003ccode\u003esqlite-utils\u003c/code\u003e\u003c/a\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-bash\" data-lang=\"bash\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003epython3 process_html_tables.py \u003cspan class=\"p\"\u003e|\u003c/span\u003e sqlite-utils insert sunrise-sunset.db london -\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThat will create the \u0026ldquo;london\u0026rdquo; (in this case) table for you, but there\u0026rsquo;s also an SQL file to create both the table for the sunrise and sunset times and also one to log successful checks (\u003ccode\u003ecreate-tables.sql\u003c/code\u003e):\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sql\" data-lang=\"sql\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003eCREATE\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003eTABLE\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"n\"\u003elondon\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003etext\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003eprimary\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003ekey\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"n\"\u003esunrise\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003etext\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enot\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enull\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"n\"\u003esunset\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003etext\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enot\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enull\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"p\"\u003e);\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"k\"\u003eCREATE\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003eTABLE\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"n\"\u003esuccessful_checks\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003etext\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enot\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enull\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003ereferences\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"n\"\u003elondon\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"p\"\u003e),\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"n\"\u003ekind\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003etext\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enot\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enull\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"k\"\u003eprimary\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003ekey\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003ekind\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"p\"\u003e),\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"k\"\u003echeck\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003ekind\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003ein\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;sunrise\u0026#39;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;sunset\u0026#39;\u003c/span\u003e\u003cspan class=\"p\"\u003e))\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"p\"\u003e);\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThis second table exists so that you can have, say, Keyboard Maestro automatically switch between light and dark modes, but also not have it override any manual change you make after that within the check period (for instance, if it\u0026rsquo;s a cloudy day and you want to keep dark mode on, so you manually re-enable it).\u003c/p\u003e\n\u003cp\u003eHere\u0026rsquo;s \u003ccode\u003echeck-sunrise.sql\u003c/code\u003e (\u003ccode\u003echeck-sunset.sql\u003c/code\u003e is similar):\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sql\" data-lang=\"sql\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003eselect\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"n\"\u003etime\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;now\u0026#39;\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"o\"\u003e\u0026gt;=\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"n\"\u003etime\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003esunrise\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003eas\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;sun has risen\u0026#34;\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"k\"\u003efrom\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"n\"\u003elondon\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"k\"\u003ewhere\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;now\u0026#39;\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"k\"\u003eand\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003enot\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"k\"\u003eexists\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e        \u003c/span\u003e\u003cspan class=\"k\"\u003eselect\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e            \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e        \u003c/span\u003e\u003cspan class=\"k\"\u003efrom\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e            \u003c/span\u003e\u003cspan class=\"n\"\u003esuccessful_checks\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e        \u003c/span\u003e\u003cspan class=\"k\"\u003ewhere\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e            \u003c/span\u003e\u003cspan class=\"n\"\u003ekind\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;sunrise\u0026#39;\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e            \u003c/span\u003e\u003cspan class=\"k\"\u003eand\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"nb\"\u003edate\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s1\"\u003e\u0026#39;now\u0026#39;\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e    \u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"p\"\u003e;\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eNote that it only returns true (well, 1) if there hasn\u0026rsquo;t already been a successful (ie one causing a change) sunrise check today.\u003c/p\u003e\n\u003cp\u003eA fair question is: \u0026ldquo;Why keep checking if you\u0026rsquo;ve already passed sunrise or sunset?\u0026rdquo; And the answer is: mostly so I don\u0026rsquo;t have to worry about the procedure for disabling and enabling the relevant Keyboard Maestro macros. Instead, I can have the macro run at a set interval between the earliest and latest sunrise/sunset times, and be confident it won\u0026rsquo;t change anything if it\u0026rsquo;s already run successfully. Here\u0026rsquo;s the sunrise macro itself:\u003c/p\u003e\n\u003cp\u003e\u003cimg src=\"keyboard-maestro-sunrise.png\" alt=\"A screenshot of the sunrise macro in Keyboard Maestro\"\u003e\u003c/p\u003e\n\u003cp\u003eAs a convenience, there\u0026rsquo;s an SQL script in the repo to find when these times are from the database file.\u003c/p\u003e\n\u003cp\u003eOne other convenience is a LaunchBar action that shows duration between the current time and today\u0026rsquo;s sunrise and sunset:\u003c/p\u003e\n\u003cp\u003e\u003cvideo src=\"launchbar-sunrise-sunset.mp4\" controls muted playsinline\u003e\u003c/p\u003e\n\u003cp\u003eYou\u0026rsquo;ll have to create the actual action yourself through LaunchBar\u0026rsquo;s Action Editor, but the file \u003ca href=\"https://github.com/robjwells/sunrise-sunset/blob/main/launchbar-sunrise-sunset.sh\"\u003e\u003ccode\u003elaunchbar-sunrise-sunset.sh\u003c/code\u003e\u003c/a\u003e can be pasted in to the default shell script in a new action, with of course the database file path and table names adjusted.\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2022/12/typinator-is-on-sale/","url":"https://www.robjwells.com/2022/12/typinator-is-on-sale/","title":"Typinator is on sale","date_published":"2022-12-09T11:37:57Z","content_html":"\u003cp\u003eLong time! Lots to talk about, some point soon.\u003c/p\u003e\n\u003cp\u003eIn the mean time, \u003ca href=\"https://www.ergonis.com/typinator\"\u003eTypinator\u003c/a\u003e is currently on sale (along with all ergonis\nsoftware), at 30% off, to celebrate the snazzy new ergonis website.\u003c/p\u003e\n\u003cp\u003eI\u0026rsquo;ve much more to say about Typinator, but in short: I was a TextExpander user\nfor a long time, but grew hesitant to use it because it was slow and awkward.\u003c/p\u003e\n\u003cp\u003eTypinator is not! It\u0026rsquo;s fast and flexible. I switched in March 2021 and I use it\na lot more than I did TextExpander, and creating new snippets is\nstraightforward and pleasant. The snippet search is also really fast and\nresponsive.\u003c/p\u003e\n\u003cp\u003eThe snippet language differs from TextExpander\u0026rsquo;s, but it\u0026rsquo;s easy to get used to.\u003c/p\u003e\n\u003cp\u003e\u003ca href=\"https://www.ergonis.com/typinator\"\u003eDo give it a try!\u003c/a\u003e\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2022/01/populating-the-books-list-with-awk/","url":"https://www.robjwells.com/2022/01/populating-the-books-list-with-awk/","title":"Populating the books list with AWK","date_published":"2022-01-20T20:40:28Z","content_html":"\u003cp\u003eI read \u003ca href=\"https://www.robjwells.com/books/\"\u003e48 books\u003c/a\u003e last year, which is a lot for me. At least part of\nthat, I think, was to keep my mind in gear in a fairly healthy way, when I\ndidn’t feel like engaging with the world (poor mental health — thanks\nonce-in-a-century pandemic!)\u003c/p\u003e\n\u003cp\u003eWhat I did \u003cem\u003enot\u003c/em\u003e do, though, is keep the \u003ca href=\"https://www.robjwells.com/books/\"\u003elist of books\u003c/a\u003e up to date,\nnot since spring 2020.\u003c/p\u003e\n\u003cp\u003eSince I’ve been playing around with AWK a little, and had a big TSV file\ncontaining the details for each of the books, I thought I\u0026rsquo;d pair the two to\nfill out the missing files.\u003c/p\u003e\n\u003cp\u003eI use \u003ca href=\"https://gohugo.io\"\u003eHugo\u003c/a\u003e to generate this site, and each book is represented by a\nmarkdown file containing a metadata block with (at least) the title, author’s\nname, and the date on which I finished reading it. For example:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-markdown\" data-lang=\"markdown\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e---\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003etitle: \u0026#34;Empire of Pain\u0026#34;\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003eauthor: \u0026#34;Patrick Radden Keefe\u0026#34;\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003efinish-date: 2022-01-01\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e---\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"ge\"\u003e_Short review to come!_\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAWK is great for processing lines of text, but it’s missing some of the library\nfunctions you’d like for working with the filesystem. The book files are stored\nin this directory structure, where the markdown files are under each year\nfolder:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003econtent/books\n├── 2019\n├── 2020\n├── 2021\n└── 2022\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eThose year folders might not exist (\u003cem\u003edid not\u003c/em\u003e for 2021 and 2022). I could have\njust created them by hand, but then we’d miss out on some yak-shaving.\u003c/p\u003e\n\u003cp\u003eThe path to the markdown file is constructed using the book\u0026rsquo;s title (for the\nmarkdown filename itself) and finish date (for the year directory). We can wrap\nthe \u003ccode\u003edirname\u003c/code\u003e Unix utility:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-awk\" data-lang=\"awk\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e1\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kd\"\u003efunction\u003c/span\u003e \u003cspan class=\"nx\"\u003edirname\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003epath\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e2\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003ecmd\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;dirname \u0026#34;\u003c/span\u003e \u003cspan class=\"nx\"\u003epath\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e3\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003ecmd\u003c/span\u003e \u003cspan class=\"o\"\u003e|\u0026amp;\u003c/span\u003e \u003cspan class=\"kr\"\u003egetline\u003c/span\u003e \u003cspan class=\"nx\"\u003eresult\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e4\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"kr\"\u003eclose\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003ecmd\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e5\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"nx\"\u003eresult\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e6\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe funny \u003ccode\u003e|\u0026amp;\u003c/code\u003e operator on line 3 executes the command in \u003ccode\u003ecmd\u003c/code\u003e and then\n\u003ccode\u003egetline\u003c/code\u003e stores the first line of the output (there\u0026rsquo;s just one for \u003ccode\u003edirname\u003c/code\u003e) in\n\u003ccode\u003eresult\u003c/code\u003e. We call \u003ccode\u003eclose\u003c/code\u003e on the command string to release the associated file\ndescriptor.\u003c/p\u003e\n\u003cp\u003eLet’s wrap our wrapper in a function that just makes sure the directory exists,\nusing \u003ccode\u003emkdir -p\u003c/code\u003e, to make our lives easier:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-awk\" data-lang=\"awk\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e 8\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kd\"\u003efunction\u003c/span\u003e \u003cspan class=\"nx\"\u003eensure_dir\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003epath\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e 9\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"kr\"\u003esystem\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;mkdir -p \u0026#34;\u003c/span\u003e \u003cspan class=\"nx\"\u003edirname\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003epath\u003c/span\u003e\u003cspan class=\"p\"\u003e))\u003c/span\u003e  \n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e10\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eNow we can think about the filename itself. It doesn\u0026rsquo;t have to be anything\nparticular, but I like to keep mine \u003ccode\u003ereally-simple-like-this.md\u003c/code\u003e. We’re using\nthe title of the books as the filenames, so the key is just to strip out\nnon-word characters:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-awk\" data-lang=\"awk\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e12\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kd\"\u003efunction\u003c/span\u003e \u003cspan class=\"nx\"\u003esafe_name\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003estring\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e13\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"nx\"\u003eremove_non_word_characters\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e \u003cspan class=\"nx\"\u003eremove_apostrophes\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e \u003cspan class=\"kr\"\u003etolower\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e \u003cspan class=\"nx\"\u003estring\u003c/span\u003e \u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e14\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e15\u003c/span\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e16\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kd\"\u003efunction\u003c/span\u003e \u003cspan class=\"nx\"\u003eremove_apostrophes\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003estring\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e17\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kr\"\u003egensub\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"sr\"\u003e/[\u0026#39;’]/\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;g\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"nx\"\u003estring\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e18\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e19\u003c/span\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e20\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kd\"\u003efunction\u003c/span\u003e \u003cspan class=\"nx\"\u003eremove_non_word_characters\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003estring\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e21\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kr\"\u003egensub\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"sr\"\u003e/\\W+/\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;-\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;g\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"nx\"\u003estring\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e22\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe \u003ccode\u003eremove_apostrophes\u003c/code\u003e function is not strictly necessary but ensures titles\nsuch as \u003cem\u003eHitler’s Army\u003c/em\u003e don’t become \u003ccode\u003ehitler-s-army\u003c/code\u003e when they go through\n\u003ccode\u003eremove_non_word_characters\u003c/code\u003e.\u003c/p\u003e\n\u003cp\u003eWith that setup done, we can move on to the meat of the file, the lone\npattern-action statement:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-awk\" data-lang=\"awk\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e24\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"sr\"\u003e/^(2020-(0[6-9]|1[0-2])|202[12])/\u003c/span\u003e \u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e25\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003epath_template\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;content/books/%s/%s.md\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e26\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003eyear\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"kr\"\u003esubstr\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"o\"\u003e$\u003c/span\u003e\u003cspan class=\"mi\"\u003e1\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"mi\"\u003e0\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"mi\"\u003e4\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e27\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003etitle_for_path\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"nx\"\u003esafe_name\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"o\"\u003e$\u003c/span\u003e\u003cspan class=\"mi\"\u003e2\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e28\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003epath\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"kr\"\u003esprintf\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003epath_template\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"nx\"\u003eyear\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"nx\"\u003etitle_for_path\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e29\u003c/span\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e30\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003econtent_template\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"p\"\u003e(\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e31\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;---\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e32\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;title: \\\u0026#34;%s\\\u0026#34;\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e33\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;author: \\\u0026#34;%s\\\u0026#34;\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e34\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;finish-date: %s\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e35\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;---\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e36\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;\\n\u0026#34;\u003c/span\u003e \u003cspan class=\"err\"\u003e\\\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e37\u003c/span\u003e\u003cspan class=\"cl\"\u003e        \u003cspan class=\"s2\"\u003e\u0026#34;_Short review to come!_\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e38\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003econtent\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"kr\"\u003esprintf\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003econtent_template\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"o\"\u003e$\u003c/span\u003e\u003cspan class=\"mi\"\u003e2\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"o\"\u003e$\u003c/span\u003e\u003cspan class=\"mi\"\u003e3\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"o\"\u003e$\u003c/span\u003e\u003cspan class=\"mi\"\u003e1\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e39\u003c/span\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e40\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nx\"\u003eensure_dir\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nx\"\u003epath\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e41\u003c/span\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"kr\"\u003eprint\u003c/span\u003e \u003cspan class=\"nx\"\u003econtent\u003c/span\u003e \u003cspan class=\"o\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"nx\"\u003epath\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"ln\"\u003e42\u003c/span\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eWe match lines that start with dates representing June 2020 (the last update\nbeing in May 2020) through 2022. Next, we pull out the year from the date\n(field 1), and munge the book title (field 2), before formatting them into the\npath template with the built-in \u003ccode\u003esprintf\u003c/code\u003e function.\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003econtent_template\u003c/code\u003e is just the markdown you saw earlier in the post, again\nformatted using \u003ccode\u003esprintf\u003c/code\u003e and this time involving the author name (field 3).\u003c/p\u003e\n\u003cp\u003eWe ensure the directory exists for the target markdown file, and just print the\nformatted template via redirection into the file.\u003c/p\u003e\n\u003cp\u003eSo, would I use AWK over Python for things like this in the future? Maybe. The\ndisadvantage with AWK is that it’s missing built-in tools for working with\nthe system, and the work done above would be easier with, say, \u003ca href=\"https://docs.python.org/3.10/library/pathlib.html#module-pathlib\"\u003ePython’s\npathlib module\u003c/a\u003e. Or, then again, I could have just done \u003ccode\u003emkdir 2021 2022\u003c/code\u003e — but I wanted to get a feel for calling tools from AWK.\u003c/p\u003e\n\u003cp\u003eI think this tool is perhaps just on the edge where it could go either way.\nPython has more tools available, but AWK is so focussed on text processing that\nit does some of that boring work you’d have to do manually in Python.\u003c/p\u003e\n\u003cp\u003eIt was fun, and I look forward to using AWK for more in the future. (I’ve\nbought \u003ca href=\"https://archive.org/details/pdfy-MgN0H1joIoDVoIC7\"\u003ethe book\u003c/a\u003e second-hand.)\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2022/01/ejecting-all-disks/","url":"https://www.robjwells.com/2022/01/ejecting-all-disks/","title":"Ejecting all disks","date_published":"2022-01-09T20:48:27Z","content_html":"\u003cp\u003eDavid Sparks posted this week about \u003ca href=\"https://www.macsparky.com/blog/2022/01/using-keyboard-maestro-and-applescript-to-eject-external-drives/\"\u003eejecting disks using AppleScript\nand Keyboard Maestro\u003c/a\u003e. I’m grateful to David for posting\nabout this — it’s something I’ve needed for a while but hadn’t got\naround to doing. Currently I plug my laptop into several external drives\nwhen I’m sat at my desk, and have to eject them before taking it\nsomewhere more relaxed.\u003c/p\u003e\n\u003cp\u003eBut David’s script didn’t quite work for me in that a blunt \u003ccode\u003eeject the disks\u003c/code\u003e command to the Finder will attempt to eject all the disks that\nare considered “ejectable”. For me, this includes APFS snapshots that\naren’t obviously visible in the user interface, where trying to eject\nthem pops up a dialog referencing \u0026ldquo;Macintosh HD – Data@snap…\u0026rdquo;.\u003c/p\u003e\n\u003cp\u003eMy alternative is to filter out any drives that contain \u0026ldquo;Macintosh HD\u0026rdquo;\nin their displayed name:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-applescript\" data-lang=\"applescript\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003etell\u003c/span\u003e \u003cspan class=\"nb\"\u003eapplication\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;Finder\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nv\"\u003eeject\u003c/span\u003e \u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"nb\"\u003eevery\u003c/span\u003e \u003cspan class=\"nv\"\u003edisk\u003c/span\u003e \u003cspan class=\"nb\"\u003ewhere\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;Macintosh HD\u0026#34;\u003c/span\u003e \u003cspan class=\"ow\"\u003eis not\u003c/span\u003e \u003cspan class=\"k\"\u003ein\u003c/span\u003e \u003cspan class=\"k\"\u003eits\u003c/span\u003e \u003cspan class=\"nv\"\u003edisplayed\u003c/span\u003e \u003cspan class=\"na\"\u003ename\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003eend\u003c/span\u003e \u003cspan class=\"k\"\u003etell\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eNote that it’s important to use the \u003cem\u003edisplayed name\u003c/em\u003e property as the\nAPFS snapshots have UUIDs as their name property.\u003c/p\u003e\n\u003cp\u003eI’ve also stripped out David’s error handling, as I find that if there’s\nan error ejecting the disk it’s usually communicated by the Finder\nitself rather than via an error code visible to AppleScript.\u003c/p\u003e\n\u003cp\u003eThis doesn’t tackle the problem of the Finder refusing to eject a drive\ncontaining opened files, even if those files are opened by (from the\nuser’s perspective) unimportant background commands. I spent a chunk of\nthe morning mulling this over (and looking at \u003ccode\u003elsof\u003c/code\u003e’s inscrutable man\npage!), and think I might try to run the output of \u003ccode\u003elsof -F\u003c/code\u003e through AWK\nto enable a prompt-to-terminate interface. This is particularly\nimportant for me as I keep my music and photo libraries on an external\ndrive; the photo library in particular is often opened by background\nsystem tasks, I think as iCloud syncs across new photos from my phone.\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2021/02/how-to-wrap-bc-to-do-calculations/","url":"https://www.robjwells.com/2021/02/how-to-wrap-bc-to-do-calculations/","title":"How to wrap bc to do calculations","date_published":"2021-02-23T18:14:00Z","content_html":"\u003caside class=\"flag\"\u003e\n    \u003cp\u003eThis was \u003ca href=\"https://github.com/robjwells/til/blob/main/shell/202102230701%20How%20to%20wrap%20bc%20to%20do%20calculations.md\"\u003eoriginal published as a TIL\u003c/a\u003e but I thought it was interesting enough to post on its own.\u003c/p\u003e\n    \u003cp\u003eFunnily enough, the planets seem to have aligned and \u003ca href=\"https://leancrew.com/all-this/2021/02/some-bc-stuff/\"\u003eDr Drang has also written about \u003ccode\u003ebc\u003c/code\u003e today\u003c/a\u003e.\u003c/p\u003e\n\u003c/aside\u003e\n\u003chr\u003e\n\u003cp\u003eI find \u003ca href=\"https://www.gnu.org/software/bc/\"\u003e\u003ccode\u003ebc\u003c/code\u003e\u003c/a\u003e trips me up when I try to calculations at the shell, so I wrapped it.\u003c/p\u003e\n\u003cp\u003eThere are two hazards for me:\u003c/p\u003e\n\u003col\u003e\n\u003cli\u003eFailure to handle expressions without a final newline.\u003c/li\u003e\n\u003cli\u003eInteger division.\u003c/li\u003e\n\u003c/ol\u003e\n\u003ch3 id=\"final-newline-missing\"\u003eFinal newline missing\u003c/h3\u003e\n\u003cp\u003eNo 1 often occurs when I produce a list of numbers in \u003ca href=\"https://www.barebones.com/products/bbedit/\"\u003eBBEdit\u003c/a\u003e, usually extracted and transformed via regexes.\u003c/p\u003e\n\u003cp\u003eFor example, we get a PDF receipt from Sainbury’s for our online shopping, and to work out my share I like to edit the text from \u003ca href=\"https://linux.die.net/man/1/pdftotext\"\u003e\u003ccode\u003epdftotext\u003c/code\u003e\u003c/a\u003e in BBEdit, then run the line totals through a pipeline to calculate the total.\u003c/p\u003e\n\u003cp\u003eThat used to look like this:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ pbpaste \u003cspan class=\"p\"\u003e|\u003c/span\u003e paste -s -d \u003cspan class=\"s1\"\u003e\u0026#39;+\u0026#39;\u003c/span\u003e - \u003cspan class=\"p\"\u003e|\u003c/span\u003e bc\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"m\"\u003e609\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003ccode\u003epbpaste\u003c/code\u003e pastes the macOS system clipboard, \u003ca href=\"https://linux.die.net/man/1/paste\"\u003e\u003ccode\u003epaste\u003c/code\u003e\u003c/a\u003e joins those lines with \u003ccode\u003e+\u003c/code\u003e, and \u003ccode\u003ebc\u003c/code\u003e (should!) print the total.\u003c/p\u003e\n\u003cp\u003eExcept this happens if you’re missing a final newline:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ pbpaste \u003cspan class=\"p\"\u003e|\u003c/span\u003e paste -s -d \u003cspan class=\"s1\"\u003e\u0026#39;+\u0026#39;\u003c/span\u003e - \u003cspan class=\"p\"\u003e|\u003c/span\u003e bc\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"o\"\u003e(\u003c/span\u003estandard_in\u003cspan class=\"o\"\u003e)\u003c/span\u003e 1: parse error\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch2 id=\"integer-division\"\u003eInteger division\u003c/h2\u003e\n\u003cp\u003eAnd secondly, with the default settings, \u003ccode\u003ebc\u003c/code\u003e does integer division:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ bc \u003cspan class=\"o\"\u003e\u0026lt;\u0026lt;\u0026lt;\u003c/span\u003e 9/2\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"m\"\u003e4\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eWhich can be fixed by setting \u003ccode\u003escale\u003c/code\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ bc \u003cspan class=\"o\"\u003e\u0026lt;\u0026lt;\u0026lt;\u003c/span\u003e \u003cspan class=\"s1\"\u003e\u0026#39;scale=2; 9/2\u0026#39;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e4.50\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e(\u003ccode\u003e\u0026lt;\u0026lt;\u0026lt;\u003c/code\u003e starts a “here-string”, which you can learn more about at \u003ccode\u003eman -P \u0026quot;less -p '\u0026lt;\u0026lt;\u0026lt;'\u0026quot; zshmisc\u003c/code\u003e, which will take you directly to the right part of the \u003ccode\u003ezshmisc\u003c/code\u003e man page. Search the \u003ccode\u003ebash\u003c/code\u003e man page if you’re using \u003ccode\u003ebash\u003c/code\u003e, but it’s significantly more terse than the \u003ccode\u003ezsh\u003c/code\u003e explanation.)\u003c/p\u003e\n\u003ch3 id=\"wrapping-bc\"\u003eWrapping \u003ccode\u003ebc\u003c/code\u003e\u003c/h3\u003e\n\u003cp\u003eSo I wrote a simple wrapper script, \u003ccode\u003ecalc\u003c/code\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"cp\"\u003e#!/usr/local/bin/zsh\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"cp\"\u003e\u003c/span\u003e\u003cspan class=\"nb\"\u003eset\u003c/span\u003e -euo pipefail\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# Read expression from $1 or stdin.\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003eif\u003c/span\u003e \u003cspan class=\"o\"\u003e[\u003c/span\u003e \u003cspan class=\"nv\"\u003e$#\u003c/span\u003e -ge \u003cspan class=\"m\"\u003e1\u003c/span\u003e \u003cspan class=\"o\"\u003e]\u003c/span\u003e\u003cspan class=\"p\"\u003e;\u003c/span\u003e \u003cspan class=\"k\"\u003ethen\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nv\"\u003eexpression\u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"nv\"\u003e$1\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003eelse\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"nb\"\u003eread\u003c/span\u003e expression \u003cspan class=\"o\"\u003e||\u003c/span\u003e \u003cspan class=\"nb\"\u003etrue\u003c/span\u003e  \u003cspan class=\"c1\"\u003e# Force successful exit\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003efi\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# Default scale to 3 (.123) if $2 is not given.\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003ebc \u003cspan class=\"o\"\u003e\u0026lt;\u0026lt;\u0026lt;\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;scale=\u003c/span\u003e\u003cspan class=\"si\"\u003e${\u003c/span\u003e\u003cspan class=\"nv\"\u003e2\u003c/span\u003e\u003cspan class=\"k\"\u003e:-\u003c/span\u003e\u003cspan class=\"nv\"\u003e3\u003c/span\u003e\u003cspan class=\"si\"\u003e}\u003c/span\u003e\u003cspan class=\"s2\"\u003e; \u003c/span\u003e\u003cspan class=\"nv\"\u003e$expression\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe conditional in the middle just handles being called in a pipe, with the expression coming on standard input, or having the expression provided as the first argument. The \u003ccode\u003e|| true\u003c/code\u003e is necessary after \u003ccode\u003eread\u003c/code\u003e as \u003ca href=\"https://stackoverflow.com/questions/40547032/bash-read-returns-with-exit-code-1-even-though-it-runs-as-expected\"\u003e\u003ccode\u003eread\u003c/code\u003e only exits with a success code if it encounters EOF\u003c/a\u003e, and since I have \u003ccode\u003e-e\u003c/code\u003e set in the file, that would cause the whole script to exit.\u003c/p\u003e\n\u003cp\u003eI set the scale to 3 by default (showing 3 decimal places), but you can configure that with the second argument.\u003c/p\u003e\n\u003cp\u003eUsing a here-string for \u003ccode\u003ebc\u003c/code\u003e’s input means that it should work whether or not there is a final newline.\u003c/p\u003e\n\u003cp\u003eThe use of \u003ccode\u003eread\u003c/code\u003e also allows the use of \u003ccode\u003ecalc\u003c/code\u003e by itself to print the result of one expression, for example:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ calc\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"o\"\u003e(\u003c/span\u003e\u003cspan class=\"m\"\u003e2\u003c/span\u003e + 2\u003cspan class=\"o\"\u003e)\u003c/span\u003e / \u003cspan class=\"o\"\u003e(\u003c/span\u003e\u003cspan class=\"m\"\u003e10\u003c/span\u003e / 2\u003cspan class=\"o\"\u003e)\u003c/span\u003e↩\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e.800\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003ch3 id=\"wrapping-summation\"\u003eWrapping summation\u003c/h3\u003e\n\u003cp\u003eI use the “sum this list of numbers” pipeline fairly often, and it’s easy to pull out into its own command. I’m just using an alias, which I’ve added to my \u003ccode\u003e.zshrc\u003c/code\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-sh\" data-lang=\"sh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"nb\"\u003ealias\u003c/span\u003e ∑\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;paste -s -d \u0026#39;+\u0026#39; - | calc\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e\u003ca href=\"https://linux.die.net/man/1/sum\"\u003e\u003ccode\u003esum\u003c/code\u003e\u003c/a\u003e is already taken, so \u003ca href=\"https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation\"\u003e∑\u003c/a\u003e seemed appropriate. On macOS you can type it with ⌥w.\u003c/p\u003e\n\u003cp\u003eSo my pipeline from before just becomes \u003ccode\u003epbpaste | ∑\u003c/code\u003e.\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2020/07/give-pythons-static-typing-a-go/","url":"https://www.robjwells.com/2020/07/give-pythons-static-typing-a-go/","title":"Give Python’s static typing a go","date_published":"2020-07-08T06:00:00+01:00","content_html":"\u003cp\u003eWhile my go-to language is still Python, at \u003ca href=\"https://www.dcs.bbk.ac.uk/\"\u003euniversity\u003c/a\u003e I’ve also used Java and C#.\nI have to say, there was a huge amount to like — much to my surprise initially!\u003c/p\u003e\n\u003cp\u003ePreviously I’d kind of written off Java as being a \u003ca href=\"https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition\"\u003eboring language for boring businesspeople\u003c/a\u003e.\nThere are certainly elements of that (\u003ccode\u003epackage com.robjwells.MyPackage;\u003c/code\u003e in \u003ccode\u003esrc/main/java/com/robjwells/MyPackage.java\u003c/code\u003e) but there’s a lot to like.\nStreams are great, lambdas are great, the standard library is great (though not without its rough edges, such as the repeated attempts at date and time), and the tooling is great (I have actually come round to really like \u003ca href=\"https://www.jetbrains.com/idea/\"\u003eIntelliJ IDEA\u003c/a\u003e — certainly buying a full licence when my academic licence expires).\nPlus, it’s really coming along at a clip now with the shorter release cycle.\u003c/p\u003e\n\u003cp\u003eAnd C# was an even more pleasant surprise. It’s easy to get the impression that it’s “Microsoft’s Java”, but that’s really selling it short.\n\u003ca href=\"https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/\"\u003eLINQ\u003c/a\u003e is a joy that makes \u003ca href=\"https://www.youtube.com/watch?v=ei71YpmfRX4\"\u003ecomprehensions in Python\u003c/a\u003e (of which I am a huge fan!) seem… just a bit clunky. But C# is full of features, not just LINQ, that make working in it a real pleasure. (I will say, though, that at least on the Mac \u003ca href=\"https://www.jetbrains.com/rider/\"\u003eRider\u003c/a\u003e is a far superior editor than Visual Studio.)\u003c/p\u003e\n\u003cp\u003eAll of this is to say, in a round-about way, that those \u003cem\u003eweird verbose enterprise-y languages\u003c/em\u003e have a lot going for them — even in the still small-scale things that I’m working on where without this exposure I would just use Python.\u003c/p\u003e\n\u003cp\u003eNow, I still \u003cem\u003eam\u003c/em\u003e using Python, but there are things that I miss.\nChief among them in Python is having the type system actively help me out. \u003ca href=\"https://www.executeprogram.com/blog/porting-to-typescript-solved-our-api-woes\"\u003eGary Bernhardt writes a bit about this in the context of Ruby and TypeScript\u003c/a\u003e. (Be warned there are trivial examples ahead.)\u003c/p\u003e\n\u003cp\u003eNow, I’m using \u003ca href=\"https://code.visualstudio.com/\"\u003eVSCode\u003c/a\u003e so actually the editor will step in and help you even if you do nothing to aid it. For instance, in the following situation:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# untyped.py v1\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"mi\"\u003e42\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003emain\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"n\"\u003eu\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"n\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e()\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eVSCode (using the \u003ca href=\"https://marketplace.visualstudio.com/items?itemName=ms-python.python\"\u003ePython extension\u003c/a\u003e) will correctly infer that the type of \u003ccode\u003eu\u003c/code\u003e is \u003ccode\u003eint\u003c/code\u003e. So let’s do something with that:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# untyped.py v2\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"mi\"\u003e42\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003en\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003en\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u003c/span\u003e \u003cspan class=\"mi\"\u003e11\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003emain\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"n\"\u003eu\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"n\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e())\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eVSCode still correctly infers \u003ccode\u003eu\u003c/code\u003e to be an \u003ccode\u003eint\u003c/code\u003e. Great! But let’s be clear: this is VSCode doing the work so that it can offer you handy things like code completion.\u003c/p\u003e\n\u003cp\u003eWhat happens if we have a change in requirements and we change our API… only we don’t catch everything so we end up with the following:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# untyped.py v3\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;42\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003en\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003en\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u003c/span\u003e \u003cspan class=\"mi\"\u003e11\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003emain\u003c/span\u003e\u003cspan class=\"p\"\u003e():\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"n\"\u003eu\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"n\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e())\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAt this point, VSCode gives up: \u003ccode\u003eu\u003c/code\u003e is an \u003ccode\u003eint\u003c/code\u003e or it’s a \u003ccode\u003estr\u003c/code\u003e. In fact it’s neither, because \u003ccode\u003edo_something()\u003c/code\u003e raises a \u003ccode\u003eTypeError\u003c/code\u003e so \u003ccode\u003eu\u003c/code\u003e is never assigned. This is “obvious” to a human reading the code, in this simple example, but it’s easy to imagine a complex system where the types get out of line but the definitions are far apart from each other and the eventual call site.\u003c/p\u003e\n\u003cp\u003eThere are type checkers for Python, the main one being \u003ca href=\"http://www.mypy-lang.org/\"\u003emypy\u003c/a\u003e (which is great!). Can mypy help us here?\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e$ mypy untyped.py\nSuccess: no issues found in 1 source file\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eOh, success! Great.\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e$ python3 untyped.py\nTraceback (most recent call last):\n  File \u0026#34;untyped.py\u0026#34;, line 10, in \u0026lt;module\u0026gt;\n    main()\n  File \u0026#34;untyped.py\u0026#34;, line 8, in main\n    u = do_something(ultimate_answer())\n  File \u0026#34;untyped.py\u0026#34;, line 5, in do_something\n    return n - 11\nTypeError: unsupported operand type(s) for -: \u0026#39;str\u0026#39; and \u0026#39;int\u0026#39;\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eAh, no, no magic was performed and we still have a \u003ccode\u003eTypeError\u003c/code\u003e. It’s worth pointing out here that mypy is all about gradual typing — adding type annotations to your programs as and when. If there are no annotations, there are no checks performed. It’s not clairvoyant.\u003c/p\u003e\n\u003cp\u003eIf we switch back to VSCode, what if we try \u003ca href=\"https://devblogs.microsoft.com/python/announcing-pylance-fast-feature-rich-language-support-for-python-in-visual-studio-code/\"\u003eMicrosoft’s shiny new Pylance extension\u003c/a\u003e? In the basic type-checking mode it reports that the type of \u003ccode\u003eu\u003c/code\u003e is unknown which … is a step in the right direction? But no warnings.\u003c/p\u003e\n\u003cp\u003eIf we ratchet up the type-checking mode to strict it reports, with a bunch of red error squiggles, that the return type of \u003ccode\u003edo_something()\u003c/code\u003e is unknown and the type of \u003ccode\u003eu\u003c/code\u003e is unknown. We get a similar result if we pass the \u003ccode\u003e--strict\u003c/code\u003e flag to mypy, which essentially tells the type checker “forget about this gradual business” and attempts to check the whole file. This effectively fails, because we’ve done nothing to help it. Let’s do that now.\u003c/p\u003e\n\u003cp\u003eIn fact, very little is needed before Pylance starts to push you in the right direction, only this:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# typed.py v1 excerpt\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003en\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e \u003cspan class=\"nb\"\u003eint\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003en\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u003c/span\u003e \u003cspan class=\"mi\"\u003e11\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAfter which we’re rewarded with red squiggles underneath the call to \u003ccode\u003eultimate_answer()\u003c/code\u003e that provides the argument inline to \u003ccode\u003edo_something()\u003c/code\u003e. The message we get is interesting because it reveals something about the knowledge of the type-checker:\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003eArgument of type \u0026#34;Literal[\u0026#39;42\u0026#39;]\u0026#34; cannot be assigned to\nparameter \u0026#34;n\u0026#34; of type \u0026#34;int\u0026#34; in function \u0026#34;do_something\u0026#34;\n  \u0026#34;Literal[\u0026#39;42\u0026#39;]\u0026#34; is incompatible with \u0026#34;int\u0026#34;\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eWe haven’t typed \u003ccode\u003eultimate_answer()\u003c/code\u003e, but it knows that \u0026ldquo;42\u0026rdquo; can’t be treated as an \u003ccode\u003eint\u003c/code\u003e. Mypy needs a little more help to get there:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# typed.py v2 excerpt\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e()\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"nb\"\u003estr\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;42\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003en\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e \u003cspan class=\"nb\"\u003eint\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003en\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u003c/span\u003e \u003cspan class=\"mi\"\u003e11\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eSo, at this point we’ve said that \u003ccode\u003eultimate_answer()\u003c/code\u003e returns a string and that \u003ccode\u003edo_something()\u003c/code\u003e takes an integer. What does mypy think?\u003c/p\u003e\n\u003cpre tabindex=\"0\"\u003e\u003ccode\u003e\u0026gt; $ mypy typed.py\nSuccess: no issues found in 1 source file\n\n\u0026gt; $ mypy --strict typed.py\n[…snip…]\ntyped.py:8: error: Argument 1 to \u0026#34;do_something\u0026#34; has\n    incompatible type \u0026#34;str\u0026#34;; expected \u0026#34;int\u0026#34;\n[…snip…]\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eThis is an interesting situation because \u0026ldquo;normal\u0026rdquo; mode mypy reports that this is fine, no problems here. Strict mode complains — among other things — that the types don’t match. Finally, this is what we want.\u003c/p\u003e\n\u003cp\u003eBut why doesn’t “normal” mode mypy not see the problem? I think this is to do with what is considered a “typed context”. By using \u003ccode\u003e--strict\u003c/code\u003e we force everything to be a typed context, so we get a lot more warnings and errors from mypy. But without this, \u003ccode\u003emain()\u003c/code\u003e is not a typed context — it has no typed arguments, and no explicit return type, so “normal” mode mypy just skips over it.\u003c/p\u003e\n\u003cp\u003eThe strength of gradual typing is that if you don’t want to or aren’t ready to add type information, you don’t. But even in this toy example, the standard Python type-checker under its default settings does not pick up this “obvious” (to us!) type error. In \u003ca href=\"https://www.youtube.com/watch?v=ST33zDM9vOE\u0026amp;feature=youtu.be\"\u003eDustin Ingram’s Pycon talk about static typing\u003c/a\u003e he says you should use static typing everywhere — for a few reasons, but here we can see that failing to do so leaves a clear error undetected..\u003c/p\u003e\n\u003cp\u003eIt doesn’t take much to rectify that for mypy, just a return type on \u003ccode\u003emain()\u003c/code\u003e, yielding the following:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"c1\"\u003e# typed.py v3\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e()\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"nb\"\u003estr\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;42\u0026#34;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003en\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e \u003cspan class=\"nb\"\u003eint\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003en\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u003c/span\u003e \u003cspan class=\"mi\"\u003e11\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003emain\u003c/span\u003e\u003cspan class=\"p\"\u003e()\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"kc\"\u003eNone\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"n\"\u003eu\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"n\"\u003edo_something\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003eultimate_answer\u003c/span\u003e\u003cspan class=\"p\"\u003e())\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eAnd now \u003ccode\u003emypy typed.py\u003c/code\u003e gives the same error that strict mode did for the previous example. Adding the explicit return type to \u003ccode\u003emain()\u003c/code\u003e is honestly pretty useless, but now it opens up the definition of \u003ccode\u003emain()\u003c/code\u003e to be type checked, at which point the error is spotted.\u003c/p\u003e\n\u003chr\u003e\n\u003cp\u003eI wrote the title of this blog post before I really knew where I was going (I have a Beeminder deadline to hit!) so at this point it feels to me like I haven’t quite delivered on (why you should) “Give Python’s static typing a go”. Really we’re at “If you decide to use Python’s static typing you need to go all-in.” Which actually is something I do believe! I think the strictest settings are the most useful, but leaving something untyped leaves a hole for type errors to sneak through.\u003c/p\u003e\n\u003cp\u003eBut it is useful in itself. It’s unfamiliar and, honestly, a bit clunky in Python. (The dance for declaring a \u003ccode\u003eTypeVar\u003c/code\u003e for a generic function taking some type \u003ccode\u003eT\u003c/code\u003e is … Not Good and looks worse once you run your code through a formatter, with it then two lines away.)\u003c/p\u003e\n\u003cp\u003eBut thinking about types is thinking about design, and thinking about the contract that you’re willing to offer to the outside world. I’ve found that in Java and C# sometimes I’m ready to bound straight into defining a function … only to stop after realising that I haven’t really clarified what expectations I have of the outside world (parameter types) and what expectations the outside world has of me (return type).\u003c/p\u003e\n\u003cp\u003eHere’s a dead-obvious example from a simple exercise on \u003ca href=\"https://exercism.io/\"\u003eExercism\u003c/a\u003e (sorry for any spoilers but I should hope this one is straightforward to anyone with any knowledge of the \u003ccode\u003edatetime\u003c/code\u003e module!):\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"kn\"\u003efrom\u003c/span\u003e \u003cspan class=\"nn\"\u003edatetime\u003c/span\u003e \u003cspan class=\"kn\"\u003eimport\u003c/span\u003e \u003cspan class=\"n\"\u003edatetime\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"n\"\u003etimedelta\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"n\"\u003eGIGASECOND\u003c/span\u003e \u003cspan class=\"o\"\u003e=\u003c/span\u003e \u003cspan class=\"n\"\u003etimedelta\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003eseconds\u003c/span\u003e\u003cspan class=\"o\"\u003e=\u003c/span\u003e\u003cspan class=\"mi\"\u003e1_000_000_000\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eadd\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003emoment\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e \u003cspan class=\"n\"\u003edatetime\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e \u003cspan class=\"o\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"n\"\u003edatetime\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e    \u003cspan class=\"k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"n\"\u003emoment\u003c/span\u003e \u003cspan class=\"o\"\u003e+\u003c/span\u003e \u003cspan class=\"n\"\u003eGIGASECOND\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eNow, \u003ccode\u003eadd()\u003c/code\u003e is a bad name in general but fine in this confined case, but it’s the simple addition of the \u003ccode\u003edatetime\u003c/code\u003e annotations that make it clear what we’re handling here. You give a datetime, receive a datetime. Nothing fancy, but compare with the following signature:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-python\" data-lang=\"python\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"k\"\u003edef\u003c/span\u003e \u003cspan class=\"nf\"\u003eadd\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"n\"\u003emoment\u003c/span\u003e\u003cspan class=\"p\"\u003e):\u003c/span\u003e \u003cspan class=\"o\"\u003e...\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eIt’s concise, sure, but is the cost in understanding worth it? Explicitly annotating the types forces you to consider what the interface is and, in return, tools like mypy will give you a hand in finding bugs.\u003c/p\u003e\n\u003cp\u003eAnyway, give \u003ca href=\"https://www.youtube.com/watch?v=ST33zDM9vOE\u0026amp;feature=youtu.be\"\u003eDustin Ingram’s talk\u003c/a\u003e a watch (check out those t-shirts!) as it’s informative, straightforward and short. Do check out the \u003ca href=\"https://mypy.readthedocs.io/en/stable/index.html\"\u003emypy documentation\u003c/a\u003e, as there’s plenty of descriptive information in there beyond the interface to the command line tool and the \u003ccode\u003etyping\u003c/code\u003e module. \u003ca href=\"https://www.youtube.com/watch?v=7ZbwZgrXnwY\"\u003eJukka Lehtosalo and David Fisher spoke in some detail at Pycon 2017 about mypy\u003c/a\u003e, and I highly recommend \u003ca href=\"https://dropbox.tech/application/our-journey-to-type-checking-4-million-lines-of-python\"\u003eJukka’s article on the Dropbox tech blog that looks at the history and practical aspects of mypy\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eIt’s clear also that this is the direction of travel for Python — there is a lot on the horizon that will make life easier (see \u003ca href=\"https://www.python.org/dev/peps/\"\u003ethe list of PEPs\u003c/a\u003e), and I’m particularly looking forward to seeing what becomes of \u003ca href=\"https://www.python.org/dev/peps/pep-0622/\"\u003ePEP 622\u003c/a\u003e as at the moment it looks like it will bring with it \u003ca href=\"https://fsharpforfunandprofit.com/posts/discriminated-unions/\"\u003esum types\u003c/a\u003e, even if it is a year or two off at this point.\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2020/06/scheduling-posts-in-hugo/","url":"https://www.robjwells.com/2020/06/scheduling-posts-in-hugo/","title":"Scheduling posts in Hugo","date_published":"2020-06-06T06:00:00+01:00","content_html":"\u003cp\u003eMy enthusiasm for writing posts here tends to come in reasonably short bursts, usually over a weekend, so I’ve taken to scheduling the two or three posts I might write over the following weeks.\u003c/p\u003e\n\u003cp\u003eIt’s reasonably straightforward to schedule publication with \u003ca href=\"https://gohugo.io/\"\u003eHugo\u003c/a\u003e by using the \u003ccode\u003epublishDate\u003c/code\u003e attribute in your post frontmatter. For instance, this is the frontmatter for this post right now:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-yaml\" data-lang=\"yaml\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"nt\"\u003etitle\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;Scheduling posts in Hugo\u0026#34;\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"nt\"\u003edate\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"ld\"\u003e2020-05-24T09:25:44\u003c/span\u003e\u003cspan class=\"m\"\u003e+01\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"m\"\u003e00\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"nt\"\u003epublishDate\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"ld\"\u003e2020-06-06T06:00:00\u003c/span\u003e\u003cspan class=\"m\"\u003e+01\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"m\"\u003e00\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"w\"\u003e\u003c/span\u003e\u003cspan class=\"nt\"\u003edraft\u003c/span\u003e\u003cspan class=\"p\"\u003e:\u003c/span\u003e\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"kc\"\u003etrue\u003c/span\u003e\u003cspan class=\"w\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe \u003ccode\u003edate\u003c/code\u003e attribute is filled in by Hugo when I create the post bundle skeleton. I tend to leave this as a marker for when I started writing a post (though I have changed it for posts that I start, leave for a while, and \u003ca href=\"/2020/05/keyboard-maestro-macro-to-insert-images-into-blog-posts/\"\u003efinish later\u003c/a\u003e).\u003c/p\u003e\n\u003cp\u003eThe \u003ccode\u003epublishDate\u003c/code\u003e attribute controls when the post is actually published. Hugo by default doesn’t build posts with this set in the future.\u003c/p\u003e\n\u003cp\u003eOne important change that I made from the defaults, though, is to define the handling of dates in my site-wide config file (\u003ccode\u003econfig.toml\u003c/code\u003e) like so:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-toml\" data-lang=\"toml\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e[\u003c/span\u003e\u003cspan class=\"nx\"\u003efrontmatter\u003c/span\u003e\u003cspan class=\"p\"\u003e]\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"nx\"\u003edate\u003c/span\u003e \u003cspan class=\"p\"\u003e=\u003c/span\u003e \u003cspan class=\"p\"\u003e[\u003c/span\u003e\u003cspan class=\"s2\"\u003e\u0026#34;publishDate\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;date\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e,\u003c/span\u003e \u003cspan class=\"s2\"\u003e\u0026#34;:default\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e]\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eWhat this means is that Hugo will prefer the \u003ccode\u003epublishDate\u003c/code\u003e as the date of the post, before falling back to the \u003ccode\u003edate\u003c/code\u003e attribute, and then resuming its default lookup, which is listed \u003ca href=\"https://gohugo.io/getting-started/configuration/#configure-dates\"\u003ein the Hugo documentation\u003c/a\u003e.\n(At the moment, the only other thing in the default lookup order is the file modification time, but mostly I include \u0026ldquo;:default\u0026rdquo; to be safe if this changes in the future.)\u003c/p\u003e\n\u003cp\u003eOtherwise, you might end up with a situation where you write “Post Future”, set a publish date in the future, then write “Post Now” and publish immediately, and when “Post Future” is published it will be shown as being published earlier than “Post Now” because its \u003ccode\u003edate\u003c/code\u003e is earlier. Changing the date lookup order in the config will preserve your deliberate schedule.\u003c/p\u003e\n\u003cp\u003e(Thanks to “n m” on StackOverflow who got me started \u003ca href=\"https://stackoverflow.com/questions/59655470/hugo-date-vs-publishdate/59760977#59760977\"\u003ewith this answer\u003c/a\u003e.)\u003c/p\u003e\n\u003cp\u003eObviously, just having a bunch of files with the dates set properly doesn’t mean your post will actually be published automatically at the right time.\nI have a script on my server that cron runs every 15 minutes that pulls from \u003ca href=\"https://github.com/robjwells/primaryunit\"\u003ethe GitHub repository\u003c/a\u003e and rebuilds the site.\u003c/p\u003e\n\u003cp\u003eThis set-up is made easy thanks to Hugo being a single binary, so it’s simple to install on the server, whereas \u003ca href=\"https://github.com/robjwells/majestic/\"\u003ebefore\u003c/a\u003e I would generally build the site locally and upload it to my server with rsync.\u003c/p\u003e\n\u003cp\u003eHugo can list posts that are scheduled in the future:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-zsh\" data-lang=\"zsh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ hugo list future\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003econtent/posts/2020/05/24/scheduling-posts-in-hugo/index.md,2020-06-06T06:00:00+01:00\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003econtent/posts/2020/05/24/keyboard-maestro-macro-to-insert-images-into-blog-posts/index.md,2020-05-30T06:00:00+01:00\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThis is a bit noisy, so here’s a sed one-liner to improve things slightly:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-zsh\" data-lang=\"zsh\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e$ hugo list future \u003cspan class=\"p\"\u003e|\u003c/span\u003e sed -E \u003cspan class=\"s1\"\u003e\u0026#39;s/^.+\\/([^\\/]+)\\/index.md,(.+)/\\2 - \\1/\u0026#39;\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e2020-06-06T06:00:00+01:00 - scheduling-posts-in-hugo\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e2020-05-30T06:00:00+01:00 - keyboard-maestro-macro-to-insert-images-into-blog-posts\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003e(This assumes that you’re using \u003ca href=\"https://gohugo.io/content-management/page-bundles/\"\u003epage bundles\u003c/a\u003e.)\u003c/p\u003e\n\u003chr\u003e\n\u003cp\u003eI actually now have a \u003ca href=\"https://www.beeminder.com/robjwells/blog\"\u003eBeeminder goal\u003c/a\u003e to ensure that I write a post a month — well, I was being lenient with myself so it’s every 31 days. It’s also set for a maximum 31 safe days, so I feel that scheduling once a week is enough to tamp down any short-term blog-mania while also not gaming the Beeminder goal too much by scheduling posts at 31-day intervals. (Perhaps this is a sign though that I can set the goal to have a shorter period, but at the moment I have enough commitments that I don’t feel comfortable doing so.)\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2020/05/keyboard-maestro-macro-to-insert-images-into-blog-posts/","url":"https://www.robjwells.com/2020/05/keyboard-maestro-macro-to-insert-images-into-blog-posts/","title":"Keyboard Maestro macro to insert images into blog posts","date_published":"2020-05-30T06:00:00+01:00","content_html":"\u003cp\u003eHere’s a quick Keyboard Maestro macro to make it easier to insert images into blog posts, or any other markdown or HTML document really. The details of the macro are set up to create a \u003ca href=\"https://gohugo.io/content-management/shortcodes/#figure\"\u003eHugo figure shortcode\u003c/a\u003e, but the Hugo-specific bits are just scaffolding and could be swapped out for whatever you need.\u003c/p\u003e\n\u003cp\u003eYou can \u003ca href=\"insert-hugo-figure.kmmacros\"\u003edownload the macro file here\u003c/a\u003e, but the whole thing ended up being a bit long so I’m not going to include the usual image of the whole macro (which is 1,965 pixels tall). Let’s step through it.\u003c/p\u003e\n\u003cfigure class=\"full-width no-border\"\u003e\u003ca href=\"macro-1-prompt-and-read.png\"\u003e\u003cimg src=\"macro-1-prompt-and-read.png\"\n         alt=\"A screenshot showing a portion of a Keyboard Maestro macro, prompting the user for a file and then reading it.\" width=\"551\" height=\"275\"/\u003e\u003c/a\u003e\u003cfigcaption\u003e\n            \u003cp\u003eThe first stage of the macro, prompting for the image file and then loading it onto a named clipboard.\u003c/p\u003e\n        \u003c/figcaption\u003e\n\u003c/figure\u003e\n\u003cp\u003eAfter selecting the image, we need to load it onto a named clipboard because Keyboard Maestro’s image actions generally work on the contents of a clipboard.\u003c/p\u003e\n\u003cfigure class=\"full-width no-border\"\u003e\u003ca href=\"macro-2-store-properties.png\"\u003e\u003cimg src=\"macro-2-store-properties.png\"\n         alt=\"A screenshot showing a portion of a Keyboard Maestro macro, reading image properties into variables.\" width=\"551\" height=\"312\"/\u003e\u003c/a\u003e\u003cfigcaption\u003e\n            \u003cp\u003eNext we extract needed properties from the image into Keyboard Maestro variables.\u003c/p\u003e\n        \u003c/figcaption\u003e\n\u003c/figure\u003e\n\u003cp\u003eThen we need to prompt the user to confirm the attributes of the figure.\u003c/p\u003e\n\u003cfigure class=\"full-width no-border\"\u003e\u003ca href=\"macro-3-prompt-attributes.png\"\u003e\u003cimg src=\"macro-3-prompt-attributes.png\"\n         alt=\"A screenshot showing a portion of a Keyboard Maestro macro, of a prompt to the user to confirm attributes for the figure to be inserted.\" width=\"551\" height=\"377\"/\u003e\u003c/a\u003e\u003cfigcaption\u003e\n            \u003cp\u003eThe prompt set-up.\u003c/p\u003e\n        \u003c/figcaption\u003e\n\u003c/figure\u003e\n\u003cfigure class=\"full-width no-border\"\u003e\u003ca href=\"macro-prompt.png\"\u003e\u003cimg src=\"macro-prompt.png\"\n         alt=\"A screenshot showing a Keyboard Maestro prompt asking for attributes to complete an HTML figure\" width=\"537\" height=\"267\"/\u003e\u003c/a\u003e\u003cfigcaption\u003e\n            \u003cp\u003eAnd the prompt itself.\u003c/p\u003e\n        \u003c/figcaption\u003e\n\u003c/figure\u003e\n\u003cp\u003eAny of these can be empty, so after assembling the shortcode text blank attributes are removed:\u003c/p\u003e\n\u003cfigure class=\"full-width no-border\"\u003e\u003ca href=\"macro-4-assemble-figure.png\"\u003e\u003cimg src=\"macro-4-assemble-figure.png\"\n         alt=\"A screenshot showing a portion of a Keyboard Maestro macro, creating the figure shortcode from provided attributes and using a regular expression to remove any empty attributes.\" width=\"551\" height=\"631\"/\u003e\u003c/a\u003e\n\u003c/figure\u003e\n\u003cp\u003eAnd then lastly the figure shortcode text is inserted by pasting, which is handy because it end up on the clipboard if anything goes wrong — like it did when I changed the focus when inserting the previous screenshot!\u003c/p\u003e\n"},{"id":"https://www.robjwells.com/2020/05/the-atom-a-love-affair/","url":"https://www.robjwells.com/2020/05/the-atom-a-love-affair/","title":"The Atom: A Love Affair","date_published":"2020-05-24T08:15:00+01:00","content_html":"\u003cp\u003eI watched \u003ca href=\"https://theatomfilm.com/\"\u003eThe Atom: A Love Affair\u003c/a\u003e last night, a new documentary about the history of nuclear power, and it’s really great. It charts the ups and downs of nuclear power, its promoters and opponents, and looks at where we are today. I think its great strength is that it sets out the surrounding context. It doesn’t take a side but gives a fair accounting (which \u003ca href=\"https://cnduk.org/campaigns/no-nuclear-power/\"\u003eI think leads to a clear conclusion\u003c/a\u003e, though I would, wouldn’t I?).\u003c/p\u003e\n\u003cp\u003eYou can \u003ca href=\"https://www.curzonhomecinema.com/film/watch-atom-a-love-affair-online\"\u003estream it online at Curzon Home Cinema\u003c/a\u003e, and I highly recommend it.\u003c/p\u003e\n\u003cp\u003eThere’s \u003ca href=\"https://www.youtube.com/watch?v=RqUnaEi9UgM\"\u003ea Q\u0026amp;A session held after the film’s premiere\u003c/a\u003e, and more info on \u003ca href=\"https://www.facebook.com/theatomfilm\"\u003ethe Facebook page\u003c/a\u003e. There’s also \u003ca href=\"https://www.bbc.co.uk/sounds/play/m000jg85\"\u003ean episode of Radio 3’s Sound of Cinema with the film’s composer Paul Honey\u003c/a\u003e.\u003c/p\u003e\n"}]}