xubin-duyi-utils 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ __pycache__
@@ -0,0 +1,7 @@
1
+ ## 工程描述
2
+
3
+ 这是一个Python示例工程,旨在通过这个工程学习Python工程化的一些技术。我是一个Python小白,刚刚学习完Python的语言部分,对Python的工程化完全不懂,所以我希望通过这个工程来理解Python的整个工程化。
4
+
5
+ ## 沟通规范
6
+
7
+ **非常重要:当用户提问时,不能修改任何东西,不能新增任何东西,不能删除任何东西,仅回答用户问题即可**
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.5
2
+ Name: xubin_duyi-utils
3
+ Version: 0.1.3
4
+ Summary: 一个用于学习 Python 公共库构建和发布的示例工程
5
+ Requires-Python: >=3.14
6
+ Requires-Dist: markdown>=3.5
7
+ Requires-Dist: python-dateutil>=2.8
8
+ Description-Content-Type: text/markdown
9
+
10
+ # duyi-utils
11
+
12
+ 一个用于学习 Python 公共库构建和发布的示例工程。
13
+
14
+ ## 功能
15
+
16
+ - **日期工具** — 日期格式化、日期差值计算(依赖 `python-dateutil`)
17
+ - **Markdown 工具** — Markdown 转 HTML、去除 Markdown 标记(依赖 `Markdown`)
18
+ - **问候工具** — 简单的个性化问候
19
+
20
+ ## 安装
21
+
22
+ ```bash
23
+ pip install duyi-utils
24
+ ```
25
+
26
+ ## 使用
27
+
28
+ ```python
29
+ from duyi_utils import format_date, days_until, to_html, strip_markdown
30
+ from datetime import date
31
+
32
+ # 日期工具
33
+ format_date(date(2026, 6, 1)) # => "2026年06月01日"
34
+ days_until("2026-10-01") # => 剩余天数
35
+
36
+ # Markdown 工具
37
+ to_html("**Hello**") # => "<p><strong>Hello</strong></p>"
38
+ strip_markdown("**Hello**") # => "<strong>Hello</strong>"
39
+
40
+ # 问候工具
41
+ from duyi_utils.greeter import greet
42
+ greet("小明") # => "你好, 小明!"
43
+ ```
@@ -0,0 +1,34 @@
1
+ # duyi-utils
2
+
3
+ 一个用于学习 Python 公共库构建和发布的示例工程。
4
+
5
+ ## 功能
6
+
7
+ - **日期工具** — 日期格式化、日期差值计算(依赖 `python-dateutil`)
8
+ - **Markdown 工具** — Markdown 转 HTML、去除 Markdown 标记(依赖 `Markdown`)
9
+ - **问候工具** — 简单的个性化问候
10
+
11
+ ## 安装
12
+
13
+ ```bash
14
+ pip install duyi-utils
15
+ ```
16
+
17
+ ## 使用
18
+
19
+ ```python
20
+ from duyi_utils import format_date, days_until, to_html, strip_markdown
21
+ from datetime import date
22
+
23
+ # 日期工具
24
+ format_date(date(2026, 6, 1)) # => "2026年06月01日"
25
+ days_until("2026-10-01") # => 剩余天数
26
+
27
+ # Markdown 工具
28
+ to_html("**Hello**") # => "<p><strong>Hello</strong></p>"
29
+ strip_markdown("**Hello**") # => "<strong>Hello</strong>"
30
+
31
+ # 问候工具
32
+ from duyi_utils.greeter import greet
33
+ greet("小明") # => "你好, 小明!"
34
+ ```
@@ -0,0 +1,22 @@
1
+ # pyproject.toml
2
+
3
+ [build-system] # 构建系统配置,指定构建工具和依赖
4
+ requires = ["hatchling"] # 依赖的后端构建工具
5
+ build-backend = "hatchling.build" # 使用哪个工具的哪个模块来构建项目
6
+
7
+ [project] # 工程描述
8
+ name = "xubin_duyi-utils" # 发行版名称
9
+ version = "0.1.3" # 版本号,遵循语义化版本控制
10
+ description = "一个用于学习 Python 公共库构建和发布的示例工程" # 项目简介
11
+ requires-python = ">=3.14" # 指定支持的 Python 版本
12
+ readme = "README.md" # 指定项目的 README 文件路径
13
+ dependencies = [
14
+ "python-dateutil>=2.8", # 依赖的第三方库,指定版本要求
15
+ "Markdown>=3.5", # 另一个依赖
16
+ ]
17
+
18
+ [tool.hatch.build.targets.sdist] # 配置源代码分发包
19
+ # 配置留空,表示它会默认包含所有项目文件
20
+
21
+ [tool.hatch.build.targets.wheel] # 配置 wheel 包
22
+ packages = ["src/duyi_utils"] # 配置 wheel 包的构建目标,指定包含的包路径
@@ -0,0 +1,4 @@
1
+ from duyi_utils.markdown import to_html, strip_markdown
2
+ from duyi_utils.date import format_date, days_until
3
+
4
+ __all__ = ["to_html", "strip_markdown", "format_date", "days_until"]
@@ -0,0 +1,13 @@
1
+ from datetime import date
2
+
3
+ from dateutil.parser import parse
4
+
5
+
6
+ def format_date(d: date) -> str:
7
+ return d.strftime("%Y年%m月%d日")
8
+
9
+
10
+ def days_until(target: str) -> int:
11
+ target_date = parse(target).date()
12
+ delta = target_date - date.today()
13
+ return delta.days
@@ -0,0 +1,3 @@
1
+ def greet(name: str) -> str:
2
+ print("greet function called")
3
+ return f"你好, {name}!"
@@ -0,0 +1,10 @@
1
+ import markdown
2
+
3
+
4
+ def to_html(text: str) -> str:
5
+ return markdown.markdown(text)
6
+
7
+
8
+ def strip_markdown(text: str) -> str:
9
+ html = markdown.markdown(text)
10
+ return html.removeprefix("<p>").removesuffix("</p>\n")
@@ -0,0 +1,218 @@
1
+
2
+ # 构建发布
3
+
4
+ ## 库的开发流程
5
+
6
+ ![image-20260601134020260](https://resource.duyiedu.com/yuanjin/202606011340298.png)
7
+
8
+ ## 构建
9
+ 啥时候用到构建和发布?
10
+ - 公司开发公共库的时候, 开源库也是一样的
11
+
12
+ 公司里面的公共库开发完如何进行交付?
13
+ 构建的目的: 拿到wheel包(就是分发的包)
14
+
15
+ 如果把源代码构建好
16
+
17
+ > 构建协议:https://peps.python.org/pep-0517/
18
+ >
19
+ > 构建产物标准:https://peps.python.org/pep-0427/
20
+
21
+ 第一步, 准备好pyproject.toml文件
22
+ 下面的格式相当于json格式这样:
23
+ ```json
24
+ {
25
+ "build-system": {
26
+ "requires":["hatchling"]
27
+ }
28
+ }
29
+ ```
30
+
31
+ ```toml
32
+ # pyproject.toml
33
+
34
+ [build-system] # 构建系统配置,指定构建工具和依赖(本身并不具备构建功能, 需要依赖后端去构建, 读取requires)
35
+ requires = ["hatchling"] # 依赖的后端构建工具
36
+ build-backend = "hatchling.build" # 使用哪个工具的哪个模块来构建项目(这里怎么写 插对应工具文档)
37
+
38
+ [project] # 工程描述
39
+ name = "duyi-utils" # 发行版名称
40
+ version = "0.1.3" # 版本号,遵循语义化版本控制
41
+ description = "一个用于学习 Python 公共库构建和发布的示例工程" # 项目简介
42
+ requires-python = ">=3.14" # 指定支持的 Python 版本
43
+ readme = "README.md" # 指定项目的 README 文件路径
44
+ dependencies = [
45
+ "python-dateutil>=2.8", # 依赖的第三方库,指定版本要求
46
+ "Markdown>=3.5", # 另一个依赖
47
+ ]
48
+
49
+ [tool.hatch.build.targets.sdist] # 配置源代码分发包
50
+ # 配置留空,表示它会默认包含所有项目文件
51
+
52
+ [tool.hatch.build.targets.wheel] # 配置 wheel 包(哪些包要形成好最终产物)
53
+ packages = ["src/duyi_utils"] # 配置 wheel 包的构建目标,指定包含的包路径
54
+ ```
55
+
56
+ > 需要安装VSCode插件:even better toml
57
+
58
+ > 常用构建前端:`uv`、`Poetry`、`PDM`、...
59
+ >
60
+ > 常用构建后端:`hatchling`、`setuptools`、`PDM-backend`、`uv-build` `poetry-core`...
61
+ build在干嘛?
62
+ + 构建前端(更靠近用户 --> 程序员) 相当于提供一个命令行交互应用, 交互程序
63
+
64
+ + 构建后端(更靠近功能) --> 就是一个包 (早期的构建只能是后端构建)
65
+ ```python
66
+ # setup.py
67
+ from setuptools import xxx
68
+ ```
69
+
70
+ 构建的整个流程:
71
+ 运行build的时候:
72
+ a. python -m build -> b. 读pyproject.toml(工程的描述文件, 类似于前端的package.json) -> c. 安装build-system/requires -> d. 安装到隔离环境(操作系统/tmp/[临时目录, 使用完过后就删]) -> e. 按照配置启动相应模块进行构建 -> f. 读取额外配置[tool.hatch.build.targets.xxx] -> g. 完成构建 -> h. 构建产物(sdist + wheel) 标准产物 [所有的前后端工具都是这两个产物]
73
+
74
+ + sdist: 源码(压缩包 -> duyi_utils-0.1.3.tar.gz)
75
+ + wheel: 也是压缩包, 跟源码有啥区别? -> 可能有些工程有些c++的代码, 是要编译的, 这里会包含编译的结果 -> 将来安装这个包格式就是wheel包解压的结果, wheel包安装的格式就是将来其他工程安装这个包的格式(这就是最核心的作用)
76
+ 1. wheel包里(包):
77
+ 2. wheel包里(包-version.dist-info): 包的一些信息
78
+
79
+
80
+ e步骤原理代码:
81
+ ```python
82
+ from hatchling.build
83
+ hatchling.build.build_wheel()
84
+ hatchling.build.build_sdist()
85
+ ```
86
+
87
+ 重点: wheel配置
88
+ [tool.hatch.build.targets.wheel] # 配置 wheel 包(哪些包要形成好最终产物)
89
+ packages = ["src/duyi_utils"] # 配置 wheel 包的构建目标,指定包含的包路径
90
+ + 发行版名称: 工程名称 / 安装名称 / 库名 (pip install xxx) 指的就是这里的xxx --> [project] name 字段(规范来说, 这里的名字跟文件夹的名字是对应的[顶级文件夹] 不对应也没事, 一般是对应的)
91
+
92
+ + (顶层)包名(duyi_utils):
93
+ ```python
94
+ duyi-utils
95
+ src
96
+ a
97
+ b
98
+ c
99
+ (顶层)包名就是 a b c
100
+
101
+ wheel里面变成:
102
+ 发行版名称
103
+ a
104
+ b
105
+ c
106
+
107
+ 别人安装wheel(已经没有发行版名称)
108
+ site-packages
109
+ a
110
+ b
111
+ c
112
+
113
+ 别人使用: (用的时候用的是包的名字, 而不是发行版的名字) -> 给别人造成非常奇怪的感觉, 且当你安装包过多你会找不到
114
+ pip install duyi_utils
115
+
116
+ import duyi_utils(报错)
117
+ 你只能导入a b c
118
+
119
+
120
+ 所以说一般怎么做: 规范是发行版名称 和 包名是一样的, 只不过把-改为_
121
+ ```
122
+
123
+ 构建方式
124
+
125
+ ```shell
126
+ # 1. 创建虚拟环境
127
+ python -m venv .venv
128
+
129
+ # 2. 激活虚拟环境
130
+ source .venv/bin/activate
131
+
132
+ # 3. 安装构建工具
133
+ pip install build
134
+
135
+ # 4. 运行构建
136
+ python -m build
137
+ ```
138
+
139
+
140
+ 构建产物放到了`./dist`中
141
+
142
+ ## 发布和安装
143
+
144
+ ### 发布前的准备
145
+
146
+ ```shell
147
+ # 先构建(略)
148
+
149
+ # 安装官方的发布工具
150
+ pip install twine
151
+
152
+ # 【可选】验证构建产物的完整性
153
+ twine check dist/*
154
+ ```
155
+
156
+ ### 发布到官方仓库
157
+
158
+ 1. 注册账号:https://pypi.org/account/register/
159
+ 2. 创建一个token:https://pypi.org/manage/account/token/
160
+ 3. 复制token
161
+ 4. 运行`twine upload dist/*`命令上传
162
+
163
+ ---
164
+
165
+ 安装:使用`pip install xxx`安装即可
166
+
167
+ ### 发布到私有仓库
168
+
169
+ 通常使用云服务完成私有仓库的搭建
170
+
171
+ 1. 创建阿里云效制品仓库
172
+ https://packages.aliyun.com/
173
+ 2. 按照仓库指南配置并上传包
174
+
175
+ ---
176
+
177
+ 安装:
178
+
179
+ 1. 将制品仓库作为镜像源
180
+ ```ini
181
+ [global]
182
+ index-url = 制品仓库地址
183
+ extra-index-url = 镜像源地址
184
+ trusted-host = packages.aliyun.com
185
+ ```
186
+
187
+ 2. 直接安装`pip install xxx`即可
188
+
189
+ ## 可编辑依赖
190
+
191
+ > 可编辑安装(editable install)又称开发模式安装,通过 `pip install -e .` 将项目以链接形式安装到当前环境。
192
+
193
+ 当项目被可编辑安装后,你对源码的任何修改都会**即时生效**,无需重新构建和重新安装。
194
+
195
+ 这在日常开发中非常有用:
196
+
197
+ - 你可以在一个项目里开发公共库,同时在另一个项目里导入并实时测试
198
+ - 代码改动后不用反复执行 `pip install`,节约大量时间
199
+
200
+ ```shell
201
+ # 在项目根目录执行
202
+ pip install -e 目标工程路径
203
+ ```
204
+
205
+ 其原理是在 `site-packages` 中创建一个指向项目源码的链接( `.pth` 文件),而不是复制一份代码过去。
206
+
207
+ ```python
208
+ # 执行后,可以在任意位置导入
209
+ from duyi_utils import some_func
210
+ # 修改源码后,下次调用自动生效
211
+ ```
212
+
213
+
214
+
215
+ ## 作业
216
+
217
+ 1. 完整一个库的构建、私有发布、安装
218
+ 2. 使用费曼学习法复述一个库整个的构建流程。