Teach you how to carry out the packaging and release of the mofish library (moyu library)

Hello everyone, I am Pippi.

I. Introduction

A few days ago, Boss Wu recommended a Moyu library to me. It turned out to be a Python library. I was shocked and felt that Sting should be missing.

Teach you how to carry out the packaging and release of the mofish library (moyu library)

You must know that he has written an article about fishing before. Interested friends, you can go to: Teach you how to use Python to create a countdown interface for fishing.

Now he has turned this Moyu into a Python library. An article has been published on the use of this library. You can go to: Inventory a Python library called Moyu, let's touch the fish together!

In the comment area, I saw the message of the [somewhat interesting] boss, as shown in the following figure:

Teach you how to carry out the packaging and release of the mofish library (moyu library)

It probably means to encapsulate the code written by yourself into a Python library, which can be used by everyone. Here is the arrangement. This article is about how to package and publish it. Let’s take a look!

2. Code

First, prepare the code. This code has been shared in the previous article. I won't repeat it 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: '星期一',
        1: '星期二',
        2: '星期三',
        3: '星期四',
        4: '星期五',
        5: '星期六',
        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": "周末"},  # 距离周末
        {"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=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}月{today.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)
    for t_ in time_:
        print(f'\t\t {Fore.RED}距离{t_.get("title")}还有: {t_.get("v_")}天')
    tips_ = '''
    [友情提示] 三甲医院 ICU 躺一天平均费用大概一万块。
    你晚一天进 ICU,就等于为你的家庭多赚一万块。少上班,多摸鱼。\n
    '''
    print(f'{Fore.RED}{tips_}')
    print(f'\t\t\t\t\t\t\t{Fore.YELLOW} 摸鱼办')


if __name__ == '__main__':
    cli()
复制代码

Use of click library

Notice that our code in the file above uses the click library.

Python has a built-in Argparse standard library for creating command lines, but it is a bit cumbersome to use. Compared with Argparse, Click is insignificant.

Install

pip install click
复制代码

Simple use of click

An introductory example from the 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()
复制代码
  • @click.command() makes the function 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 the python built-in print method.

Instructions:

Print 10 leather bosses.

$ python hello.py --count 10 --name 皮老板    # 指定 count 和 name 的值
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
Hello 皮老板!
复制代码

setuptool package release

Install dependencies

pip install setuptools
pip install twine
复制代码

Package upload

python setup.py sdist
twine upload dist/*
复制代码

Log in to the pypi account and publish the python library

setup.py example

from setuptools import setup, find_packages

description = '你好,摸鱼人,工作再累,一定不要忘记摸鱼哦! 有事没事起身去茶水间去廊道去天台走走,别老在工位上坐着。多喝点水,钱是老板的,但命是自己的!'

setup(
    name='mofish', # 库名
    version='1.0.0', # 版本号
    description=description, # 短简介
    long_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 版本限制
    author='PY-GZKY', # 作者
    author_email='[email protected]', # 邮件
    url='https://github.com/PY-GZKY/Mofish', # git
    license='MIT', # 开源协议
    packages=find_packages(), # 包
    include_package_data=True,
    entry_points="""
        [console_scripts]
        moyu=src.main:cli
    """, # 启动命令行脚本的文件
    install_requires=[
        'click>=6.7',
        'zhdate'
    ], # 限制安装库的版本

)
复制代码

install and use

pip install mofish
moyu
复制代码

Code repository address:

https://github.com/PY-GZKY/Mofish
复制代码

Teach you how to carry out the packaging and release of the mofish library (moyu library)

Summarize

Hello everyone, I'm Pippi. This article mainly gives you an inventory of a Python library. Based on this library, this article introduces how to encapsulate the code you wrote into a Python library, package and upload it, and publish it to pypi, so that everyone can use your library later.

Finally, I would like to thank [Boss Wu] for his ideas and code support, and thank [somewhat interesting] for his needs.

[Friendly Reminder] The average cost of a day in the ICU of a tertiary hospital is about 10,000 yuan. If you enter the ICU one day later, you will earn an extra 10,000 yuan for your family. Work less, fish more.

Teach you how to carry out the packaging and release of the mofish library (moyu library)

Little friends, hurry up and practice it! If you encounter any problems during the learning process, please add me as a friend, and I will pull you into the Python learning exchange group to discuss and learn together.

Guess you like

Origin juejin.im/post/7078470961892163591