Discovered furl https://github.com/gruns/furl, a Python library for nicely manipulating URLs (query string etc). I've reinvented it several times.