Ascii Menu I/O redirection
Hendrik van Rooyen
mail at microcorp.co.za
Sun Sep 21 07:51:32 CEST 2008
Steven D'Aprano <steve at rem...source.com.au> wrote:
>I'm not sure that closing stdin and stout are a good idea. This could
>have side-effects for other parts of your program, and will almost
>certainly end badly if you're running in the interactive interpreter.
Its a very simple thingy - there will only ever be one object -
"The Console", or "The Connection" The close stuff was there because
I thought it would be better to close the RS-232 port if I were using it.
I only close it right at the end before exiting - but you are right. - if
I invoke the interpreter with -i then it will be seriously broken.
>Other than that, what you've done seems reasonable, although since every
>instance of console() has the same state, I'd write it slightly
> This spoofs a single file like object, using stdout & - in
> (Minimalistic proof of concept implementation)
> read = sys.stdin.read
> readline = sys.stdin.readline
> write = sys.stdout.write
> flush = sys.stdout.flush
> closeout = sys.stdout.close
> closein = sys.stdin.close
> def close(cls):
Thanks. It looks neater without all the "self"s.
Seems to me the next time someone complains about 'self'
he should be told :
"use class attributes and methods, and only one instance"
>> Questions are:
>> Is this a reasonable way of doing this kind of thing? Is there a
>> canonical or better way of doing it? Am I missing something?
>It seems to me that you might have been better off to write your program
>to take two files, an input and an output, instead of forcing both to go
>to the same file.
>if 'serial' in sys.argv: # for RS-232 i/o to terminal
> infile = open('/dev/ttyS0','r+b')
> outfile = infile
> else: # console i/o
> infile = sys.stdin
> outfile = sys.stdout
This is weird - This is exactly how I started off - and then
I thought about the errors, and I did not want to pass
three files, so I started looking for one. :-)
Reading your response, and thinking about what I have done, I get
the feeling that its all too complicated - What I will probably
end up doing would be to pass no file, and just use standard
print statements and sys.stdin.readline, etc.
Then if I redirect the stdin,-out and -err, the thing is sorted,
without the necessity of jumping through OO hoops, as this class
is the only one in the programme - all the rest are functions.
It is impossible to think clearly all of the time.
It is difficult to think clearly most of the time.
In fact it is nice to have an occasional lucid thought...
>Hope this helps.
Yes it has. - Thanks for the input.
We've all heard that a million monkeys banging on a million typewriters
will eventually reproduce the entire works of William Shakespeare.
Now, thanks to the Internet, we know this not true.
More information about the Python-list