Hi, I’m Pippy.

One, foreword

A few days ago, Boss Wu recommended a fish library to me. It turned out to be a Python library, which surprised me and made me feel that I should be short of sting.

He’s already written an article about fishing. If you’re interested, go to: How to build a Fish-counting interface using Python.

Now he has made this library into a Python library. The use of this library has been published before, and you can go to: A Python library named Fish.

In the comments section, I saw the big guy’s message, as shown in the picture below:

Package your code into a Python library that everyone can use. This article is about how to package and distribute your code.

Second, the code

First, prepare the code, which has been shared in previous articles and will not be described here. The code is here.

# -*- coding: utf-8 -*- import datetime import click from zhdate import ZhDate as lunar_date def get_week_day(date): Week_day_dict = {0: 'Monday', 1: 'on Tuesday, 2:' on Wednesday, 3: 'on Thursday, 4:' Friday, 5: 'Saturday, 6: } day = date.weekday() return week_day_dict[day] def time_parse(today): distance_big_year = (lunar_date(today.year, 1, 1).to_datetime().date() - today).days distance_big_year = distance_big_year if distance_big_year > 0 else ( lunar_date(today.year + 1, 1, 1).to_datetime().date() - today).days distance_5_5 = (lunar_date(today.year, 5, 5).to_datetime().date() - today).days distance_5_5 = distance_5_5 if distance_5_5 > 0 else ( lunar_date(today.year + 1, 5, 5).to_datetime().date() - today).days distance_8_15 = (lunar_date(today.year, 8, 15).to_datetime().date() - today).days distance_8_15 = distance_8_15 if distance_8_15 > 0 else ( lunar_date(today.year +  1, 8, 15).to_datetime().date() - today).days distance_year = (datetime.datetime.strptime(f"{today.year}-01-01", "%Y-%m-%d").date() - today).days distance_year = distance_year if distance_year > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-01-01", "%Y-%m-%d").date() - today).days distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05", "%Y-%m-%d").date() - today).days distance_4_5 = distance_4_5 if distance_4_5 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-04-05", "%Y-%m-%d").date() - today).days distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01", "%Y-%m-%d").date() - today).days distance_5_1 = distance_5_1 if distance_5_1 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-05-01", "%Y-%m-%d").date() - today).days distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01", "%Y-%m-%d").date() - today).days distance_10_1 = distance_10_1 if distance_10_1 > 0 else ( datetime.datetime.strptime(f"{today.year + 1}-10-01", "%Y-%m-%d").date() - today).days time_ = [ {"v_": 5-1 - today. Weekday (), "title" : "weekend"}, # distance weekend {" v_ ": distance_year," title ":" New Year's day "}, {# from New Year's day "v_" : Distance_big_year, "title" : "New Year"}, # distance for {" v_ ": distance_4_5," title ":" qingming festival "}, {# distance qingming festival "v_" : distance_5_1, "title" : "Labor day"}, # from labor {" v_ ": distance_5_5," title ":" the Dragon Boat Festival "}, # distance dragon boat {" v_ ": distance_8_15," title ":" Mid-Autumn festival "}, # distance Mid-Autumn {" v_ ": Distance_10_1, "title": "National Day "}, # remote day] time_ = sorted(time_key =lambda x: X ['v_'], reverse=False) return time_ @click.command() def cli(): "" "" from colorama import init, Fore init(autoreset=True) Print () today = datetime.date.today() now_ = f"{today.year} {today.month} month {today.day} "week_day_ = Get_week_day (today) print(f'\t\t {Fore.GREEN}{now_} {week_day_}') str_ = "" Something is nothing to get up to go to the tea room to walk the corridor to the roof, do not sit on the station. Drink more water, money is the boss, but life is their own! "' print (f '{Fore. RED} {str_}') time_ = time_parse (today) for t_ in time_ : print (f 't \ \ t {Fore. RED} from {t_. Get (" title")} are: {t_. Get (" v_ ")} days') tips_ = ' '[attention] 3 armour hospital ICU lying average cost about $ten thousand a day. Every day you stay in the ICU, it's $10,000 more for your family. Work less and fish more. \ n "' print (f '{Fore. RED} {tips_}') print (f '\ t \ \ t \ \ t t t t \ \ t {Fore. YELLOW} fishing do') if __name__ = =" __main__ ': cli ()Copy the code

Use of the Click library

Notice that our file code above uses the Click library.

Python has a built-in standard library for Argparse to create command lines, but it’s a bit cumbersome to use, and Click is nothing compared to Argparse.

The installation

pip install click
Copy the code

Simple use of Click

An example of how to get started with official documentation:

import click @click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo('Hello %s! ' % name) if __name__ == '__main__': hello()Copy the code
  • @click.command() makes hello a command line interface.
  • The first argument to @click.option specifies the name of the command-line option.
  • The click.echo method is similar to Python’s built-in print method.

Usage:

Print 10 leather bosses.

$python hello. Py --count 10 --name Hello boss! Hello boss! Hello boss! Hello boss! Hello boss! Hello boss! Hello boss! Hello boss! Hello boss!Copy the code

Setuptool is packaged and distributed

Install dependencies

pip install setuptools
pip install twine
Copy the code

Packaging upload

python setup.py sdist
twine upload dist/*
Copy the code

Log in to the pypi account and publish the Python library

Setup. Py example

From setuptools import setup, find_packages description = 'Hello, don't forget to touch fish! Something is nothing to get up to go to the tea room to walk the corridor to the roof, do not sit on the station. Drink more water, money is the boss, but life is their own! 'setup(name='mofish', # library name=' 1.0.0', # version name=' description ', # description_content_type='text/markdown', classiFIERS =['Development Status :: 4-beta ', 'Environment :: Console', 'Intended Audience :: Developers', 'Intended Audience :: Information Technology', 'Intended Audience :: System Administrators', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3 :: Only', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Topic :: Internet', 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: System :: Clustering', 'Topic :: System :: Distributed Computing', 'Topic :: System :: Monitoring', 'Topic :: System :: Systems Administration',], python_requires='>=3.7', # py version limitation author=' py-gzky ', # author_email='[email protected]', # git license='MIT', # packages=find_packages(), # include_package_data=True, entry_points="" [console_scripts] moyu=src.main:cli """, # start the command line script file install_requires=['click>=6.7', 'zhdate'], # limit the installation library version)Copy the code

Installation and use

pip install mofish
moyu
Copy the code

Code repository address:

https://github.com/PY-GZKY/Mofish
Copy the code

conclusion

Hello, I’m Pipi. This article describes how to package your code into a Python library, upload it, and publish it to PYPI so that everyone can use your library.

Finally, thank [Boss Wu] boss for his ideas and code support, thank [somewhat interesting] boss for his requirements.

The average cost of a day’s INTENSIVE care unit (ICU) bed in a grade-a hospital is about 10,000 yuan. Every day you stay in the ICU, it’s $10,000 more for your family. Work less and fish more.

Friends, quickly use the practice! If you have any problems during the learning process, please add me as a friend and I will invite you to join the Python Learning exchange group to discuss and learn together.