PythonThingies

Introduction

As you might guess, this is for python-related thingies I learn as I go along. The python docs for most things are quite usable, but sometimes things just are non-obvious. So I will document them here.

Process Madness

Dealing with processes always seem to give me grey hair. What I wanted to do is the following:

Do it like this, using the subprocess module:


    commandproc = subprocess.Popen(['commandname', 'arg1', 'arg2 etc.'],
                                 stdin=subprocess.PIPE)
    for cnt in range(outlines):
    # outfile is a file like object, containing the lines to be sent
    # to commandproc.stdin, outlines the number of lines to write
        commandproc.stdin.write(outfile.readline())

    commandproc.stdin.close()      # This seemed obvious (to me at least)
    commandproc.wait()             # This seemed non-obvious!

Well, there you have it. The tricky-bit is that you have to call the wait() method...

PolyMorphismAndDicts