How to Build, Package, and Publish a Python ‘Mofish’ CLI Library with Click
This article walks through creating the Mofish Python library, using Click to build a command‑line tool that shows days remaining to holidays, packaging it with setuptools, and publishing the package to PyPI for easy installation and use.
The article introduces the Mofish Python library, a command‑line tool that displays the current date, weekday, and the number of days remaining until various Chinese holidays, while encouraging users to take short breaks.
Code Overview
The main script imports datetime, click, and zhdate, defines helper functions to calculate distances to holidays, and uses colorama for colored output.
# -*- 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
# ... similar calculations for other holidays omitted for brevity ...
time_ = [
{"v_": 5 - 1 - today.weekday(), "title": "周末"},
{"v_": distance_year, "title": "元旦"},
{"v_": distance_big_year, "title": "过年"},
# other holiday entries ...
]
time_ = sorted(time_, key=lambda x: x['v_'])
return time_
@click.command()
def cli():
from colorama import init, Fore
init(autoreset=True)
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,就等于为你的家庭多赚一万块。少上班,多摸鱼.
"
print(f"{Fore.RED}{tips_}")
print(f"{Fore.YELLOW} 摸鱼办")
if __name__ == '__main__':
cli()Using the click library
Click simplifies the creation of command‑line interfaces compared with the built‑in argparse module, providing decorators such as @click.command() and @click.option() for concise definitions.
Installation
pip install clickSimple click example
Official documentation example:
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 _ in range(count):
click.echo(f'Hello {name}!')
if __name__ == '__main__':
hello()Packaging with setuptools
Install packaging tools:
pip install setuptools
pip install twineBuild and upload the package:
python setup.py sdist
twine upload dist/*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',
author='PY-GZKY',
author_email='[email protected]',
url='https://github.com/PY-GZKY/Mofish',
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
moyuSummary
The article demonstrates how to wrap personal code into a reusable Python package, use Click to create a friendly CLI, and publish the package to PyPI so that anyone can install and run the tool with a single command.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
