Export a list of URLs from Safari Reading List

I’ve accumulated a lot of URLs in Safari’s Reading List. Problem is, trying to navigate a long list in the Safari is quite buggy – items move around randomly, I lose my place, and I’d really like more space than afforded by a single sidebar. Wouldn’t it be nice if I could pull all those URLs out of Safari, and get them in a standalone file?

A quick poke around Google didn’t turn up anything obvious, so I decided to write my own Python script for doing it.

Continue reading →

Email with a purpose

I’ve just finished reading Operation Fortitude, by Joshua Levine. Fortitude was an Allied deception operation during WWII, intended to convince the Nazis that Normandy was not the target of the D-Day landings. The book is a good account, although at times I found myself wanting more detail.

I was particularly struck by an early passage:

This [failed operation] proved to be a valuable learning experience for Clarke. His response was to formulate his first rule of strategic deception: to make your opponent act as you want him to. It doesn’t matter what he thinks. In this case the Italian commander was led to expect an attack on Somaliland, but Clarke hadn’t considered what he would do as a result.

The deceiver, he realised, had to get inside the mind of the enemy commander. On this occasion he had failed. In future Clarke would make a point of asking his commander: “What do you want the enemy to do?” And very often, to his surprise, the commander would be unable to answer.

This rule comes up throughout the book, and things go poorly when it isn’t followed.

It made me think about email. Too often, when we write emails, we fire them into the aether without considering what we’d like the recipient to do next. This can lead to vague and unfocused emails – we want something, we just don’t know what.

Clarke’s stragegy gives us a good approach to email: when writing a message, we should ask What do I want the recipient to do? And when you’ve got an answer, include it in the email itself.

Beyond PEP 8: Best practices for beautiful intelligible code

This is easily my favourite talk from PyCon 2015. Raymond Hettinger is an excellent speaker, and this talk makes an important point:

PEP 8 is the Python style guide, but like many style guides, it can be wrongly interpreted as an indicator of code quality. There’s more to good code than simply meeting the style guide. If you just want PEP 8 adherence, there are automated tools like flake8 and pylint that will do that checking for you. When a human is doing code review, they need to look beyond the style guide.

The focus of the talk is a code example that akes this point nicely. I won’t spoil the ending – but see if you can spot the elephant in the room. (I didn’t.)

I would highly recommend this talk to all programmers, even if you’ve never written Python.

Python and the BBC micro:bit

The BBC micro:bit is a tiny computer, intended for Year 7 students to use to learn about programming (being distributed free to schools next spring). It’s the spiritual successor to the BBC Micro. Here’s a picture from the BBC’s website; it’s about half the size of a credit card:

One of the main programming languages on the micro:bit is Python, or rather MicroPython, a version of Python 3 that’s optimised for microcontrollers. (The micro:bit has just 16K of RAM, so full Python is out of the question.) I got to play with a micro:bit at tonight’s meeting of the Cambridge Python User Group, and it’s a delightful piece of engineering.

For about an hour, we got to play with it – making the lights flash, trying the buttons, even playing music – and it’s just fun. Although there are only a handful of hardware features, there’s a lot of scope for ideas – there were at least a dozen different ideas that came up tonight. I particularly liked a mini-version of Snake and Pong played on the 5-by-5 screen, and some visualisations that played with the intensity of the LEDs.

There is something incredibly visceral about programming that enters the real world, having an effect beyond just text on a screen.

The software is equally remarkable. It has a very robust, stable REPL built on MicroPython, and it works very well. Inevitably, this isn’t a complete implementation of Python 3 – 256K of storage puts a stop to that – but what it has seems very well done. This isn’t a dumbed-down or toy language. It’s proper Python, just with a smaller standard library. I didn’t have to think about it at all – I just used it like I would on any other computer.

I don’t expect that everybody who uses this device will magically become a software developer, but I hope they have fun playing with it.

Thanks to Nicholas Tollervey for bringing the micro:bits for us to play with.

If you want to play with a micro:bit yourself, you should follow the python-uk mailing list. There are going to be a handful more meetups/events with micro:bits before they all return to the BBC. I’d highly recommend going if you have the chance!

