Python修饰函数无法返回正确的值
我编写这段代码是为了在调用的底层sql脚本过于嘈杂时修饰函数。gp_info_decorator函数修改execute_gp_func以抑制来自sql脚本的消息。相反,它只是将结果存储在状态变量中 但是,修饰后的函数将返回None,而不是当前日期Python修饰函数无法返回正确的值,python,decorator,python-decorators,Python,Decorator,Python Decorators,我编写这段代码是为了在调用的底层sql脚本过于嘈杂时修饰函数。gp_info_decorator函数修改execute_gp_func以抑制来自sql脚本的消息。相反,它只是将结果存储在状态变量中 但是,修饰后的函数将返回None,而不是当前日期 import sys import psycopg2 import json import os #import contextlib.contextmanager import sys # functions to train and predi
import sys
import psycopg2
import json
import os
#import contextlib.contextmanager
import sys
# functions to train and predict
class GetExecutor:
#global props
# create instance variables from the properties file
def __init__(self,home_dir, os_type = "linux"):
if sys.platform in ('linux', 'linux2'):
self.home_dir = home_dir
self.os_type = 'linux'
self.dbname = None
self.user = None
self.host = None
self.passwd = None
self.conn = None
elif sys.platform in ('win32'):
self.home_dir = home_dir
self.os_type = 'windows'
self.dbname = None
self.user = None
self.host = None
self.passwd = None
self.conn = None
def read_properties(self):
with open('properties.json', 'r') as f1:
props = json.load(f1)
self.dbname = props['dbname']
self.user = props['user']
self.host = props['host']
self.passwd = props['passwd']
#self.start_date = props['start_date']
#self.end_date = props['end_date']
print ("Properties stored")
return props
# sql connector
def sql_connect(self):
try:
connect_string = "dbname = %s user = %s host = %s password = %s"%(self.dbname, self.user, self.host, self.passwd)
conn = psycopg2.connect(connect_string)
conn.set_isolation_level(0)
print ("Connecting --")
except:
print "Unable to connect to the database"
self.conn = conn
# suppress logger info from sql if it is too much noise during execution
def gp_info_decorator(function):
def wrapper(*args, **kwargs):
#print args[0].home_dir
#print args[0].query
#with open(sys.stdout, "w") as devnull:
function(*args, **kwargs)
"""
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
function(*args, **kwargs)
finally:
sys.stdout = old_stdout
"""
print ("Debug 3--->", wrapper)
return wrapper
# Execute training
@gp_info_decorator
def execute_gp_func(self, home_dir, query):
print ("Function Name:", query)
with self.conn.cursor() as cursor:
cursor.execute(query)
print "Executing-->"
Status= cursor.fetchall()
print ("Status is ", Status)
for notice in self.conn.notices:
print notice
print ("Debug 2--->", Status)
#Status =
return Status
def main():
home_dir = os.getcwd()
print home_dir
obj = GetExecutor(home_dir)
print obj
props = obj.read_properties()
obj.sql_connect()
#status = obj.execute_gp_func(home_dir,"select get_analytic.fn_get_logistics_train_data(%s,%s);"%(props['start_date'],props['end_date']))
status = obj.execute_gp_func(home_dir,"select current_date")
print ("Status of load cost training:" , status)
# Bill process date date params
#status = obj.execute_gp_func(home_dir,"select get_analytic.fn_get_logistics_bill_date_train(%s,%s);"%(props['start_date'],props['end_date']))
print ("Status of bill process date training:" , status)
if __name__ == '__main__':
main()
在执行时,我看到print-within函数具有正确的date值。但是,它进入包装器,然后返回None值
看起来好像您的包装器只调用函数,但对返回值不做任何操作:
def gp_info_decorator(function):
def wrapper(*args, **kwargs):
#print args[0].home_dir
#print args[0].query
#with open(sys.stdout, "w") as devnull:
result = function(*args, **kwargs) # <-- remember result
"""
... # <-- removed for readability
"""
print ("Debug 3--->", wrapper)
return result # <-- return result
return wrapper
@gp_info_decorator
def fn1():
return 5
r = fn1()
print(r) # Output: 5
实际上,您永远不会返回装饰器中的函数!我们需要安装psycopg2吗?看看如何创建一个@PeterWood谢谢,我会记住的。我的问题更多的是修改来自装饰者的值。所以,psycopg2不是必需的。谢谢你指出这一点。我对使用装饰师还不熟悉。非常感谢你的指导。
def gp_info_decorator(function):
def wrapper(*args, **kwargs):
#print args[0].home_dir
#print args[0].query
#with open(sys.stdout, "w") as devnull:
result = function(*args, **kwargs) # <-- remember result
"""
... # <-- removed for readability
"""
print ("Debug 3--->", wrapper)
return result # <-- return result
return wrapper
@gp_info_decorator
def fn1():
return 5
r = fn1()
print(r) # Output: 5