Steven D'Aprano <steve@rem...so urce.com.auwrot e:
>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.
>
>have side-effects for other parts of your program, and will almost
>certainly end badly if you're running in the interactive interpreter.
>
"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
>differently:
>
>class console(object) :
"""
This spoofs a single file like object, using stdout & - in
(Minimalistic proof of concept implementation)
"""
read = sys.stdin.read
readline = sys.stdin.readl ine
write = sys.stdout.writ e
flush = sys.stdout.flus h
closeout = sys.stdout.clos e
closein = sys.stdin.close
@classmethod
def close(cls):
cls.closein()
cls.closeout()
>instance of console() has the same state, I'd write it slightly
>differently:
>
>class console(object) :
"""
This spoofs a single file like object, using stdout & - in
(Minimalistic proof of concept implementation)
"""
read = sys.stdin.read
readline = sys.stdin.readl ine
write = sys.stdout.writ e
flush = sys.stdout.flus h
closeout = sys.stdout.clos e
closein = sys.stdin.close
@classmethod
def close(cls):
cls.closein()
cls.closeout()
Seems to me the next time someone complains about 'self'
he should be told :
"use class attributes and methods, and only one instance"
*WEG*
>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?
>>
> 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
>
>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
>
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.readl ine, 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.
- Hendrik
--
Robert Wilensky:
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.