Python Unit Testing Preamble

A little trick I just came up with for command line unit test output. It will print the docstring for the module at the beginning of your unit test. Use this code at the bottom of your unit test file:

#
# MAIN
#
if __name__ == "__main__":
    print sys.modules[globals()['__name__']].__doc__    # print module doc
    suite = unittest.TestLoader().loadTestsFromTestCase(YourTestClassHere)
    unittest.TextTestRunner(verbosity=2).run(suite)

Forcing /etc/cron.d to Reload on Ubuntu

There are two options for scheduling a script with cron on linux. Either edit the crontab:

$ sudo crontab -e

Or link a cron file to /etc/cron.d:

$ sudo ln -sv /home/klenwell/crons/my.cron /etc/cron.d/mycron

I like option two because it provides a more modular and organized way to enable cron tasks for various projects. The only problem with this method is that, when things go wrong, it’s been a huge headache trying to figure out what the problem is and how to get the tasks in the linked cron file running again. The cron documentation and Google are frustratingly tight-lipped on the subject of /etc/cron.d. But I think I have figured out a reliable way to configure and trouble /etc/cron.d.

Some rules that will help in using /etc/cron.d:

  1. You must specify a user in your cron file.
  2. The script being linked must be owned by root (giving root permission alone doesn’t seem enough)
  3. To force /etc/cron.d to reload, touch the /etc/cron.d dir: sudo touch /etc/cron.d/

I was able to confirm these rules by linking a cron file to /etc/cron.d, then changing the owner to myself (klenwell), then checking the /var/log/syslog file.

Cron checks /etc/cron.d minutely, so you have to give it at least 60 seconds to register any changes. I set up my test cron file (/home/klenwell/crons/my.cron) to touch a file in /tmp (/tmp/crontest) every minute.

When the cron file is owned by my user (klenwell), I see this message in the log:

Nov 26 02:55:01 myserver cron[1924]: (*system*mycron) WRONG FILE OWNER (/etc/cron.d/mycron)

The linked cron file will not run again until the owner is corrected and the directory touched:

$ sudo chown -v root /home/klenwell/crons/my.cron
$ sudo touch /etc/cron.d/

After this, I see this in /var/log/syslog:

Nov 26 02:57:01 myserver CRON[21170]: (klenwell) CMD (   touch /tmp/crontest)

Took me a lot of trial and error to figure this out. Hope it saves your an extra headache or two.

Get for Python Lists

With python’s dictionary objects, you have the useful get method:

>>> d = {'a':1, 'b':2, 'c':3}
>>> d.get('a')
1
>>> d.get('d', 'not found')
'not found'

Sometimes, I find something like this would be useful for lists, using the list’s index in place of the dict’s key:

>>> l = [0,1,2,3]
>>> l.get(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'get'

Here’s a quick lambda that accomplishes that:

list_get = lambda l,i,a=False: (len(l) <= i and a) or (len(l) > i and l[i])

Usage:

>>> list_get = lambda l,i,a=False: (len(l) <= i and a) or (len(l) > i and l[i])
>>> my_list = [0,False,None,3]
>>> list_get(my_list,0)
0
>>> list_get(my_list,1)
False
>>> list_get(my_list,2)  # returns None
>>> list_get(my_list,3)
3
>>> list_get(my_list,4)
False
>>> list_get(my_list,4,0)  # Note: returns False instead of 0
False
>>> list_get(my_list,4,1)
1

It’s not perfect. Be careful with False-equivalent values in the alt parameters as noted above. But otherwise it should work for most practical cases were such usage is desired.

Google Arcade Fire Website

The Google Arcade Fire HTML5 demo is cool, but the code is surprisingly simple:

from google import WebApp, Predict, Users, Maps, Html5, NsaProxy

user = Users.lookup_by_existing_cookies_and_ip()
address = Predict.get_address('Enter the address where you grew up:')
data_raper = NsaProxy.init(user, address)

try:
    map = Maps.load_data(address)

    if map.context == 'urban':
        kid = Html5.load_urban_kid()
    else:
        kid = Html5.load_suburban_kid()

    video = WebApp.play(Html5.load_videos(kid, map))

    while video.playing():
        data_raper.rapes_your_personal_data()

except Html5.MovieOver:
    WebApp.redirect_to_launchpage()
except:
    WebApp.redirect_to_chrome_required_page()

Great album, by the way.

Inception

Saw Inception recently. First movie I’ve seen in months. My micro-review, originally posted on Hacker News:

The it’s-all-a-dream interpretation is the only one that makes sense from a science-fiction point-of-view, however unsatisfying it is from a critical perspective. The slightly wobbling top at the end points, rather overtly, to the real answer: it’s meant to be ambiguous.

That said: WTF? We’ve got to bring down a company on the verge of being The Most Powerful Corporation in the World. So let’s use this highly radical, unproven and improbable, voodoo-science inception approach. Oh, and by the way, we have to plant a mole within the highest ranks of the multinational beast within the next, say, two weeks, so we can get valuable information that is necessary to penetrate and manipulate the fragile psyche of our target.

Cut-scene: it’s done. This is logic that only makes sense within the context of a dream. The sort of dream Coleridge, or Rube Goldberg, would have.

Science generally favors the application of Occam’s Razor: buy the airline and crash the damn plane with the kid on it. Or just send in the ninjas. The ensuing infighting among the various executive factions vying for the top spot after junior is out of the picture will ensure that any plans Acme Energy Inc had for world domination are put off course indefinitely.

Inception was entertaining. But Memento was a more interesting film intellectually. That’s a film that truly merited this kind of philosophical scrutiny. Remember Sammy Jankis.

This was in support of the basic premise: Inception isn’t science fiction