By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,899 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,899 IT Pros & Developers. It's quick & easy.

Any tips/comments on my code sample

P: n/a
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_Logging():
print "configuring logging"
logger = logging.getLogger("CT_Deploy")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(levelname)-8s -
%(name)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)

logger.debug("Logging configured")
def Do_DB_Stuff(deplo_num):

logger = logging.getLogger("CT_Deploy_logger.Do_DB_Stuff")

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.connect(connect_string)
#cur = conn.cursor()
print "\n\nConnected to %s..." % db
except cx_Oracle.DatabaseError:
print "\n\nUnable to connect to %s" % db
continue

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

def main():
Configure_Logging()
deplo_num = raw_input("please enter deplo num...")
Do_DB_Stuff(deplo_num)
if __name__ == "__main__":
main()

Jul 25 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.