Any tips/comments on my code sample

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • apexi.200sx@hotmail.co.uk

    #1

    Any tips/comments on my code sample

    Just wondering if anyone could give me advice on handling potential
    error conditions in python, it seems that exceptions are used alot more
    for this stuff than in other languages.

    I have provided a code sample giving an example of a first attempt at
    using them (in python anyway) and also I have given an example of using
    the logging module I intend to use to replace all the print
    statements... it seems hassle to set up, but I think logging can
    provide good flexibility. - just wondering if you could give comments
    on my use of exceptions and the logging module (and my code in general
    as i'm a relative newbie), and whether the logging module can handle
    the case where you have threaded code (handling file locking on the log
    files etc.)

    Cheers

    def Configure_Loggi ng():
    print "configurin g logging"
    logger = logging.getLogg er("CT_Deploy" )
    logger.setLevel (logging.DEBUG)
    ch = logging.StreamH andler()
    ch.setLevel(log ging.DEBUG)
    formatter = logging.Formatt er("%(asctime) s - %(levelname)-8s -
    %(name)s - %(message)s")
    ch.setFormatter (formatter)
    logger.addHandl er(ch)

    logger.debug("L ogging configured")


    def Do_DB_Stuff(dep lo_num):

    logger = logging.getLogg er("CT_Deploy_l ogger.Do_DB_Stu ff")

    sql_list = ["alter table deplo_%s move tablespace MAP" % deplo_num,
    "create index deplo_%s_esz on deplo_%s(esz) tablespace
    cadidx" % (deplo_num, deplo_num),
    "analyze table deplo_%s estimate statistics for all
    columns" % deplo_num]

    db_list = ["db1", "db2", "db3", "db4"]

    for db in db_list:
    try:
    #connect_string = "usr/pass@%s" % db
    #conn = cx_Oracle.conne ct(connect_stri ng)
    #cur = conn.cursor()
    print "\n\nConnec ted to %s..." % db
    except cx_Oracle.Datab aseError:
    print "\n\nUnable to connect to %s" % db
    continue

    try:
    try:
    for sql in sql_list:
    #cur.execute(sq l % CT_deplo_num)
    print "\tSuccess: executing '%s' against %s" %
    (sql, db)
    except cx_Oracle.Datab aseError:
    print "\tFailure: executing '%s' against %s" % (sql,
    db)
    print "\tROLLING BACK..."
    conn.rollback()
    else:
    conn.commit()
    finally:
    cur.close()
    conn.close()

    def main():
    Configure_Loggi ng()
    deplo_num = raw_input("plea se enter deplo num...")
    Do_DB_Stuff(dep lo_num)


    if __name__ == "__main__":
    main()

Working...