Python程序员糟糕的一天!!
第一张图是上午上班前,第二张图是下午下班前,一天下来就加了一个return!
为了方便你们看懂具体(主要)的改动,我加了第三张图(上班前)和第四张图(下班前)。
第一张图(因图片显示不完整而看不清楚的,请在“在新标签页中打开图片”):
第二张图:
第三张图:
第四张图:
上班前代码:
#!/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- """ Created by PyCharm. File: LinuxBashShellScriptForOps:odbp_getToken User: Guodong Create Date: 2016/8/10 Create Time: 17:04 """ import os import sqlite3 import sys # import time enable_debug = True def debug(msg, code=None): if enable_debug: if code is None: print "message: %s" % msg else: print "message: %s, code: %s " % (msg, code) MAIL = "uberurey_ups@163.com" weixin_qy_CorpID = "idxxx" weixin_qy_Secret = "secretxxx" # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Database # https://docs.djangoproject.com/en/1.9/ref/settings/#databases DATABASES = { ‘default‘: { ‘ENGINE‘: ‘db.backends.sqlite3‘, ‘NAME‘: os.path.join(BASE_DIR, ‘.odbp_db.sqlite3‘), } } sqlite3_db_file = str(DATABASES[‘default‘][‘NAME‘]) def sqlite3_conn(database): try: conn = sqlite3.connect(database) except sqlite3.Error: print >> sys.stderr, """ There was a problem connecting to Database: %s The error leading to this problem was: %s It‘s possible that this database is broken or permission denied. If you cannot solve this problem yourself, please mail to: %s """ % (database, sys.exc_value, MAIL) sys.exit(1) else: return conn # TODO(Guodong Ding) A bad function design, this function will be never used, because commit and close can not be call def sqlite3_cursor(conn): if conn is None: conn = sqlite3_conn(sqlite3_db_file) return conn.cursor() def sqlite3_commit(conn): return conn.commit() def sqlite3_close(conn): return conn.close() def sqlite3_execute(database, sql): try: sql_conn = sqlite3_conn(database) sql_cursor = sql_conn.cursor() sql_cursor.execute(sql) sql_conn.commit() sql_conn.close() except sqlite3.Error as e: print e sys.exit(1) def sqlite3_create_table_token(): sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_token" ( "id" INTEGER , "access_token" TEXT, "expires_in" TEXT, "expires_on" TEXT, "is_expired" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_create_table_account(): sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_account" ( "id" INTEGER, "name" TEXT, "corpid" TEXT, "secret" TEXT, "current" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_create_tables(): print "sqlite3_create_tables" sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_token" ( "id" INTEGER , "access_token" TEXT, "expires_in" TEXT, "expires_on" TEXT, "is_expired" INTEGER ) ; ‘‘‘) sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_account" ( "id" INTEGER, "name" TEXT, "corpid" TEXT, "secret" TEXT, "current" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_set_credential(corpid, secret): try: sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘INSERT INTO "weixin_account" ("id", "name", "corpid", "secret", "current") VALUES (1, ‘odbp‘, ?, ?, 1) ‘‘‘, (corpid, secret)) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) except sqlite3.Error: sqlite3_create_table_account() sqlite3_set_credential(corpid, secret) def sqlite3_get_credential(): try: sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() credential = sql_cursor.execute(‘‘‘SELECT "corpid", "secret" FROM weixin_account WHERE current == 1;‘‘‘) result = credential.fetchall() sqlite3_close(sql_conn) except sqlite3.Error: sqlite3_set_credential(weixin_qy_CorpID, weixin_qy_Secret) sqlite3_get_credential() else: if result is not None and len(result) != 0: return result else: try: sqlite3_set_credential(weixin_qy_CorpID, weixin_qy_Secret) sqlite3_get_credential() except Exception as e: print e print "unrecoverable problem, please alter to %s" % MAIL sys.exit(1) finally: sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() credential = sql_cursor.execute(‘‘‘SELECT "corpid", "secret" FROM weixin_account WHERE current == 1;‘‘‘) result = credential.fetchall() sqlite3_close(sql_conn) return result c = sqlite3_get_credential() if c is None: print "Strange problem! get a %s" % c # d = sqlite3_get_credential() # print d else: print c
下班前代码:
#!/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- """ Created by PyCharm. File: LinuxBashShellScriptForOps:odbp_getToken User: Guodong Create Date: 2016/8/10 Create Time: 17:04 """ import os import sqlite3 import sys # import time enable_debug = True def debug(msg, code=None): if enable_debug: if code is None: print "message: %s" % msg else: print "message: %s, code: %s " % (msg, code) MAIL = "uberurey_ups@163.com" weixin_qy_CorpID = "idxxx" weixin_qy_Secret = "secretxxx" # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Database # https://docs.djangoproject.com/en/1.9/ref/settings/#databases DATABASES = { ‘default‘: { ‘ENGINE‘: ‘db.backends.sqlite3‘, ‘NAME‘: os.path.join(BASE_DIR, ‘.odbp_db.sqlite3‘), } } sqlite3_db_file = str(DATABASES[‘default‘][‘NAME‘]) def sqlite3_conn(database): try: conn = sqlite3.connect(database) except sqlite3.Error: print >> sys.stderr, """ There was a problem connecting to Database: %s The error leading to this problem was: %s It‘s possible that this database is broken or permission denied. If you cannot solve this problem yourself, please mail to: %s """ % (database, sys.exc_value, MAIL) sys.exit(1) else: return conn # TODO(Guodong Ding) A bad function design, this function will be never used, because commit and close can not be call def sqlite3_cursor(conn): if conn is None: conn = sqlite3_conn(sqlite3_db_file) return conn.cursor() def sqlite3_commit(conn): return conn.commit() def sqlite3_close(conn): return conn.close() def sqlite3_execute(database, sql): try: sql_conn = sqlite3_conn(database) sql_cursor = sql_conn.cursor() sql_cursor.execute(sql) sql_conn.commit() sql_conn.close() except sqlite3.Error as e: print e sys.exit(1) def sqlite3_create_table_token(): sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_token" ( "id" INTEGER , "access_token" TEXT, "expires_in" TEXT, "expires_on" TEXT, "is_expired" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_create_table_account(): sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_account" ( "id" INTEGER, "name" TEXT, "corpid" TEXT, "secret" TEXT, "current" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_create_tables(): print "sqlite3_create_tables" sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_token" ( "id" INTEGER , "access_token" TEXT, "expires_in" TEXT, "expires_on" TEXT, "is_expired" INTEGER ) ; ‘‘‘) sql_cursor.execute(‘‘‘CREATE TABLE "main"."weixin_account" ( "id" INTEGER, "name" TEXT, "corpid" TEXT, "secret" TEXT, "current" INTEGER ) ; ‘‘‘) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) def sqlite3_set_credential(corpid, secret): try: sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() sql_cursor.execute(‘‘‘INSERT INTO "weixin_account" ("id", "name", "corpid", "secret", "current") VALUES (1, ‘odbp‘, ?, ?, 1) ‘‘‘, (corpid, secret)) sqlite3_commit(sql_conn) sqlite3_close(sql_conn) except sqlite3.Error: sqlite3_create_table_account() sqlite3_set_credential(corpid, secret) def sqlite3_get_credential(): try: sql_conn = sqlite3_conn(sqlite3_db_file) sql_cursor = sql_conn.cursor() credential = sql_cursor.execute(‘‘‘SELECT "corpid", "secret" FROM weixin_account WHERE current == 1;‘‘‘) result = credential.fetchall() sqlite3_close(sql_conn) except sqlite3.Error: sqlite3_set_credential(weixin_qy_CorpID, weixin_qy_Secret) return sqlite3_get_credential() else: if result is not None and len(result) != 0: return result else: print "unrecoverable problem, please alter to %s" % MAIL sys.exit(1) c = sqlite3_get_credential() if c is None: print "Strange problem! get a %s" % c # d = sqlite3_get_credential() # print d else: print c
对,就是一个return!!
--end--
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1836916
原文地址:http://dgd2010.blog.51cto.com/1539422/1836916