« | » Main « | »

Time travel

Tuesday 19 July 2011

I had dinner last night with a woman I haven't seen in 32 years. We caught up, and she and Susan met, and we had a good time, just like old friends, but with a multi-decade gap. Of course, this was all made possible by the internet, and probably Facebook, I forget.

I realized though that we may be living in an unusual moment, the onset of hyper-connectedness. Will my children ever experience the feeling of re-connecting with someone after dozens of years of silence? Or will their teen-age friends simply remain part of their Facebook/Google+/WhoKnowsWhat social network forever, so that they never quite disconnect in the first place?

Re-connecting feels like finding a lost treasure, something you valued once, and have re-found. If I had stayed tenuously connected through a de-valuing mechanism like ongoing updates in a sea of "friends", would I have enjoyed last night's dinner as much? Will my children's friends become simply a steady background of noise? Or will they keep lasting relationships because the online communities make it easy to keep them?

Time will tell.

Getting started testing

Sunday 17 July 2011

This Wednesday the 20th, I'm speaking at the Boston Python Meetup: Getting Started Testing your Python. Please do come if you're in town. My goal will be to provide a totally from-scratch introduction to how to test Python code, in two hours, with time for questions.

I think I may have too much to say, though. The presentation itself is available for review. It could use another (hundred) pair of eyes. If you have any suggestions about what I should leave out, what I need to add in, what I've done completely wrong, and so on, let me know, either in a comment here, or in an email.


Caged python

Sunday 10 July 2011

For a presentation, I wanted to produce samples of Python interactive sessions. I could have opened a terminal window and typed my input, and copied the resulting session and pasted it into a text file, but that's not repeatable, and is labor intensive and error-prone.

I looked for ways people had done this in the past, and didn't find the thing that I'm sure is out there, but it's fun to do it yourself anyway. The code module in the standard library provides most of the heavy lifting, but there's a little input and output grabbing and tweaking to be done. Here's what I ended up with:

"""A python prompt in a cage, for producing prompt sessions."""

import code
import cStringIO as StringIO
import sys
import textwrap 

class CagedPrompt(code.InteractiveConsole):
    def __init__(self):
        env = {'__name__': '__main__'}
        code.InteractiveConsole.__init__(self, env)
        self.out = StringIO.StringIO()

    def run(self, input):
        self.inlines = textwrap.dedent(input).splitlines()
        old_stdout = sys.stdout
        sys.stdout = self.out
        self.interact("Python " + sys.version.split("[")[0])
        sys.stdout = old_stdout
        self.output = self.out.getvalue()

    def raw_input(self, prompt):
            line = self.inlines.pop(0)
        except IndexError:
            raise EOFError
        if line or prompt == sys.ps2:
            self.write("%s%s\n" % (prompt, line))
        return line

    def write(self, data):

def prompt_session(input):
    cp = CagedPrompt()
    return cp.output

if __name__ == '__main__':
    TEST_INPUT = """\
        import random
        class Foo:

        f = Foo()

    print prompt_session(TEST_INPUT)

Running it produces:

$ python cagedprompt.py
Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38)
>>> 2+2
>>> import random
>>> random.random()
>>> class Foo:
...     pass

>>> f = Foo()
>>> f
<__main__.Foo instance at 0x00000000025B6448>

There's a few small ways the output differs from a real interactive session: the initial banner is shorter, and a blank line in the input will produce a true blank line in the output. These make the output nicer to use for presentations. Now I can use the prompt_session function to get the textual output of a Python prompt fed with a particular input. Nice.

« | » Main « | »