473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Ascii Menu I/O redirection


I am writing a small application with a simple ascii based menu.

The menu is used to test individual functions, and to change some timings.
Otherwise the application just runs automatically, depending on command
line options.

I want to be able to redirect the menu.
The console, a serial port, or possibly a socket are target candidates.

Now a serial port and a socket are single files, so I need a "file"
that represents the console, to pass to the working functions.

Simply re-assigning for instance sys.stdin.write to point to
sys.stdout.write, and using sys.stdin as such a file does not work...

So I do the following:

<start code fragment>

class console(object):
"""
This spoofs a single file like object, using stdout & - in
(Minimalistic proof of concept implementation)
"""

def __init__(self):
self.read = sys.stdin.read
self.readline = sys.stdin.readline
self.write = sys.stdout.write
self.flush = sys.stdout.flush
self.closeout = sys.stdout.close # keep references to close
self.closein = sys.stdin.close

def close(self):
self.closein()
self.closeout()

# see if we must run, and how:

if __name__ == "__main__":

if 'serial' in sys.argv: # for RS-232 i/o to terminal
f = open('/dev/ttyS0','r+b')
else: # console i/o
f = console()

sys.stderr = f # redirect errors
sys.stdout = f # redirect printing
sys.stdin = f # redirect raw_input stuff

if 'menu' in sys.argv: # test and timing changes
menu_loop(menu_dict,f) # (menu_dict is dispatch dict)
else: # else just run the system
autorun(menu_dict,f)

<end code fragment>

The above just shows a choice between console and serial as an
example - adding a socket option would be trivial.

This all seems to work, but I am asking here before I take the
trouble to turn it into production code, as I don't think it
is exactly a new problem.

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?

Using Linux only.

- Hendrik


Sep 20 '08 #1
1 1902
On Sat, 20 Sep 2008 23:14:26 +0200, Hendrik van Rooyen wrote:
class console(object):
"""
This spoofs a single file like object, using stdout & - in
(Minimalistic proof of concept implementation) """

def __init__(self):
self.read = sys.stdin.read
self.readline = sys.stdin.readline
self.write = sys.stdout.write
self.flush = sys.stdout.flush
self.closeout = sys.stdout.close # keep references to close
self.closein = sys.stdin.close

def close(self):
self.closein()
self.closeout()

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.

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.readline
write = sys.stdout.write
flush = sys.stdout.flush
closeout = sys.stdout.close
closein = sys.stdin.close
@classmethod
def close(cls):
cls.closein()
cls.closeout()

[...]
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

Hope this helps.
--
Steven

Sep 20 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Gerard M Foley | last post by:
Can one write a webpage which is not displayed but which simply redirects the user to another page without any action by the user? Sorry if this is simple, but I am sometimes simple myself. ...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
3
by: Ohad Young | last post by:
Hi, I have a frameset page that contains two frames: 1. the first frame, an aspx page, contains a menu for navigating in the website. 2. the second frame contains content, pdf, html or other...
4
by: Bart | last post by:
Hi, I have a menu on my master page. When I click on a menu item the state is reset after the page is redirected to the NavigateURL. How can fix this? Thanks Bart
8
by: Luciano A. Ferrer | last post by:
Hi! I was following the http://www.seomoz.org/articles/301-redirects.php article, trying to do that with one of my test sites I added this to the .htaccess file: RewriteEngine On RewriteCond...
13
by: souissipro | last post by:
Hi, I have written a C program that does some of the functionalities mentionned in my previous topic posted some days ago. This shell should: 1- execute input commands from standard input,...
1
by: comp.lang.php | last post by:
require_once("/users/ppowell/web/php_global_vars.php"); if ($_GET) { // INITIALIZE VARS $fileID = @fopen("$userPath/xml/redirect.xml", 'r'); $stuff = @fread($fileID,...
13
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.