Quick shell access for Docker containers

In the last few weeks, I’ve been spending a lot of time working with Docker containers. When developing containers, I often need to get shell access to the container, so that I can inspect its contents. This is a two-step process:

  1. Look up the container ID with docker ps.
  2. Shell into the container with docker exec -it <container ID> sh.

That’s fine, but wouldn’t it be nice if we could cut out the first step? It’s only short, but it gets tedious if you’re doing it regularly – this is the sort of boring task that we can solve with scripting.

Most of the time, I’m shelling into my most recently started container. So I wrote this short function, and put it in my bash_profile:

function docker-sh {
    if [[ $# -eq 0 ]]; then
        docker exec -it $(docker ps -l -q) sh
        docker exec -it $1 sh

If I invoke it without any arguments, it will shell into my most recent container:

$ docker-sh

But on the rare occasions I have another container in mind, I can specify it as an alternative, by supplying it as the first argument:

$ docker-sh 8d8122c10f60

Getting Started Testing, by Ned Batchelder

This PyCon talk is a great introduction to unit tests and mocking. It starts from having a common position – having no tests – and steps you through gradual improvements until you have a proper unit test framework. The second half is a clear, concise explanation of mocking.

If you’ve never done any testing, this is a really good place to start. If you already know how to test, it’s a good opportunity to revisit the fundamentals.

And even though this is a PyCon talk, it applies to all programming languages. The examples are Python, but the ideas apply everywhere.

[If you were at last week’s CamPUG meeting, this is the talk I mentioned.]

What does “Spotlight Suggestions” turn off?

One of the “features” of iOS 9 is news articles appearing in Spotlight searches. If you swipe to the search screen, and haven’t typed anything into the search field, it gets pre-populated with headlines. Lots of people (including me) think this is just a distraction, and have been looking for ways to turn it off.

In Settings, you can toggle what sort of results show up in Spotlight. For headlines, it turns out to be the relatively innocent-sounding switch “Spotlight Suggestions”. Turning off this Spotlight result will get rid of the news items; detailed instructions are on CGP Grey’s blog.

On the latest episode of Cortex, CGP was discussing this toggle with Myke Hurley. As part of their conversation, they were puzzling over the name of the label:

I don’t know why on earth this label is [Spotlight Suggestions], because as far as I can tell, if you turn it off, the only thing it removes is the news. Everything else it will still show you: it’ll still show you results from your documents […], it’ll still show you everything you have turned on. It just seems not to fill that screen with something if there are no results.

I started wondering as well. I toggled that switch as soon as I started running the iOS betas. What else is it turning off?

Near the bottom of the settings pane is a small link, “About Spotlight Suggestions & Privacy”. Tapping it brings up this explanation:

Spotlight Suggestions shows you suggestions from the web, your contacts, apps, nearby locations and media, including iTunes and the App Store – and even offers suggestions before you start typing.

If you disable Spotlight Suggestions, all of these get turned off. News is the most obvious example of Suggestions because it’s the only result that shows up on an empty search screen; all the others only appear when you start typing in a search.

I think – although I’m not certain – that it’s any result that can’t be looked up locally. Results that go via the network have privacy implications, which is why these results can be turned off, and probably why they’re all under the same toggle.

Below I’ve included a few screenshots to show the difference caused by this toggle.

Continue reading →

Apple quietly adopts HTTP/2

Amongst all the new features in iOS 9, I spotted one on Twitter that I hadn’t seen before:

There is nothing cooler than seeing my iOS9 devices report HTTP/2 from Safari @ http://http2bin.org. Welcome to the HTTP/2 future!
  — Cory Benfield (@lukasaoz) Wed Sep 16 2015 9:14 PM

It turns out that Cory blogged about this several months ago, but otherwise this change seems to have passed very quietly.

The status quo – HTTP/1.1 – is over fifteen years old. The web has changed a lot since then, and decisions made for the web of 1997 just don’t make sense today. Enter HTTP/2: a new protocol intended to replace HTTP/1.1, with a design that’s appropriate for the modern web. It’s been in the works for several years, and was finally published as an RFC in May.

I first heard of HTTP/2 in Cory’s talk at PyCon, and I thought it was an interesting technical idea. He makes a compelling case for the benefits, but I didn’t expect it to be adopted quickly. Until lots of browers could use it, who would rush to adopt it server-side?

With iOS 9, that calculus has changed.

It turns out that Apple announced support for HTTP/2 all the way back in June, at WWDC: Networking with NSURLSession (start at the 13 minute mark). The same session introduced App Transport Security (HTTPS by default for all apps), and I suspect that overshadowed this announcement. It doesn’t help that I’ve found almost no mention of it in Apple’s developer docs.

But the crux is this:

As of iOS 9 and El Capitan, all NSURLSession requests can support HTTP/2. (And if you’ve been running the developer seeds, you’ve had it for a while.)

Apple have been using this themselves: remote access to HomeKit devices via iCloud all goes via HTTP/2. And I wouldn’t be surprised if they start using it to push out software updates, given that CDNs were explicitly called out as an HTTP/2 provider in that session.

Even assuming a very conservative adoption rate, there are now tens of millions of new clients that support HTTP/2. That’s got to push the needle on server-side adoption (and guess what I’m planning for next weekend!).

HTTP is a fundamental part of the web, and HTTP/2 is a key part of its future. I’m incredibly pleased to see Apple driving it forward.

Review: Effective Python

I received a copy of Brett Slatkin’s Effective Python for my birthday.

The book is split into a series of lessons. Each lesson is a self-contained discussion of some Python feature or best practice. There are examples, code snippets1, and explanations of the advice. This structure makes it easy to read the book in small chunks, or dip in at random.

It covers a lot of material, from basic concepts like strings, up to advanced topics such as mixins and metaclasses. I learnt a lot of new things – even in areas where I thought I was already well-versed. And I’ve noticed a big improvement in the Python I’m writing, even just while I was reading the book.

My favourite parts were the sections on iterators and generators. Iteration is one of Python’s killer features, but I know I don’t use it enough. It’s a substantial portion of the first two chapters, and I have a much better understanding of iterators than I did before.

This is also the first book to sell me on Python 3. There wasn’t a single great feature, but a lot of nice touches that are only available in Py3. The accumulation of those small improvements convinced me to start writing more Py3 – if you’re on the fence about switching, maybe this will convince you as well.

If you want a taste of Slatkin’s style, watch his PyCon talk How to Be More Effective with Functions. It draws material from the early sections of the book, and gives a flavour of the book’s tone.

This isn’t a good book for learning Python. It assumes that you already know how to write Python, and builds on that knowledge to suggest ways to improve. Perhaps this could be a third or fourth Python book – but probably not your first.

Overall, I really enjoyed reading this book, and I know I’ll be using it again in the future. I would recommend it to anybody who already knows how to write Python.

  1. As a bonus, all the code samples are printed in colour. Syntax highlighting made it much easier to understand the examples. It’s by no means required, but it really does change the reading experience. ↩︎

Finding even more untagged posts on Tumblr

When I wrote my original script for finding untagged Tumblr posts, I expected it to be a one-off. I never expected to write a dedicated site, or for that site to become the most popular thing I’ve ever made. I’ve been flattered by some of the emails and tweets I’ve received about the site.

But I’ve also been letting it stagnate. I’ve been putting off a steady trickle of bug reports and feature requests, and the site was getting rough around the edges. On Monday, I inadvertently broke the site completely with some changes on the blog, so I decided that it was finally time to fix it.

This is a fairly major update, which I’m calling “v2.0”. It’s a ground-up rewrite that makes the site much simpler and easier to maintain.

Along with a fresh coat of paint and lots of bug fixes, there are a few new features:

  • A progress meter, so you can see how many posts have been checked (and how many are still to go)
  • Better filtering, allowing you to include/exclude reblogs and filter on post type
  • An improved mobile design

As always, the URL is http://finduntaggedtumblrposts.com and the code is on GitHub.

Feedback, bug reports, etc. can be sent via email or on Twitter.

← Older Posts