标签:false 描述 安全性 __init__ des 位置 href fill src
好处:
1)code不以可编辑的状态被用户接触,对于不懂反编译的一般用户,可提升一定的代码安全性;
2)不需要用户机器上安装 python环境。
3)可以将能够执行不同任务的脚本编译成同一个exe程序,通过传入不同参数实现不同任务。
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
import sys,getopt # 用来解析参数的两个库
class HandleData():
def __init__(self,process_name,log_path=‘‘,excel_path=‘‘,new_excel_path=‘‘):
self.process_name=process_name
self.log_path=log_path
self.excel_path=excel_path
self.new_excel_path=new_excel_path
def deal_with_data(self):
file_obj=open(self.excel_path)
df=pd.read_csv(file_obj)
# df=pd.read_csv(self.excel_path)
df=df.reindex(columns=[‘CNUM‘,‘COMPANY‘,‘C_col‘,‘D_col‘,‘E_col‘,‘F_col‘,‘G_col‘,‘H_col‘],fill_value=None)
df.rename(columns={‘COMPANY‘:‘Company_New‘}, inplace = True)
df=df.dropna(axis=0,how=‘all‘)
df[‘CNUM‘] = df[‘CNUM‘].astype(‘int32‘)
df = df.drop_duplicates(subset=[‘CNUM‘, ‘Company_New‘], keep=‘first‘)
df.to_csv(self.new_excel_path,index=False,encoding=‘GBK‘)
file_obj.close()
def writeLog(self):
with open(self.log_path,"a") as logfile:
logfile.write("\nthat‘s a test log message")
def writeEventLog(self):
with open(r"C:\Users\12078\Desktop\UIPATH_test\0419\EventLogs_Bot1.txt","a") as logfile:
logfile.write("\nno function found" + self.process_name)
def mainprocess(self):
if self.process_name=="deal_with_data":
HandleData.deal_with_data(self)
elif self.process_name=="writeLog":
HandleData.writeLog(self)
else:
HandleData.writeEventLog(self)
if __name__ == "__main__":
process_name=""
log_path = ""
excel_path = ""
new_excel_path = ""
argv=sys.argv[1:]
try:
opts,args = getopt.getopt(argv,"h",["process_name=","log_path=","excel_path=","new_excel_path="])
except getopt.GetoptError:
print(‘cnum_company_data0418.py --process_name processname str --log_path logpath str --excel_path datafilepath str --new_excel_path outputfilepath str‘)
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print(‘cnum_company_data0418.py --process_name processname str --log_path logpath str --excel_path datafilepath str --new_excel_path outputfilepath str‘)
sys.exit()
elif opt == "--process_name":
process_name = arg
elif opt == "--log_path":
log_path = arg
elif opt =="--excel_path":
excel_path = arg
elif opt =="--new_excel_path":
new_excel_path = arg
# print(process_name + "--"+log_path+"--"+excel_path+"--"+new_excel_path+"")
handle=HandleData(process_name,log_path,excel_path,new_excel_path)
handle.mainprocess()
测试生成log message的功能:
C:\Users\12078\Desktop\UIPATH_test\0419>python cnum_company_data0419.py --process_name=writeLog --log_path=C:\Users\12078\Desktop\UIPATH_test\0419\mylog.txt
测试处理excel/csv数据的功能:
C:\Users\12078\Desktop\UIPATH_test\0419>python cnum_company_data0419.py --process_name=deal_with_data --excel_path=C:\Users\12078\Desktop\UIPATH_test\0419\CNUM_COMPANY.csv --new_excel_path=C:\Users\12078\Desktop\UIPATH_test\0419\out.csv
经过测试,脚本没有问题
# 知识点:面向对象,反射,元组解包,不定长参数的函数
import pandas as pd
from pandas import DataFrame,Series
class HandleData():
def deal_with_data(self,paths):
excel_path,new_excel_path=paths
file_obj=open(excel_path)
df=pd.read_csv(file_obj)
df=df.reindex(columns=[‘CNUM‘,‘COMPANY‘,‘C_col‘,‘D_col‘,‘E_col‘,‘F_col‘,‘G_col‘,‘H_col‘],fill_value=None)
df.rename(columns={‘COMPANY‘:‘Company_New‘}, inplace = True)
df=df.dropna(axis=0,how=‘all‘)
df[‘CNUM‘] = df[‘CNUM‘].astype(‘int32‘)
df = df.drop_duplicates(subset=[‘CNUM‘, ‘Company_New‘], keep=‘first‘)
df.to_csv(new_excel_path,index=False,encoding=‘GBK‘)
file_obj.close()
def writeLog(self,log_path):
log_path=log_path[0]
with open(log_path,"a") as logfile:
logfile.write("\nthat‘s a test log message")
def writeEventLog(self,p):
process_name,content=p
with open(r"C:\Users\12078\Desktop\uibot\pytest_mutiply.txt","a") as logfile:
logfile.write("\nno function found" + process_name+"--"+content)
def main(f,*args):
h=HandleData()
if hasattr(h,f):
getattr(h,f)(args)
if __name__ == "__main__":
main(‘writeEventLog‘,‘fuc_test‘,‘ttesthahah‘)
C:\Users\12078\Desktop\UIPATH_test\0419>pyinstaller cnum_company_data0419.py
1.总体框架:
2.第1次调用exe完成第一个任务:
3.第2次调用 exe完成第二个任务:
4.执行结果:
1.Author: Collin_PXY ; wechat: pxy123abc
标签:false 描述 安全性 __init__ des 位置 href fill src
原文地址:https://www.cnblogs.com/Collin-pxy/p/13227861.html