1.前言前几天吴老师给我推荐了一个Mofish库。.要知道他之前写过一篇关于钓鱼的文章。有兴趣的可以去:手把手教你用Python制作钓鱼倒计时界面。现在他已经把这个魔域做成了一个Python库。他之前发表过一篇关于这个库的使用的文章。意思】大佬的留言,如下图:大概意思是把我写的代码打包成一个Python库,大家可以用。这是安排。这篇文章是关于如何打包发布的。一起来看看吧!2.代码首先,准备代码。这段代码在之前的文章中已经分享过了。在此不再赘述。代码在这里。#-*-coding:utf-8-*-importdatetimeimportclickfromzhdateimportZhDateaslunar_datedefget_week_day(date):week_day_dict={0:'星期一',1:'星期二',2:'星期三',3:'星期四',4:'星期五',5:'星期六',6:'星期天',}day=date.weekday()returnweek_day_dict[day]deftime_parse(today):distance_big_year=(lunar_date(today.year,1,1).to_datetime().date()-今天).daysdistance_big_year=distance_big_yearifdistance_big_year>0else(lunar_date(today.year+1,1,1).to_datetime().date()-今天).daysdistance_5_5=(lunar_date(today.year,5,5).to_datetime().date()-今天).daysdistance_5_5=distance_5_5ifdistance_5_5>0else(lunar_date(today.year+1,5,5).to_datetime().date()-今天).daysdistance_8_15=(lunar_date(today.year,8,15).to_datetime().date()-今天).daysdistance_8_15=distance_8_15ifdistance_8_15>0else(lunar_date(today.year+1,8,15).to_datetime().date()-今天).daysdistance_year=(datetime.datetime.strptime(f"{today.year}-01-01","%Y-%m-%d").date()-today).daysdistance_year=distance_yearifdistance_year>0else(datetime.datetime.strptime(f"{today.year+1}-01-01","%Y-%m-%d").date()-today).daysdistance_4_5=(datetime.datetime.strptime(f"{today.year}-04-05","%Y-%m-%d").date()-今天).daysdistance_4_5=distance_4_5ifdistance_4_5>0else(datetime.datetime.strptime(f"{today.year+1}-04-05","%Y-%m-%d").date()-今天).daysdistance_5_1=(datetime.datetime.strptime(f"{today.year}-05-01","%Y-%m-%d").date()-今天)。distance_5_1=distance_5_1如果distance_5_1>0else(datetime.datetime.strptime(f"{today.year+1}-05-01","%Y-%m-%d").date()-今天)。daysdistance_10_1=(datetime.datetime.strptime(f"{today.year}-10-01","%Y-%m-%d").date()-今天).daysdistance_10_1=distance_10_1ifdistance_10_1>0else(datetime.datetime.strptime(f"{today.year+1}-10-01","%Y-%m-%d").date()-今天).daystime_=[{"v_":5-1-today.weekday(),"title":"Weekend"},#离周末的距离{"v_":distance_year,"title":"元旦"},#距离元旦{"v_":distance_big_year,"title":"中国新年"},#距离中国新年{"v_":distance_4_5,"title":"清明节"},#距离中国新年清明{"v_":distance_5_1,"title":"劳动节"},#劳动距离{"v_":distance_5_5,"title":"端午节"},#距离端午节{"v_":distance_8_15,"title":"中秋节"},#距离中秋节{"v_":distance_10_1,"title":"国庆节"},#距离国庆节]time_=sorted(time_,key=lambdax:x['v_'],reverse=False)returntime_@click.command()defcli():"""渔夫你好,工作再累也别忘了去摸鱼!”“”来回mcoloramaimportinit,Foreinit(autoreset=True)#初始化,并设置颜色设置自动恢复print()today=datetime.date.today()now_=f"{today.year}{today.month}month{今天.day}"week_day_=get_week_day(today)print(f'\t\t{Fore.GREEN}{now_}{week_day_}')str_='''渔夫你好,工作再累也别忘了渔夫!有事就起身去茶水间,走到楼道,上楼顶。不要只是坐着喝更多的水。老板的钱是他的,命是他的!'''print(f'{Fore.RED}{str_}')time_=time_parse(today)fort_intime_:print(f'\t\t{Fore.RED}distance{t_.get("title")}and:{t_.get("v_")}days')tips_='''【友情提示】平均躺在三甲医院ICU一天的费用在1万元左右。晚一天进ICU,相当于给家人多赚一万元。少工作,多钓鱼。\n'''print(f'{Fore.RED}{tips_}')print(f'\t\t\t\t\t\t\t{Fore.YELLOW}钓鱼处')if__name__=='__main__':cli()点击库的使用注意上面的文件代码使用的是点击库。Python内置了用于创建命令行的Argparse标准库,但使用起来有点麻烦,Click与Argparse相比根本算不上什么。安装pipinstallclickclick使用官方文档的简单例子:importclick@click.command()@click.option('--count',default=1,help='Numberofgreetings.')@click.option('--name',prompt='你的名字',help='要问候的人。')defhello(count,name):"""一个简单的程序,总共问候NAMECOUNT次。"""forxinrange(count):click.echo('Hello%s!'%name)if__name__=='__main__':hello()@click.command()使函数hello成为命令行界面。@click.option的第一个参数指定命令行选项的名称。click.echo方法类似于python内置的print方法。要使用:打印10个皮革老板。$pythonhello.py--count10--namePiboss#指定计数值和名称HelloPiboss!HelloPiboss!HelloPisboss!HelloPisboss!HelloPisboss!HelloPisanboss!HelloPisanBoss!你好皮老板!皮老板您好!皮老板您好!setuptoolpackagerelease安装依赖pipinstallsetuptoolspipinstalltwinepackageuploadpythonsetup.pysdisttwineuploaddist/*loginpypiaccountandpublishpythonlibrarysetup.pyexamplefromsetuptoolsimportsetup,find_packagesdescription='你好,fishtoucher,不管多累你在工作,别忘了摸鱼!没事就起身去茶水间,去走廊,去屋顶,不要一直坐在办公桌前。多喝水,钱是老板的,生命是自己的!'setup(name='mofish',#库名version='1.0.0',#版本号description=description,#简介long_description_content_type='text/markdown',classifiers=['开发状态::4-Beta','环境::控制台','目标受众::开发人员','目标受众::信息技术','目标受众::系统管理员','许可证::OSI批准::MIT许可证'、'编程语言::Python'、'编程语言::Python::3'、'编程语言::Python::3::仅'、'编程语言::Python::3.7','编程语言::Python::3.8','编程语言::Python::3.9','主题::互联网','主题::软件开发::库::Python模块','主题::系统::集群','主题::系统::分布式计算','主题::系统::监控g','Topic::System::SystemsAdministration',],python_requires='>=3.7',#py版本限制author='PY-GZKY',#authorauthor_email='341796767@qq.com',#emailurl='https://github.com/PY-GZKY/Mofish',#gitlicense='MIT',#开源协议packages=find_packages(),#packageinclude_package_data=True,entry_points="""[console_scripts]moyu=src.main:cli""",#启动命令行脚本文件install_requires=['click>=6.7','zhdate'],#限制安装库版本)安装使用pipinstallmofishmoyu代码仓库地址:https://github.com/PY-GZKY/Mofish总结大家好,我是皮皮。这篇文章主要是给大家盘点一个Python库。本文基于这个库,介绍如何将自己写的代码打包成Python库,打包上传,发布到pypi,方便大家以后使用你的库。本文转载自微信公众号《Python爬虫与数据挖掘》,可通过以下二维码关注。转载本文请联系Python爬虫与数据挖掘公众号。
