George V. Reilly

Relative Imports in a Python Script

Have you ever attempted a relative import in a Python script?

$ ./foo/bar/ some parameters
Traceback (most recent call last):
  File "foo/bar/", line 16, in <module>
    from .quux import find_vcs
ValueError: Attempted relative import in non-package

I prefer to use absolute imports to minimize ambiguity and confusion, and most of my Python modules begin with:

from __future__ import absolute_import, unicode_literals, print_function

(Using uni­code_lit­er­als and print­_­func­tion makes porting to Python 3 easier.)

I recently read the accepted answer to Python relative imports for the billionth time and the solution to the above ValueError occurred to me: Use python -m package instead:

$ python -m some parameters

(Assuming that package foo exists.)

Installing Python 2.7.11 on Ubuntu

We deploy on Ubuntu 14.04, the most recent Long Term Support release. It comes with Python 2.7.6, but we need Python 2.7.9+ to get the some important SSL fixes and work with a recent version of the Requests library.

Felix Krull maintains a Personal Package Archive for Python 2.7 Updates, which makes it straight­for­ward to upgrade to Python 2.7.11 on supported versions of Ubuntu.

sudo apt-add-repository ppa:fkrull/deadsnakes-python2.7
sudo apt-get update
sudo apt-get install python2.7 python2.7-dev

Be sure not to use Felix Krull’s other Python PPA by mistake. I did that on a colleague’s machine yesterday. In our attempts to figure out why we still had Python 2.7.6, we managed to mess up the machine suf­fi­cient­ly that continue.

DVI Not Considered Equal

We got a new monitor today for one of our pairing work­sta­tions. At 2560x1600, it was higher resolution than its pre­de­ces­sor. I dis­con­nect­ed the DVI cable from the old monitor, leaving it connected to one of the video ports on the computer, and connected it to the new monitor. All I could see was static and noise on the screen. After trying a variety of other things, I eventually thought to use the DVI cable that came with the new monitor. It worked!

I had assumed that all DVI cables were in­ter­change­able. Reading the Wikipedia article, I see that at higher res­o­lu­tions, dual link is needed. I hadn’t known such a thing existed.

Python Base Class Order

When I declare a derived class that inherits from both a base class and some mixins, I am always tempted to write:

class Derived(Base, Mixin1, Mixin2):
    "Some class"

My reasoning is that Derived is a Base with some Mixin1 and Mixin2 goodness sprinkled on. Generally, that’s fine. The exception is when I want one of the mixins to override a method or attribute that’s defined in Base. Because the Method Resolution Order is left-to-right, then Base‘s im­ple­men­ta­tion will always be found first.

To get the desired behavior of the mixin overriding the base, Base should always appear last in the in­her­i­tance list.

from __future__ import print_function

class Useful(object):
    def __init__(self, msg):

Review: Star Doc

Title: Star Doc
Author: S.L. Viehl
Rating: ★ ★ ★ ★
Publisher: Roc
Copyright: 2000
Pages: 400
Keywords: SF
Reading period: 11–12 January, 2016

Dr. Cherijo Grey Veil flees Earth to the remote planet of Kevarzanga-2, seeking to get away from her dom­i­neer­ing father and the xenophobic Terrans. She finds in­ter­est­ing challenges and romance working with a variety of alien species at the FreeClinic. Then a plague strikes that kills thousands. When that’s resolved, the news comes that her father really, really wants her back, and is bringing enormous resources to make that happen.

I didn’t like the book at first, but it grew on me as I read more of it and I got caught up continue.

Bash: Getting and Setting Default Values

Bash has some handy syntax for getting and setting default values. Un­for­tu­nate­ly, it’s a collection of punc­tu­a­tion characters, which makes it hard to Google when you can’t quite remember the syntax.

Getting a default value using ${var:-fallback}:

# set $LOGDIR to $1 if $1 has a value; otherwise set $LOGDIR to "/var/log"

# use $VERSION unless it's empty or unset; fall back to extracting someprog's version num
build_version=${VERSION:-$(someprog --version | sed 's/[^0-9.]*\([0-9.]*\).*/\1/')}

The colon-dash con­struc­tion is known as the dog’s bollocks in typography.

Setting a default value, using ${var:=fallback}:

$ echo $HOME
$ echo ${HOME:=/tmp}
$ unset HOME
$ echo ${HOME:=/tmp}
$ echo $HOME
$ cd; pwd

Note: := uses the new value in two cases. First, when the shell continue.

Review: Blind Justice

Title: Blind Justice
Author: Bruce Alexander
Rating: ★ ★ ★ ★
Publisher: Berkeley
Copyright: 1994
Pages: 336
Keywords: historical mystery
Reading period: 10 January, 2016

The first in a series about Sir John Fielding, the blind magistrate who founded the Bow Street Runners, London’s first pro­fes­sion­al police force in 1749. Jeremy Proctor, a newly orphaned 13-year-old, is taken under Sir John’s wing and assists him in dis­cov­er­ing how the rakish Lord Goodhope was murdered in a locked room.

Although I figured out the murderer halfway through, I still enjoyed both the plot and the characters. Alexander vividly brings Georgian London to life.

Review: A Dedicated Man

Title: A Dedicated Man
Author: Peter Robinson
Rating: ★ ★ ★ ★
Publisher: Avon
Copyright: 1988
Pages: 352
Keywords: police procedural
Reading period: 8–10 January, 2016

A Dedicated Man is the second novel in the Inspector Banks series. A local historian has been murdered in the Yorkshire Dales. He was well-liked and there seems to be no obvious motives or suspects. Banks must dig into the dead man’s past if there are no leads in the present.

Like other books in the series, this is a competent well-written police procedural, partly seen through Banks’ eyes and partly through the eyes of some of the locals. Robinson is an expatriate York­shire­man with a fondness for his homeland and continue.

Review: Dollmaker

Title: Dollmaker
Author: J. Robert Janes
Rating: ★ ★ ★ ½
Publisher: Soho Crime
Copyright: 1995
Pages: 272
Keywords: police procedural, WW II
Reading period: 2–7 January, 2016

Occupied France, January 1943. Detectives Jean-Louis St-Cyr and Hermann Kohler are sent to the German submarine base at Lorient in Brittany to in­ves­ti­gate a murder. The Gross-Admiral wants a quick resolution to the case since the prime suspect is a U-Boat captain known as the Dollmaker, whose crew are de­mor­al­ized after many months of punishing cruises and who won’t go back to sea without him.

St-Cyr and Kohler are unlikely partners, a Chief Inspector from the Sûreté in Paris and a longtime criminal policeman now in the Gestapo. Both are astute continue.

Happy Birthday, Roy Batty

I’ve seen things you people wouldn’t believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears…in…rain. Time to die.

—Roy Batty, the Tears in Rain monologue

According to Blade Runner, the replicant Roy Batty’s incept date is January 8th, 2016. The Sydney Morning Herald has an article.

In honor of which, we watched Blade Runner tonight.

Previous » « Next