Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Awkward looking code

Posted by cloughie (cloughie), 23 January 2008
Is there a neater way of avoiding the continual use of the boolean 'quit_input' below, such that I don't get quite so much indended IF/ELSE constructs ?  (exception handling ?)

Code:
def input_for_AIS(input_connection):

   s = input_connection
   index_stats="AIS"
   
   result_time = 1.5
   quit_input = ''
   db_nm_upper = get_objectName('DATABASE').upper()
   if db_nm_upper == 'Q': quit_input = 'Y'
   else:
       tb_nm_upper = get_objectName('TABLE').upper()
       if tb_nm_upper == 'Q': quit_input = 'Y'

   if quit_input:
       returning('Returning to Menu ...')                
   else:
       env=get_environment(db_nm_upper)
       if env == '':
           returning('Database must be prefixed with environment.  Please try again',4)
           quit_input = 'Y'

       if not quit_input:
           if tb_nm_upper.endswith('_V01'):
               tb_nm_upper = tb_nm_upper.replace('_V01','')
           if db_nm_upper.endswith('_V'):
               db_nm_upper = db_nm_upper.replace('_V','_T')

           sql_dict = general_sql()
           result = getdata(s,sql_dict.get(index_stats).replace \
                       (':IN_DB',encase(db_nm_upper,quote,quote)).replace(':IN_TB',encase(tb_nm_upper,quote,quote)) \
                       .replace(':ENV',env))
           if result == '': returning('No action taken.  Please check the input, incase not in correct form for procedure',4)
           else:
               for row_entry in result:
                   if len(row_entry) > 0:
                       resultCode = row_entry[0]
                       resultMsg = row_entry[1]
                       print 'restultCode=',resultCode
                       if resultCode <> '00': result_time = 5
                       returning('ResultCode= '+resultCode+' '+resultMsg,result_time)        


I have added code tags to help readability - Graham

Posted by admin (Graham Ellis), 24 January 2008
I tried to edit that to make opentalk present the (critical in the case of Python) spaces well ... but it made it no better to read - sorry.

It is a bit haevy, isn't it?    Bit of a contivance, but I wonder about purring the whole of teh offending logic in a while 1: with a break to get out at every point needed, and a break at the end to stop it looping.

while 1:
   if ............. break

   if .............. break

   if  .............  break

   break

print "clean up code:

Posted by cloughie (cloughie), 24 January 2008
Right, ok, I've re-worked this particular function, as you can see below.  Is it any better, well, I'd say .... YES.  

Like you point out, as long as you remember to code the final 'break', it's fine.

Certainly it's got rid of that 'quit_input' boolean that seemed to want ot take over the world!

Anyway, thanks for the tip, I'll code it this way from now on.  It's certainly been an annoyance of mine for some time.

*******************************************

def input_for_AIS(input_connection):

   s = input_connection
   index_stats="AIS"
   
   result_time = 3
   
   clear_windows_screen()
   
   print 'INDEX STATS'
   print '***********'

   while 1:
       db_nm_upper = get_objectName('DATABASE').upper()
       if db_nm_upper == 'Q':
           returning('Returning to Menu ...')                
           break
       else:
           tb_nm_upper = get_objectName('TABLE').upper()
           if tb_nm_upper == 'Q':
               returning('Returning to Menu ...')                
               break

       print  '\n'
       env=get_environment(db_nm_upper)
       if env == '':
           returning('Database must be prefixed with environment.  Please try again',4)
           break

       if tb_nm_upper.endswith('_V01'):
           tb_nm_upper = tb_nm_upper.replace('_V01','')
       if db_nm_upper.endswith('_V'):
           db_nm_upper = db_nm_upper.replace('_V','_T')

       sql_dict = general_sql()
       result = getdata(s,sql_dict.get(index_stats).replace \
                   (':IN_DB',encase(db_nm_upper,quote,quote)).replace(':IN_TB',encase(tb_nm_upper,quote,quote)) \
                   .replace(':ENV',env))
       if result == '':
           returning('No action taken.  Please check the input, incase not in correct form for procedure',4)
       else:
           for row_entry in result:
               if len(row_entry) > 0:
                   resultCode = row_entry[0]
                   resultMsg = row_entry[1]
                   if resultCode <> '00': result_time = 5
                   returning('ResultCode= '+resultCode+' '+resultMsg,result_time)                                    

       break # while loop            




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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho