[python-uk] Question from uniq dojo about fileinput

Tom Viner tom at viner.tv
Wed Jan 7 23:10:13 CET 2015


Hi all,

So before the London dojo meets again tomorrow night I wanted to resolve a
question from last month.

The task last month was to make our own GNU uniq commands. After the dojo I
got our team's code working with the fileinput module
<https://docs.python.org/2/library/fileinput.html>. This allowed the
flexibility of seamlessly reading from either stdin or taking filenames as
arguments.

With fileinput the code
<https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/uniq.py#L11>
can just say:
for line in fileinput.input():

and that gives you all these usages:

python uniq.py my_filename.txt other_file.txt
echo -e "hello\nhello2" | python uniq.py
python uniq.py <(python print_sleep_print.py)

With the print_sleep_print.py script
<https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/print_sleep_print.py>
I can even see about 8Kb at a time will be buffered into uniq.py, made
unique and printed, and then more data will be passed in.

The problem came when testing this feature. I found a way to connect to
both the input and output of a subprocess running the command:

    master, slave = pty.openpty()
    process = Popen("python uniq.py", shell=True, stdin=PIPE, stdout=slave)
    stdin_handle = process.stdin
    stdout_handle = os.fdopen(master)

I then write some data in, and read data out. It all works except for one
thing: even if I close the stdin_handle the stdout_handle will just block
once I've read all the output. Full test here
<https://github.com/tomviner/pydojo-uniq-s6e4/blob/master/team1/test_uniq.py#L37>
.

Appreciate any insight!
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-uk/attachments/20150107/330aecd6/attachment.html>


More information about the python-uk mailing list