Training, Open Source computer languages

This is page http://www.wellho.net/forum/Programm ... -Ruby/Awkward-looking-code.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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