Package: zyf
安装pip install zyf或者pip install zyf -ihttps://pypi.python.org/simple使用函数计时示例1：timeitfromzyf.timerimporttimeit@timeitdefsleep(seconds:int):time.sleep(seconds)sleep()运行>> sleep(1)
Function sleep -> takes 1.001 seconds示例2：Timeitfromzyf.timerimporttimeit,Timeit@Timeit(prefix='跑步')defrun():time.sleep(3)run()运行跑步 -> takes 3.000 seconds示例3：repeat_timeitfromzyf.timerimportrepeat_timeit@repeat_timeit(number=5)deflist_insert_time_test():l=[]foriinrange(10000):l.insert(0,i)@repeat_timeit(repeat=3,number=5)deflist_append_time_test():l=[]foriinrange(1000000):l.append(i)returnl@repeat_timeit(number=5,print_detail=True)deflist_gen_time_test():l=[iforiinrange(1000000)]returnl@repeat_timeit(repeat=3,number=5,print_detail=True)deflist_extend_time_test():l=[]foriinrange(1000000):l.extend([i])@repeat_timeit(repeat=3,number=5,print_detail=True,print_table=True)deflist_range_time_test():l=list(range(1000000))运行>>list_insert_time_test()Functionlist_insert_time_test->5functioncalls:averagetakes0.097seconds>>list_append_time_test()Functionlist_append_time_test->3trialswith5functioncallspertrial:averagetrial3.269seconds.averagefunctioncall0.654seconds>>list_gen_time_test()TimeSpendof5functioncalls:Function->list_gen_time_test:total1.550seconds,average0.310secondsAverage:0.310seconds>>list_extend_time_test()TimeSpendof3trialswith5functioncallspertrial:Function->list_extend_time_test:best:3.289seconds,worst:3.626seconds,average:3.442secondsAveragetrial:3.442seconds.Averagefunctioncall:0.688seconds>>list_range_time_test()TimeSpendof3trialswith5functioncallspertrial:+----------------------+---------------+---------------+---------------+-----------------------+|Function|Besttrial|Worsttrial|Averagetrial|Averagefunctioncall|+----------------------+---------------+---------------+---------------+-----------------------+|list_range_time_test|0.640seconds|0.714seconds|0.677seconds|0.135seconds|+----------------------+---------------+---------------+---------------+-----------------------+示例4：构建列表效率对比fromzyf.timerimportrepeat_timeit@repeat_timeit(number=3)deflist_insert_time_test():l=[]foriinrange(100000):l.insert(0,i)@repeat_timeit(number=5)deflist_extend_time_test():l=[]foriinrange(100000):l.extend([i])@repeat_timeit(number=5)deflist_append_time_test():l=[]foriinrange(100000):l.append(i)returnl@repeat_timeit(number=5)deflist_gen_time_test():l=[iforiinrange(100000)]returnl@repeat_timeit(number=5)deflist_range_time_test():l=list(range(100000))if__name__=='__main__':list_range_time_test()list_gen_time_test()list_append_time_test()list_extend_time_test()list_insert_time_test()运行结果Functionlist_range_time_test->5functioncalls:averagetakes0.012seconds
Functionlist_gen_time_test->5functioncalls:averagetakes0.017seconds
Functionlist_append_time_test->5functioncalls:averagetakes0.038seconds
Functionlist_extend_time_test->5functioncalls:averagetakes0.067seconds
Functionlist_insert_time_test->3functioncalls:averagetakes13.747seconds请求头user_agent功能说明支持获取各类请求头，包含移动端和PC端浏览器，可以指定获取某类请求头，也可以随机获取。使用示例fromzyf.user_agentimportUserAgentua=UserAgent()print(ua.random)print(ua.chrome)print(ua.firefox)print(ua.opera)print(ua.uc)print(ua.mobile)输出Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/536.3(KHTML,likeGecko)Chrome/19.0.1061.1Safari/536.3
Mozilla/5.0(WindowsNT6.2)AppleWebKit/536.6(KHTML,likeGecko)Chrome/20.0.1090.0Safari/536.6
Mozilla/5.0(X11;U;Linuxx86_64;zh-CN;rv:1.9.2.10)Gecko/20100922Ubuntu/10.10(maverick)Firefox/3.6.10
Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;en)Opera9.50
Openwave/UCWEB7.0.2.37/28/999
Mozilla/5.0(iPad;U;CPUOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5文件操作scan_directory_contents功能说明扫描指定文件夹内所有文件，输出文件路径使用示例fromzyf.fileimportscan_directory_contentsforfileinscan_directory_contents('D:/python/data'):print(file)# 可以指定后缀forfileinscan_directory_contents('D:/python/data',suffix='.csv'):print(file)count_word_freq功能说明对文献.xlsx中关键词列的进行词频统计，可指定单词分隔符，默认为`; ',也可指定输出词频统计列名，默认为freq和word。使用示例fromzyf.fileimportcount_word_freqcount_word_freq('文献.xlsx',col_name='关键词',sep='; ',to_col_freq='频数',to_col_word='单词',to_file='文献_关键词_统计.xlsx')颜色相关color功能说明打印功能扩展，添加颜色输出使用示例fromzyf.colorimportprint_color,Foregroundprint_color("这是什么颜色",foreground=Foreground.Red)print_color("这是什么颜色",foreground=Foreground.White)print_color("这是什么颜色",foreground=Foreground.Green)print_color("这是什么颜色",foreground=Foreground.Black)print_color("这是什么颜色",foreground=Foreground.Blue)print_color("这是什么颜色",foreground=Foreground.Cyan)print_color("这是什么颜色",foreground=Foreground.Purplish_red)print_color("这是什么颜色",foreground=Foreground.Yellow)数据下载政策数据下载根据关键词对政策数据库进行搜索，并将搜索到的政策数据进行下载及字段解析，存储到文件中。使用说明国务院政策文件库
    1. 设置settings中的请求参数 -> gov_policy_params
    2. 运行代码
北大法宝
    1. 网页登陆之后将cookie复制，修改settings中的cookie信息
    2. 根据你的检索词和检索时间修改settings中的QueryBased64Request和Year
    3. 运行代码
律商网
    1. 网页登陆之后将cookie复制，修改settings中的cookie信息
    2. 根据你的检索信息修改settings中的keyword/start/end/page_size
    3. 运行代码注：北大法宝和律商网需要有会员账号才能全部完整政策信息, 所以需要设置cookie信息。使用示例国务院政策数据下载defgov_policy_demo():fromzyf.crawler.policy.goverment_policyimportGovPolicyCrawlerspider=GovPolicyCrawler()spider.run(keyword='疫情',issue_depart=['国务院','国务院部门','国务院公报'],page_size=50)北大法宝政策数据下载defpkulaw_policy_demo():fromzyf.crawler.policy.pkulaw_policyimportPkulawdCrawlerpkulaw_request_params={'cookie':None,'query_base64_request':{'疫情':'eyJGaWVsZE5hbWUiOm51bGwsIlZhbHVlIjpudWxsLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjowLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoyLCJDaGlsZE5vZGVzIjpbeyJGaWVsZE5hbWUiOiJLZXl3b3JkU2VhcmNoVHJlZSIsIlZhbHVlIjpudWxsLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjowLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoxLCJDaGlsZE5vZGVzIjpbeyJGaWVsZE5hbWUiOiJDaGVja0Z1bGxUZXh0IiwiVmFsdWUiOiLnlqvmg4UiLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjoxLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoyLCJDaGlsZE5vZGVzIjpbXSwiQW5hbHl6ZXIiOiJpa19zbWFydCIsIkJvb3N0IjoiMC4xIiwiTWluaW11bV9zaG91bGRfbWF0Y2giOm51bGx9LHsiRmllbGROYW1lIjoiU291cmNlQ2hlY2tGdWxsVGV4dCIsIlZhbHVlIjoi55ar5oOFIiwiUnVsZVR5cGUiOjQsIk1hbnlWYWx1ZVNwbGl0IjoiXHUwMDAwIiwiV29yZE1hdGNoVHlwZSI6MSwiV29yZFJhdGUiOjAsIkNvbWJpbmF0aW9uVHlwZSI6MiwiQ2hpbGROb2RlcyI6W10sIkFuYWx5emVyIjpudWxsLCJCb29zdCI6bnVsbCwiTWluaW11bV9zaG91bGRfbWF0Y2giOm51bGx9XSwiQW5hbHl6ZXIiOm51bGwsIkJvb3N0IjpudWxsLCJNaW5pbXVtX3Nob3VsZF9tYXRjaCI6bnVsbH1dLCJBbmFseXplciI6bnVsbCwiQm9vc3QiOm51bGwsIk1pbmltdW1fc2hvdWxkX21hdGNoIjpudWxsfQ==',},'year':[2003,2004],'page_size':100,}crawler=PkulawdCrawler(**pkulaw_request_params)crawler.run()律商网政策数据下载deflexis_policy_demo():fromzyf.crawler.policy.lexis_policyimportLexisNexisCrawlerlexis_request_params={'cookie':None,'keywords':'疫情','start':'2020-01-01','end':'2020-12-31','page_size':100,}crawler=LexisNexisCrawler(**lexis_request_params)crawler.run()综合示例配置文件：settings.py# 国务院gov_policy_params={'keyword':'医疗联合体','min_time':None,'max_time':None,'issue_depart':['国务院','国务院部门','国务院公报'],'searchfield':'title:content:summary','sort':'pubtime','page_size':50,'to_file':None}# 北大法宝pkulaw_request_params={'cookie':None,'query_base64_request':{'疫情':'eyJGaWVsZE5hbWUiOm51bGwsIlZhbHVlIjpudWxsLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjowLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoyLCJDaGlsZE5vZGVzIjpbeyJGaWVsZE5hbWUiOiJLZXl3b3JkU2VhcmNoVHJlZSIsIlZhbHVlIjpudWxsLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjowLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoxLCJDaGlsZE5vZGVzIjpbeyJGaWVsZE5hbWUiOiJDaGVja0Z1bGxUZXh0IiwiVmFsdWUiOiLnlqvmg4UiLCJSdWxlVHlwZSI6NCwiTWFueVZhbHVlU3BsaXQiOiJcdTAwMDAiLCJXb3JkTWF0Y2hUeXBlIjoxLCJXb3JkUmF0ZSI6MCwiQ29tYmluYXRpb25UeXBlIjoyLCJDaGlsZE5vZGVzIjpbXSwiQW5hbHl6ZXIiOiJpa19zbWFydCIsIkJvb3N0IjoiMC4xIiwiTWluaW11bV9zaG91bGRfbWF0Y2giOm51bGx9LHsiRmllbGROYW1lIjoiU291cmNlQ2hlY2tGdWxsVGV4dCIsIlZhbHVlIjoi55ar5oOFIiwiUnVsZVR5cGUiOjQsIk1hbnlWYWx1ZVNwbGl0IjoiXHUwMDAwIiwiV29yZE1hdGNoVHlwZSI6MSwiV29yZFJhdGUiOjAsIkNvbWJpbmF0aW9uVHlwZSI6MiwiQ2hpbGROb2RlcyI6W10sIkFuYWx5emVyIjpudWxsLCJCb29zdCI6bnVsbCwiTWluaW11bV9zaG91bGRfbWF0Y2giOm51bGx9XSwiQW5hbHl6ZXIiOm51bGwsIkJvb3N0IjpudWxsLCJNaW5pbXVtX3Nob3VsZF9tYXRjaCI6bnVsbH1dLCJBbmFseXplciI6bnVsbCwiQm9vc3QiOm51bGwsIk1pbmltdW1fc2hvdWxkX21hdGNoIjpudWxsfQ==',},'year':[2003,2004],'page_size':100,}# 律商网lexis_request_params={'cookie':None,'keywords':'疫情','start':'2020-01-01','end':'2020-12-31','page_size':100,}使用示例importsettingsdefpolicy_spider():print('请选择政策来源: 1. 国务院政策文件库 2.北大法宝 3.律商网 4. 新冠疫情数据（卫健委）')choice=input('请选择政策来源（数字）>> ')ifchoice=='1':fromzyf.crawler.policy.goverment_policyimportGovPolicyCrawlercrawler=GovPolicyCrawler()crawler.run(**settings.gov_policy_params)elifchoice=='2':fromzyf.crawler.policy.pkulaw_policyimportPkulawdCrawlercrawler=PkulawdCrawler(**settings.pkulaw_request_params)crawler.run()elifchoice=='3':fromzyf.crawler.policy.lexis_policyimportLexisNexisCrawlercrawler=LexisNexisCrawler(**settings.lexis_request_params)crawler.run()else:raiseException('输入的政策来源不正确')图片下载使用说明使用示例fromzyf.colorimportprint_colordefstart_spider():print_color('高清壁纸：1. NET牛人(https://ss.netnr.com/) 2. 彼岸图网(https://pic.netbian.com/)')choice=input('请选择壁纸来源 >> ')ifchoice=='1':fromzyf.crawler.image.netnrimportNetnrCrawlercrawler=NetnrCrawler(dir_path='images/netnr')elifchoice=='2':fromzyf.crawler.image.netbianimportNetbianCrawlercrawler=NetbianCrawler(dir_path='images/netbian')else:raiseException('输入的壁纸来源不正确')crawler.run()if__name__=='__main__':start_spider()数据库连接DBPoolHelper使用说明提供sqlite3、mysql、postgresql、sqkserver连接池，方便操作，该功能使用依赖于dbutils，需要提前安装，另外，需要安装对应数据库的第三方依赖postgressql  -> psycopg2mysql  ->  pymysqlsqlite  -> sqlite3使用示例fromzyf.dbimportDBPoolHelperdb1=DBPoolHelper(db_type='postgressql',dbname='student',user='postgres',password='0000',host='localhost',port=5432)db2=DBPoolHelper(db_type='mysql',dbname='student',user='root',password='0000',host='localhost',port=3306)db3=DBPoolHelper(db_type='sqlite3',dbname='student.db')MongoHelper使用说明为mongodb操作提供便利，需要安装pymongo使用示例fromzyf.dbimportMongoHelpermongo=MongoHelper(mongo_db='flask',mongo_uri='localhost')data=mongo.read('label')print(data.head())condition={"药品ID":509881}data=mongo.dbFind('label',condition)print(data)foriindata:print(i)foriteminmongo.findAll():print(item)远程控制ParamikoHelper使用说明在Python代码中直接使用SSH协议对远程服务器、网络设备执行命令操作、文件上传下载服务。使用示例fromzyf.remote_controlimportParamikoHelperhostname='X.X.X.X'username='XXX'password='XXX'remotepath1='XXX'localpath1='XXX'remotepath2='XXX'localpath2='XXX'client=ParamikoHelper(hostname=hostname,username=username,password=password)content,code=client.exec_command("display ip interface brief")client.download_file(remotepath1,localpath1)client.put_file(localpath2,remotepath2)client.__close__()print(content,code)