| |||||||||||
menu Posted by cloughie (cloughie), 24 October 2007 Is there a pretty standard way of coding a simple windows (or even Dos) Menu option program ?I've done all the backend hard part (well, for one as inexperienced as me it was hard) but I wonder whether there's a neat way of presenting a number of options to the User. The main mental stumbling block I've got is how to allow the Users to navigate back and forwards in the menu. It doesn't have to be too sophisticated, just something that works. Any ideas appreciated. Regards, Dave. Posted by admin (Graham Ellis), 25 October 2007 Are you looking for a Python-based solution or batch file stuff? I'm so much at the other end of the language spectrum this week (teaching Tcl)! that I would have difficulty siwtching to a Python example tonight ... but I could come up with one over the weekend. Batch just ain't my thing!Posted by cloughie (cloughie), 25 October 2007 It's a Python solution I was looking for.I have come with something, which seems to work. It might be very 'noddy' to you gurus .... take a look : ==================================== import re from sys import * from os.path import * #from getTeradataCompressData import #main_getCompressData #from obtainExportedInformation import main_getDDL def main_menu(msg): delim1='='*20 delim2='='*60 main_hdr = delim1 + ' Python DBD Menu ' + delim1 option_1 = '1 : do nothing' option_2 = '2 : Specify Teradata Table' option_3 = '3 : Form Compressd version of Teradata Table' print delim2 print main_hdr print delim2 print option_1 print option_2 print option_3 if msg: print msg in_sel = raw_input (">>> Please enter your Selection (Q to Quit): ") return in_sel end = 0 quit_input = '' valid_input = '' msg = '' while not quit_input : selected_option = main_menu(msg) msg = '' if selected_option.upper() == 'Q': quit_input = 'Y' elif str(selected_option) not in ('1','2','3'): msg = '>>> ' + selected_option + ' <<< is an Error selection' else : valid_input = 'true' if not quit_input : if selected_option == '2': print '2' #main_getCompressData() elif selected_option == '3': print '3' #main_getDDL() else : print 'do nothing' if quit_input : exit('01') ==================================== Is this just embarrassing code, or is it ok ? (It does work, so that surely gets at least get a pass mark, right?) Thanks again. P.S. I love this site. Posted by admin (Graham Ellis), 26 October 2007 A good start. I would tend to generalise the code - passing in a list of options to main_menu and then looping through them. And also including the validation in main_menu ... or providing a separate method to do the validation. Heading in that direction will give you the code o re-use in other requirements, and a consistent user interface.Posted by cloughie (cloughie), 29 October 2007 Thanks for those comments.Yes, I can see that generalizing at least the list of options passed through to main_menu would be of advantage ... I'll address this. As to the validation, I think I'll leave that bit until, or if, further checks are required. I may have to come back to this same question in the probably not too distanct future as sub-menus become more necessary. I'll want to add options for those 'bits and pieces' that appear useful at the time of writing but get lost when you next come to need them (at least, they do with me!) So, for the moment, thanks again - I've got plenty to get on with. Posted by cloughie (cloughie), 1 November 2007 Just for further feedback, I've now made the printing of the menu option more generic - as suggested - whilst also incorporating a simple way of calling sub-menus (the code for which is pretty much the same as the main menu, which of course gives me thoughts about making one generic function).Anyway, here's the code for the main menu : ================================= import time from CompressSubmenu import Sub_Menu def returning(msg,delay=1): print msg time.sleep(delay) menu_selections = """('1','2')""" def main_menu(msg): option_X = '1. do nothing' option_CPR = '2. Compress Utility' option_list = (option_X,option_CPR) delim1='='*20 delim2='='*60 main_hdr = delim1 + ' Python DBD Menu ' + delim1 print delim2 print main_hdr print delim2 for entry in option_list: print entry if msg: print msg in_sel = raw_input (">>> Please enter your Selection (Q to Quit): ") return in_sel quit_input = '' valid_input = '' msg = '' CPR = '2' while not quit_input : selected_option = main_menu(msg) msg = '' if selected_option.upper() == 'Q': quit_input = 'Y' elif str(selected_option) not in eval('menu_selections'): msg = '>>> ' + selected_option + ' <<< is an Error selection' else : valid_input = 'true' if not quit_input : if selected_option == CPR: Sub_Menu() else : continue if quit_input : returning('Leaving the Pyton Main Menu .... bye.') exit ================================= What I do like about it (if I say it myself) is the use of the check against the variable CPR, rather than the value of it (i.e. '2'). It means that if I want to imbed new options in the menu selection list, at least that part of the code doesn't change. Posted by cloughie (cloughie), 19 December 2007 FYI and QUESTION============= Pleased to say that my TNT Python menu is going from strenth to strength (or maybe weakness to strength is closer to the truth). In fact, even with a few items now on the menu, it's becoming quite difficult to know which functions need modifying. So, for your information (and for anyone's benefit, in case anyone else has similar problems) I'm going to use BPwin (similar to ERwin but for Process modelling, not data modelling) to define which function or process calls which sub-process, using Function decomposition techniques. And now for a question ... I need to count the number of leading spaces on a field. I've coded it in rather a convoluted manner : def get_leadingblanks(instring): splitter=compile('\S') #print instring mysplitwords = splitter.split(instring) x = 1 return_ct = 0 #print mysplitwords for entry in mysplitwords: if x == 1 : return_ct = len(entry) break return return_ct All works very nicely but I suspect there will be a 'one line command' for this - is there ? If nothing else, you can have a titter at my code ! Kind regards and Merry Christmas Posted by admin (Graham Ellis), 19 December 2007 Trim the field and subtract the new length from the old?Posted by cloughie (cloughie), 20 December 2007 That's clever. Even our most experienced programmer confessed to never having thought of that solution.Thanks. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |