#coding:utf8 from commonutil import * import psycopg2 import sys entity_circle = [["label_guest_wintenderer","is_wintenderer_label_inference"]] def save(list): conn=psycopg2.connect(database="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101",port="5432") cur=conn.cursor() #创建指针对象 for table in list: source = table[0] result_bak = source+"_bak" # 创建表 cur.execute(" SELECT to_regclass('"+result_bak+"') is null ") flag = cur.fetchall()[0][0] if flag: cur.execute(" create table "+result_bak+"(entity_id text,label int,rule_id text)") cur.execute(" insert into "+result_bak+"(entity_id ,label,rule_id) select entity_id,label,rule_id from "+source+" ") else: cur.execute(" delete from "+result_bak) cur.execute(" insert into "+result_bak+"(entity_id,label,rule_id) select entity_id,label,rule_id from "+source+" ") conn.commit() conn.close() def cir(list,expectation=0.7): conn=psycopg2.connect(database="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101",port="5432") cur=conn.cursor() #创建指针对象 for table in list: source = table[0] des = table[1] cur.execute(" delete from "+source) cur.execute(" insert into "+source+"(entity_id,label,rule_id) select entity_id,case when expectation>"+str(expectation)+" then 1 else 0 end,'circle' from "+des) conn.commit() conn.close() def iterate(list,expectation=0.7): conn=psycopg2.connect(database="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101",port="5432") cur=conn.cursor() #创建指针对象 for table in list: source = table[0] des = table[1] sql = " delete from "+source+" S where exists(select 1 from "+des+" E where S.entity_id=E.entity_id and E.id in (select variable_id from dd_graph_variables_holdout)) " cur.execute(sql) sql = " insert into "+source+"(entity_id,label,rule_id) select A.entity_id,case when A.expectation>0.8 then 1 when A.expectation<0.2 then -1 else B.label end,'iterate' from "+des+" A,"+source+"_bak B where A.entity_id=B.entity_id and A.id in (select variable_id from dd_graph_variables_holdout) " cur.execute(sql) conn.commit() conn.close() if __name__=="__main__": args = sys.argv if len(args)>0: if args[1]=="save": save(entity_circle) elif args[1]=="cir": cir(entity_circle) elif args[1]=="iterate": iterate(entity_circle) else: pass