hutool-python 1.0.0__tar.gz → 1.0.1__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.
- {hutool_python-1.0.0/hutool_python.egg-info → hutool_python-1.0.1}/PKG-INFO +439 -438
- {hutool_python-1.0.0 → hutool_python-1.0.1}/README-EN.md +2 -1
- {hutool_python-1.0.0 → hutool_python-1.0.1}/README.md +2 -1
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/fifo_cache.py +11 -2
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/lru_cache.py +11 -2
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/coll.py +65 -10
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/date.py +1108 -1024
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/exceptions.py +50 -1
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/data_size_util.py +79 -79
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/file_name_util.py +111 -111
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/file_util.py +775 -650
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/io_util.py +133 -133
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/resource_util.py +137 -137
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/map.py +971 -933
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/text/str_builder.py +224 -224
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/charset_util.py +42 -6
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/id_util.py +40 -6
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/number_util.py +720 -720
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/page_util.py +96 -61
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/random_util.py +97 -14
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/runtime_util.py +113 -89
- hutool_python-1.0.1/hutool/core/util/system_util.py +104 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/version_util.py +29 -6
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cron/cron_util.py +8 -5
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/crypto/digest_util.py +144 -22
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/crypto/secure_util.py +11 -2
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/json_util.py +124 -25
- {hutool_python-1.0.0 → hutool_python-1.0.1/hutool_python.egg-info}/PKG-INFO +439 -438
- {hutool_python-1.0.0 → hutool_python-1.0.1}/pyproject.toml +2 -2
- {hutool_python-1.0.0 → hutool_python-1.0.1}/setup.cfg +4 -4
- {hutool_python-1.0.0 → hutool_python-1.0.1}/setup.py +2 -2
- hutool_python-1.0.0/hutool/core/util/system_util.py +0 -62
- {hutool_python-1.0.0 → hutool_python-1.0.1}/LICENSE +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/MANIFEST.in +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/cache_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/lfu_cache.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cache/timed_cache.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/captcha/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/captcha/captcha_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/_base.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/bean.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/codec.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/io/path_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/math_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/net.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/text/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/text/csv_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/text/unicode_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/tree.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/array_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/boolean_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/class_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/coordinate_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/credit_code_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/desensitized_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/enum_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/escape_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/hash_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/hex_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/idcard_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/object_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/phone_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/re_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/reflect_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/str_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/url_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/xml_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/core/util/zip_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cron/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/cron/cron_pattern.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/crypto/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/crypto/sign_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/dfa/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/dfa/sensitive_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/extra/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/extra/emoji_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/extra/pinyin_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/extra/qr_code_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/extra/template_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/http/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/http/html_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/http/http_request.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/http/http_response.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/http/http_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/jwt_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/setting/__init__.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/setting/props_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/setting/setting_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool/setting/yaml_util.py +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool_python.egg-info/SOURCES.txt +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool_python.egg-info/dependency_links.txt +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool_python.egg-info/requires.txt +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/hutool_python.egg-info/top_level.txt +0 -0
- {hutool_python-1.0.0 → hutool_python-1.0.1}/requirements.txt +0 -0
|
@@ -1,438 +1,439 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: hutool-python
|
|
3
|
-
Version: 1.0.
|
|
4
|
-
Summary: Python port of Java Hutool utility library
|
|
5
|
-
Home-page: https://github.com/wgp520/hutool-python
|
|
6
|
-
Author: Hutool-Python
|
|
7
|
-
License: MulanPSL-2.0
|
|
8
|
-
Project-URL: Homepage, https://github.com/wgp520/hutool-python
|
|
9
|
-
Project-URL: Documentation, https://wgp520.github.io/hutool-python
|
|
10
|
-
Project-URL: Repository, https://github.com/wgp520/hutool-python
|
|
11
|
-
Project-URL: Issues, https://github.com/wgp520/hutool-python/issues
|
|
12
|
-
Project-URL: Changelog, https://github.com/wgp520/hutool-python/blob/master/docs/changelog.md
|
|
13
|
-
Keywords: hutool,util,utility,toolkit,java-hutool
|
|
14
|
-
Classifier: Development Status :: 4 - Beta
|
|
15
|
-
Classifier: Intended Audience :: Developers
|
|
16
|
-
Classifier: Operating System :: OS Independent
|
|
17
|
-
Classifier: Programming Language :: Python :: 3
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
-
Requires-Python: >=3.8
|
|
26
|
-
Description-Content-Type: text/markdown
|
|
27
|
-
License-File: LICENSE
|
|
28
|
-
Requires-Dist: pendulum>=3.0
|
|
29
|
-
Requires-Dist: httpx>=0.27
|
|
30
|
-
Requires-Dist: cryptography>=42.0
|
|
31
|
-
Requires-Dist: qrcode[pil]>=7.0
|
|
32
|
-
Requires-Dist: Pillow>=10.0
|
|
33
|
-
Requires-Dist: pypinyin>=0.50
|
|
34
|
-
Requires-Dist: emoji>=2.0
|
|
35
|
-
Requires-Dist: jinja2>=3.0
|
|
36
|
-
Requires-Dist: pyjwt>=2.8
|
|
37
|
-
Requires-Dist: pyyaml>=6.0
|
|
38
|
-
Requires-Dist: watchdog>=4.0
|
|
39
|
-
Requires-Dist: sortedcontainers>=2.0
|
|
40
|
-
Provides-Extra: dev
|
|
41
|
-
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
42
|
-
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
43
|
-
|
|
44
|
-
<p align="center">
|
|
45
|
-
<h1 align="center">Hutool-Python</h1>
|
|
46
|
-
</p>
|
|
47
|
-
<p align="center">
|
|
48
|
-
<strong>🐍 让 Python 和 Java 一样"甜蜜"的工具库</strong>
|
|
49
|
-
</p>
|
|
50
|
-
|
|
51
|
-
<p align="center">
|
|
52
|
-
<img src="https://img.shields.io/badge/Python-3.8+-blue.svg" />
|
|
53
|
-
<img src="https://img.shields.io/
|
|
54
|
-
<img src="https://img.shields.io/badge/license-MulanPSL2-blue.svg" />
|
|
55
|
-
<img src="https://img.shields.io/badge/tests-613 passed-brightgreen.svg" />
|
|
56
|
-
<a href="https://wgp520.github.io/hutool-python/"><img src="https://img.shields.io/badge/docs-online-blueviolet.svg" /></a>
|
|
57
|
-
</p>
|
|
58
|
-
|
|
59
|
-
-------------------------------------------------------------------------------
|
|
60
|
-
|
|
61
|
-
[**🌎English Version**](README-EN.md)
|
|
62
|
-
|
|
63
|
-
-------------------------------------------------------------------------------
|
|
64
|
-
|
|
65
|
-
## 📚 简介
|
|
66
|
-
|
|
67
|
-
`Hutool-Python` 是 [Java Hutool](https://github.com/dromara/hutool) 的 Python 移植版本,旨在将 Hutool 丰富且易用的工具类生态带到 Python 世界。
|
|
68
|
-
|
|
69
|
-
**Hutool** 是一个功能丰富且易用的 Java 工具库,涵盖了字符串、数字、集合、编码、日期、文件、IO、加密、JSON、HTTP 客户端等一系列操作。`Hutool-Python` 将这些能力忠实移植到 Python,保留 Hutool 的命名风格和设计理念,让 Java 开发者无缝切换,也让 Python 开发者享受 Hutool 式的便捷体验。
|
|
70
|
-
|
|
71
|
-
### 🍺 设计理念
|
|
72
|
-
|
|
73
|
-
- **忠实移植**:类名保持 Hutool 的 PascalCase 风格(如 `StrUtil`、`DateUtil`),方法名转为 Python 的 snake_case(如 `is_blank`、`offset_day`)
|
|
74
|
-
- **Python 化实现**:底层实现使用 Python 生态最佳实践(`pendulum`、`httpx`、`cryptography` 等),而非机械翻译 Java 代码
|
|
75
|
-
- **中文注释**:所有文档注释均使用中文,采用 Sphinx 风格(`:param:` / `:return:` / `:raises:`),便于源码阅读
|
|
76
|
-
- **零配置开箱即用**:无需额外配置,`pip install` 后即可使用
|
|
77
|
-
|
|
78
|
-
---
|
|
79
|
-
|
|
80
|
-
## 🛠️ 包含模块
|
|
81
|
-
|
|
82
|
-
| 模块 | 介绍 | 对应 Java Hutool |
|
|
83
|
-
|------|------|-----------------|
|
|
84
|
-
| `core/util/` | 核心工具类:字符串、数字、数组、对象、布尔、随机、ID、正则、哈希、转义、URL、版本、分页等 25 个工具类 | `cn.hutool.core.util` |
|
|
85
|
-
| `core/coll.py` | 集合工具:`CollUtil`、`ListUtil` | `cn.hutool.core.collection` |
|
|
86
|
-
| `core/map.py` | Map 工具:`MapUtil`、`BiMap`、`DictUtil` | `cn.hutool.core.map` |
|
|
87
|
-
| `core/io/` | IO 工具:文件、IO 流、路径、文件名、数据大小、资源 | `cn.hutool.core.io` |
|
|
88
|
-
| `core/date.py` | 日期工具:`DateUtil`、`DateTime`(基于 pendulum) | `cn.hutool.core.date` |
|
|
89
|
-
| `core/bean.py` | Bean 工具:属性拷贝、Map 互转 | `cn.hutool.core.bean` |
|
|
90
|
-
| `core/codec.py` | 编解码:`Base64`、`Base32` | `cn.hutool.core.codec` |
|
|
91
|
-
| `core/text/` | 文本工具:`UnicodeUtil`、`CsvUtil`、`StrBuilder` | `cn.hutool.core.text` |
|
|
92
|
-
| `core/net.py` | 网络工具:`NetUtil`、`Ipv4Util` | `cn.hutool.core.net` |
|
|
93
|
-
| `core/math_util.py` | 数学工具:`MathUtil`、`BitStatusUtil` | `cn.hutool.core.math` |
|
|
94
|
-
| `core/tree.py` | 树工具:`TreeUtil` | `cn.hutool.core.lang.tree` |
|
|
95
|
-
| `http/` | HTTP 客户端:`HttpUtil`、`HttpRequest`、`HttpResponse`、`HtmlUtil`(基于 httpx) | `cn.hutool.http` |
|
|
96
|
-
| `json_util.py` | JSON 工具:`JSONUtil`(基于内置 json 扩展) | `cn.hutool.json` |
|
|
97
|
-
| `crypto/` | 加密工具:`DigestUtil`、`SecureUtil`、`SignUtil`(基于 cryptography) | `cn.hutool.crypto` |
|
|
98
|
-
| `cache/` | 缓存工具:`FIFOCache`、`LFUCache`、`LRUCache`、`TimedCache` | `cn.hutool.cache` |
|
|
99
|
-
| `captcha/` | 验证码:`CaptchaUtil`(基于 Pillow) | `cn.hutool.captcha` |
|
|
100
|
-
| `dfa/` | 敏感词过滤:`SensitiveUtil`(基于 DFA 算法) | `cn.hutool.dfa` |
|
|
101
|
-
| `extra/` | 扩展工具:`EmojiUtil`、`PinyinUtil`、`TemplateUtil`、`QrCodeUtil` | `cn.hutool.extra` |
|
|
102
|
-
| `cron/` | 定时任务:`CronUtil`、`CronPattern` | `cn.hutool.cron` |
|
|
103
|
-
| `jwt_util.py` | JWT 工具:`JWTUtil`(基于 PyJWT) | `cn.hutool.jwt` |
|
|
104
|
-
| `setting/` | 配置工具:`SettingUtil`、`YamlUtil`、`PropsUtil` | `cn.hutool.setting` |
|
|
105
|
-
|
|
106
|
-
> 注:Java Hutool 中的 AOP、JDBC/Swing、POI、JNDI、ClassLoader、BloomFilter、Log、Script、Socket 等 Java 专属模块不包含在 Hutool-Python 中。
|
|
107
|
-
|
|
108
|
-
---
|
|
109
|
-
|
|
110
|
-
## 📦 安装
|
|
111
|
-
|
|
112
|
-
### 🍊 pip 安装(推荐)
|
|
113
|
-
|
|
114
|
-
```bash
|
|
115
|
-
pip install hutool-python
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### 🍐 从源码安装
|
|
119
|
-
|
|
120
|
-
```bash
|
|
121
|
-
git clone https://github.com/wgp520/hutool-python.git
|
|
122
|
-
cd hutool-python
|
|
123
|
-
pip install -e .
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### 🧪 安装开发依赖
|
|
127
|
-
|
|
128
|
-
```bash
|
|
129
|
-
pip install -e ".[dev]"
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
> **要求**:Python 3.8+
|
|
133
|
-
|
|
134
|
-
---
|
|
135
|
-
|
|
136
|
-
## 🚀 快速开始
|
|
137
|
-
|
|
138
|
-
### 字符串工具
|
|
139
|
-
|
|
140
|
-
```python
|
|
141
|
-
from hutool import StrUtil
|
|
142
|
-
|
|
143
|
-
# 判断是否为空白
|
|
144
|
-
StrUtil.is_blank("") # True
|
|
145
|
-
StrUtil.is_blank(" ") # True
|
|
146
|
-
StrUtil.is_blank("hello") # False
|
|
147
|
-
|
|
148
|
-
# 子串操作
|
|
149
|
-
StrUtil.sub_before("abc.jpg", ".") # "abc"
|
|
150
|
-
StrUtil.sub_after("abc.jpg", ".") # "jpg"
|
|
151
|
-
StrUtil.sub_between("a(b)c", "(", ")") # "b"
|
|
152
|
-
|
|
153
|
-
# 命名转换
|
|
154
|
-
StrUtil.to_camel_case("hello_world") # "helloWorld"
|
|
155
|
-
StrUtil.to_snake_case("helloWorld") # "hello_world"
|
|
156
|
-
|
|
157
|
-
# 字符串相似度
|
|
158
|
-
StrUtil.similar("我爱学习", "我爱学") # 0.75
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
### 日期工具
|
|
162
|
-
|
|
163
|
-
```python
|
|
164
|
-
from hutool import DateUtil
|
|
165
|
-
|
|
166
|
-
# 获取当前时间
|
|
167
|
-
DateUtil.now() # "2024-01-15 14:30:00"
|
|
168
|
-
DateUtil.today() # "2024-01-15"
|
|
169
|
-
|
|
170
|
-
# 解析日期
|
|
171
|
-
dt = DateUtil.parse("2024-01-15")
|
|
172
|
-
|
|
173
|
-
# 日期偏移
|
|
174
|
-
DateUtil.offset_day(dt, 7) # 7天后
|
|
175
|
-
DateUtil.offset_month(dt, -1) # 1个月前
|
|
176
|
-
|
|
177
|
-
# 日期差
|
|
178
|
-
start = DateUtil.parse("2024-01-01")
|
|
179
|
-
end = DateUtil.parse("2024-12-31")
|
|
180
|
-
DateUtil.between_day(start, end) # 365
|
|
181
|
-
|
|
182
|
-
# 判断闰年
|
|
183
|
-
DateUtil.is_leap_year(2024) # True
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### 集合工具
|
|
187
|
-
|
|
188
|
-
```python
|
|
189
|
-
from hutool import CollUtil, MapUtil
|
|
190
|
-
|
|
191
|
-
# 分组
|
|
192
|
-
data = [{"type": "a", "val": 1}, {"type": "b", "val": 2}, {"type": "a", "val": 3}]
|
|
193
|
-
CollUtil.group_by(data, key_func=lambda x: x["type"])
|
|
194
|
-
# {"a": [{"type": "a", "val": 1}, {"type": "a", "val": 3}], "b": [...]}
|
|
195
|
-
|
|
196
|
-
# 分页
|
|
197
|
-
CollUtil.page([1, 2, 3, 4, 5], page_num=1, page_size=2) # [1, 2]
|
|
198
|
-
|
|
199
|
-
# Map 操作
|
|
200
|
-
MapUtil.sort({"c": 3, "a": 1, "b": 2}) # {"a": 1, "b": 2, "c": 3}
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
### ID 生成
|
|
204
|
-
|
|
205
|
-
```python
|
|
206
|
-
from hutool import IdUtil
|
|
207
|
-
|
|
208
|
-
IdUtil.random_uuid() # "550e8400-e29b-41d4-a716-446655440000"
|
|
209
|
-
IdUtil.simple_uuid() # "550e8400e29b41d4a716446655440000"
|
|
210
|
-
IdUtil.nano_id() # "V1StGXR8_Z5jdHi6B-myT"
|
|
211
|
-
IdUtil.snowflake_id() # 1480946864314982400
|
|
212
|
-
IdUtil.object_id() # "5f8b2c3d4e5f6a7b8c9d0e1f"
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### HTTP 客户端
|
|
216
|
-
|
|
217
|
-
```python
|
|
218
|
-
from hutool.http import HttpUtil
|
|
219
|
-
|
|
220
|
-
# GET 请求
|
|
221
|
-
html = HttpUtil.get("https://example.com")
|
|
222
|
-
|
|
223
|
-
# POST 请求
|
|
224
|
-
result = HttpUtil.post("https://api.example.com/data", json_data={"key": "value"})
|
|
225
|
-
|
|
226
|
-
# 链式调用
|
|
227
|
-
from hutool.http import HttpRequest, HttpResponse
|
|
228
|
-
|
|
229
|
-
resp = (HttpRequest.post("https://api.example.com/data")
|
|
230
|
-
.header("Authorization", "Bearer token")
|
|
231
|
-
.json({"key": "value"})
|
|
232
|
-
.timeout(30)
|
|
233
|
-
.execute())
|
|
234
|
-
|
|
235
|
-
print(resp.is_ok()) # True
|
|
236
|
-
print(resp.to_json()) # {...}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### 加密工具
|
|
240
|
-
|
|
241
|
-
```python
|
|
242
|
-
from hutool.crypto import DigestUtil, SecureUtil
|
|
243
|
-
|
|
244
|
-
# 摘要算法
|
|
245
|
-
DigestUtil.md5_hex("hello") # "5d41402abc4b2a76b9719d911017c592"
|
|
246
|
-
DigestUtil.sha256_hex("hello") # "2cf24dba5fb0a30e..."
|
|
247
|
-
|
|
248
|
-
# AES 对称加密
|
|
249
|
-
key = SecureUtil.generate_aes_key()
|
|
250
|
-
encrypted = SecureUtil.encrypt_aes(b"secret data", key, "ECB")
|
|
251
|
-
decrypted = SecureUtil.decrypt_aes(encrypted, key, "ECB")
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
### 敏感词过滤
|
|
255
|
-
|
|
256
|
-
```python
|
|
257
|
-
from hutool.dfa import SensitiveUtil
|
|
258
|
-
|
|
259
|
-
# 初始化敏感词库
|
|
260
|
-
SensitiveUtil.init(["敏感词", "违禁词", "垃圾信息"])
|
|
261
|
-
|
|
262
|
-
# 检测
|
|
263
|
-
SensitiveUtil.contains("这是一段包含敏感词的文本") # True
|
|
264
|
-
|
|
265
|
-
# 查找
|
|
266
|
-
SensitiveUtil.find_all("这是一段包含敏感词和违禁词的文本") # ["敏感词", "违禁词"]
|
|
267
|
-
|
|
268
|
-
# 替换
|
|
269
|
-
SensitiveUtil.replace("这是一段包含敏感词的文本") # "这是一段包含**的文本"
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### 验证码
|
|
273
|
-
|
|
274
|
-
```python
|
|
275
|
-
from hutool.captcha import CaptchaUtil
|
|
276
|
-
|
|
277
|
-
# 线干扰验证码
|
|
278
|
-
captcha = CaptchaUtil.create_line_captcha(width=200, height=80, code_count=5)
|
|
279
|
-
code = captcha.create_code()
|
|
280
|
-
img_bytes = captcha.get_image_bytes()
|
|
281
|
-
|
|
282
|
-
# 算术验证码
|
|
283
|
-
arith = CaptchaUtil.create_arithmetic_captcha(width=200, height=80)
|
|
284
|
-
arith.create_code() # "3+5=?"
|
|
285
|
-
arith.get_result() # "8"
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
### 更多工具
|
|
289
|
-
|
|
290
|
-
```python
|
|
291
|
-
from hutool.core.util import (
|
|
292
|
-
NumberUtil, ArrayUtil, RandomUtil, HexUtil, HashUtil,
|
|
293
|
-
ReUtil, EscapeUtil, PhoneUtil, IdcardUtil, DesensitizedUtil,
|
|
294
|
-
CoordinateUtil, ZipUtil, XmlUtil, UrlUtil, VersionUtil,
|
|
295
|
-
)
|
|
296
|
-
from hutool.core.codec import Base64
|
|
297
|
-
from hutool.core.io import FileUtil, IoUtil, PathUtil
|
|
298
|
-
from hutool.json_util import JSONUtil
|
|
299
|
-
from hutool.cache import CacheUtil
|
|
300
|
-
from hutool.extra import PinyinUtil, EmojiUtil, TemplateUtil
|
|
301
|
-
from hutool.jwt_util import JWTUtil
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
---
|
|
305
|
-
|
|
306
|
-
## 📁 项目结构
|
|
307
|
-
|
|
308
|
-
```
|
|
309
|
-
hutool-python/
|
|
310
|
-
├── hutool/ # Python 包
|
|
311
|
-
│ ├── __init__.py
|
|
312
|
-
│ ├── core/ # 核心模块(对应 hutool-core)
|
|
313
|
-
│ │ ├── util/ # 核心工具类(25 个)
|
|
314
|
-
│ │ ├── io/ # IO 工具类(6 个)
|
|
315
|
-
│ │ ├── text/ # 文本工具类(3 个)
|
|
316
|
-
│ │ ├── date.py # 日期工具
|
|
317
|
-
│ │ ├── coll.py # 集合工具
|
|
318
|
-
│ │ ├── map.py # Map 工具
|
|
319
|
-
│ │ ├── bean.py # Bean 工具
|
|
320
|
-
│ │ ├── codec.py # 编解码工具
|
|
321
|
-
│ │ ├── net.py # 网络工具
|
|
322
|
-
│ │ ├── math_util.py # 数学工具
|
|
323
|
-
│ │ └── tree.py # 树工具
|
|
324
|
-
│ ├── http/ # HTTP 客户端(基于 httpx)
|
|
325
|
-
│ ├── json_util.py # JSON 工具
|
|
326
|
-
│ ├── crypto/ # 加密工具(基于 cryptography)
|
|
327
|
-
│ ├── cache/ # 缓存工具(FIFO/LFU/LRU/Timed)
|
|
328
|
-
│ ├── captcha/ # 验证码(基于 Pillow)
|
|
329
|
-
│ ├── dfa/ # 敏感词过滤(DFA 算法)
|
|
330
|
-
│ ├── extra/ # 扩展工具(拼音/Emoji/模板/二维码)
|
|
331
|
-
│ ├── cron/ # 定时任务
|
|
332
|
-
│ ├── jwt_util.py # JWT 工具
|
|
333
|
-
│ └── setting/ # 配置工具(YAML/Properties)
|
|
334
|
-
├── tests/ # 测试(47 个测试文件,613 个测试用例)
|
|
335
|
-
├── requirements.txt
|
|
336
|
-
└── setup.py
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
---
|
|
340
|
-
|
|
341
|
-
## 🧪 运行测试
|
|
342
|
-
|
|
343
|
-
```bash
|
|
344
|
-
# 安装开发依赖
|
|
345
|
-
pip install pytest
|
|
346
|
-
|
|
347
|
-
# 运行全部测试
|
|
348
|
-
pytest tests/ -v
|
|
349
|
-
|
|
350
|
-
# 运行核心模块测试
|
|
351
|
-
pytest tests/test_core/ -v
|
|
352
|
-
|
|
353
|
-
# 运行单个模块测试
|
|
354
|
-
pytest tests/test_core/test_str_util.py -v
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
---
|
|
358
|
-
|
|
359
|
-
## 🔧 依赖清单
|
|
360
|
-
|
|
361
|
-
| 依赖库 | 用途 | 对应模块 |
|
|
362
|
-
|--------|------|---------|
|
|
363
|
-
| `pendulum>=3.0` | 日期时间处理 | `core/date.py` |
|
|
364
|
-
| `httpx>=0.27` | HTTP 客户端 | `http/` |
|
|
365
|
-
| `cryptography>=42.0` | 加密算法 | `crypto/` |
|
|
366
|
-
| `qrcode[pil]>=7.0` | 二维码生成 | `extra/qr_code_util.py` |
|
|
367
|
-
| `Pillow>=10.0` | 图像处理(验证码) | `captcha/` |
|
|
368
|
-
| `pypinyin>=0.50` | 中文拼音转换 | `extra/pinyin_util.py` |
|
|
369
|
-
| `emoji>=2.0` | Emoji 处理 | `extra/emoji_util.py` |
|
|
370
|
-
| `jinja2>=3.0` | 模板引擎 | `extra/template_util.py` |
|
|
371
|
-
| `pyjwt>=2.8` | JWT 处理 | `jwt_util.py` |
|
|
372
|
-
| `pyyaml>=6.0` | YAML 解析 | `setting/yaml_util.py` |
|
|
373
|
-
| `sortedcontainers>=2.0` | 有序容器 | `cache/` |
|
|
374
|
-
|
|
375
|
-
---
|
|
376
|
-
|
|
377
|
-
## 📝 Java Hutool 与 Hutool-Python 对照
|
|
378
|
-
|
|
379
|
-
### 方法名映射规则
|
|
380
|
-
|
|
381
|
-
| Java Hutool | Hutool-Python | 示例 |
|
|
382
|
-
|------------|---------------|------|
|
|
383
|
-
| `StrUtil.isBlank()` | `StrUtil.is_blank()` | 驼峰 → 蛇形 |
|
|
384
|
-
| `DateUtil.offsetDay()` | `DateUtil.offset_day()` | 驼峰 → 蛇形 |
|
|
385
|
-
| `new DateTime()` | `DateTime()` | Python 无 `new` |
|
|
386
|
-
| `CollUtil.newArrayList()` | `CollUtil.new_array_list()` | 驼峰 → 蛇形 |
|
|
387
|
-
|
|
388
|
-
### 使用对比
|
|
389
|
-
|
|
390
|
-
```java
|
|
391
|
-
// Java Hutool
|
|
392
|
-
String uuid = IdUtil.randomUUID();
|
|
393
|
-
boolean blank = StrUtil.isBlank(" ");
|
|
394
|
-
DateTime dt = DateUtil.parse("2024-01-15");
|
|
395
|
-
String md5 = SecureUtil.md5("hello");
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
```python
|
|
399
|
-
# Hutool-Python
|
|
400
|
-
uuid = IdUtil.random_uuid()
|
|
401
|
-
blank = StrUtil.is_blank(" ")
|
|
402
|
-
dt = DateUtil.parse("2024-01-15")
|
|
403
|
-
md5 = DigestUtil.md5_hex("hello")
|
|
404
|
-
```
|
|
405
|
-
|
|
406
|
-
---
|
|
407
|
-
|
|
408
|
-
## 🏗️ 贡献指南
|
|
409
|
-
|
|
410
|
-
欢迎为 Hutool-Python 贡献代码!请遵循以下原则:
|
|
411
|
-
|
|
412
|
-
1. **保持 Hutool 风格**:类名使用 PascalCase,方法名使用 snake_case
|
|
413
|
-
2. **中文注释**:所有新增方法须包含中文文档注释,格式为 Sphinx 风格(`:param:` / `:return:` / `:raises:`)
|
|
414
|
-
3. **编写测试**:新增方法须附带 pytest 测试用例
|
|
415
|
-
4. **不引入额外依赖**:尽量使用 Python 标准库,仅在必要时引入第三方依赖
|
|
416
|
-
|
|
417
|
-
### 贡献步骤
|
|
418
|
-
|
|
419
|
-
1. Fork 项目
|
|
420
|
-
2. 创建功能分支 (`git checkout -b feature/xxx`)
|
|
421
|
-
3. 提交代码 (`git commit -m 'Add xxx feature'`)
|
|
422
|
-
4. 推送到分支 (`git push origin feature/xxx`)
|
|
423
|
-
5. 创建 Pull Request
|
|
424
|
-
|
|
425
|
-
---
|
|
426
|
-
|
|
427
|
-
## 📖 相关链接
|
|
428
|
-
|
|
429
|
-
- [
|
|
430
|
-
- [
|
|
431
|
-
- [Java Hutool
|
|
432
|
-
- [Hutool
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: hutool-python
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Python port of Java Hutool utility library
|
|
5
|
+
Home-page: https://github.com/wgp520/hutool-python
|
|
6
|
+
Author: Hutool-Python
|
|
7
|
+
License: MulanPSL-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/wgp520/hutool-python
|
|
9
|
+
Project-URL: Documentation, https://wgp520.github.io/hutool-python
|
|
10
|
+
Project-URL: Repository, https://github.com/wgp520/hutool-python
|
|
11
|
+
Project-URL: Issues, https://github.com/wgp520/hutool-python/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/wgp520/hutool-python/blob/master/docs/changelog.md
|
|
13
|
+
Keywords: hutool,util,utility,toolkit,java-hutool
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: pendulum>=3.0
|
|
29
|
+
Requires-Dist: httpx>=0.27
|
|
30
|
+
Requires-Dist: cryptography>=42.0
|
|
31
|
+
Requires-Dist: qrcode[pil]>=7.0
|
|
32
|
+
Requires-Dist: Pillow>=10.0
|
|
33
|
+
Requires-Dist: pypinyin>=0.50
|
|
34
|
+
Requires-Dist: emoji>=2.0
|
|
35
|
+
Requires-Dist: jinja2>=3.0
|
|
36
|
+
Requires-Dist: pyjwt>=2.8
|
|
37
|
+
Requires-Dist: pyyaml>=6.0
|
|
38
|
+
Requires-Dist: watchdog>=4.0
|
|
39
|
+
Requires-Dist: sortedcontainers>=2.0
|
|
40
|
+
Provides-Extra: dev
|
|
41
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
42
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
43
|
+
|
|
44
|
+
<p align="center">
|
|
45
|
+
<h1 align="center">Hutool-Python</h1>
|
|
46
|
+
</p>
|
|
47
|
+
<p align="center">
|
|
48
|
+
<strong>🐍 让 Python 和 Java 一样"甜蜜"的工具库</strong>
|
|
49
|
+
</p>
|
|
50
|
+
|
|
51
|
+
<p align="center">
|
|
52
|
+
<img src="https://img.shields.io/badge/Python-3.8+-blue.svg" />
|
|
53
|
+
<a href="https://pypi.org/project/hutool-python/"><img src="https://img.shields.io/pypi/v/hutool-python.svg" /></a>
|
|
54
|
+
<img src="https://img.shields.io/badge/license-MulanPSL2-blue.svg" />
|
|
55
|
+
<img src="https://img.shields.io/badge/tests-613 passed-brightgreen.svg" />
|
|
56
|
+
<a href="https://wgp520.github.io/hutool-python/"><img src="https://img.shields.io/badge/docs-online-blueviolet.svg" /></a>
|
|
57
|
+
</p>
|
|
58
|
+
|
|
59
|
+
-------------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
[**🌎English Version**](README-EN.md)
|
|
62
|
+
|
|
63
|
+
-------------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
## 📚 简介
|
|
66
|
+
|
|
67
|
+
`Hutool-Python` 是 [Java Hutool](https://github.com/dromara/hutool) 的 Python 移植版本,旨在将 Hutool 丰富且易用的工具类生态带到 Python 世界。
|
|
68
|
+
|
|
69
|
+
**Hutool** 是一个功能丰富且易用的 Java 工具库,涵盖了字符串、数字、集合、编码、日期、文件、IO、加密、JSON、HTTP 客户端等一系列操作。`Hutool-Python` 将这些能力忠实移植到 Python,保留 Hutool 的命名风格和设计理念,让 Java 开发者无缝切换,也让 Python 开发者享受 Hutool 式的便捷体验。
|
|
70
|
+
|
|
71
|
+
### 🍺 设计理念
|
|
72
|
+
|
|
73
|
+
- **忠实移植**:类名保持 Hutool 的 PascalCase 风格(如 `StrUtil`、`DateUtil`),方法名转为 Python 的 snake_case(如 `is_blank`、`offset_day`)
|
|
74
|
+
- **Python 化实现**:底层实现使用 Python 生态最佳实践(`pendulum`、`httpx`、`cryptography` 等),而非机械翻译 Java 代码
|
|
75
|
+
- **中文注释**:所有文档注释均使用中文,采用 Sphinx 风格(`:param:` / `:return:` / `:raises:`),便于源码阅读
|
|
76
|
+
- **零配置开箱即用**:无需额外配置,`pip install` 后即可使用
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 🛠️ 包含模块
|
|
81
|
+
|
|
82
|
+
| 模块 | 介绍 | 对应 Java Hutool |
|
|
83
|
+
|------|------|-----------------|
|
|
84
|
+
| `core/util/` | 核心工具类:字符串、数字、数组、对象、布尔、随机、ID、正则、哈希、转义、URL、版本、分页等 25 个工具类 | `cn.hutool.core.util` |
|
|
85
|
+
| `core/coll.py` | 集合工具:`CollUtil`、`ListUtil` | `cn.hutool.core.collection` |
|
|
86
|
+
| `core/map.py` | Map 工具:`MapUtil`、`BiMap`、`DictUtil` | `cn.hutool.core.map` |
|
|
87
|
+
| `core/io/` | IO 工具:文件、IO 流、路径、文件名、数据大小、资源 | `cn.hutool.core.io` |
|
|
88
|
+
| `core/date.py` | 日期工具:`DateUtil`、`DateTime`(基于 pendulum) | `cn.hutool.core.date` |
|
|
89
|
+
| `core/bean.py` | Bean 工具:属性拷贝、Map 互转 | `cn.hutool.core.bean` |
|
|
90
|
+
| `core/codec.py` | 编解码:`Base64`、`Base32` | `cn.hutool.core.codec` |
|
|
91
|
+
| `core/text/` | 文本工具:`UnicodeUtil`、`CsvUtil`、`StrBuilder` | `cn.hutool.core.text` |
|
|
92
|
+
| `core/net.py` | 网络工具:`NetUtil`、`Ipv4Util` | `cn.hutool.core.net` |
|
|
93
|
+
| `core/math_util.py` | 数学工具:`MathUtil`、`BitStatusUtil` | `cn.hutool.core.math` |
|
|
94
|
+
| `core/tree.py` | 树工具:`TreeUtil` | `cn.hutool.core.lang.tree` |
|
|
95
|
+
| `http/` | HTTP 客户端:`HttpUtil`、`HttpRequest`、`HttpResponse`、`HtmlUtil`(基于 httpx) | `cn.hutool.http` |
|
|
96
|
+
| `json_util.py` | JSON 工具:`JSONUtil`(基于内置 json 扩展) | `cn.hutool.json` |
|
|
97
|
+
| `crypto/` | 加密工具:`DigestUtil`、`SecureUtil`、`SignUtil`(基于 cryptography) | `cn.hutool.crypto` |
|
|
98
|
+
| `cache/` | 缓存工具:`FIFOCache`、`LFUCache`、`LRUCache`、`TimedCache` | `cn.hutool.cache` |
|
|
99
|
+
| `captcha/` | 验证码:`CaptchaUtil`(基于 Pillow) | `cn.hutool.captcha` |
|
|
100
|
+
| `dfa/` | 敏感词过滤:`SensitiveUtil`(基于 DFA 算法) | `cn.hutool.dfa` |
|
|
101
|
+
| `extra/` | 扩展工具:`EmojiUtil`、`PinyinUtil`、`TemplateUtil`、`QrCodeUtil` | `cn.hutool.extra` |
|
|
102
|
+
| `cron/` | 定时任务:`CronUtil`、`CronPattern` | `cn.hutool.cron` |
|
|
103
|
+
| `jwt_util.py` | JWT 工具:`JWTUtil`(基于 PyJWT) | `cn.hutool.jwt` |
|
|
104
|
+
| `setting/` | 配置工具:`SettingUtil`、`YamlUtil`、`PropsUtil` | `cn.hutool.setting` |
|
|
105
|
+
|
|
106
|
+
> 注:Java Hutool 中的 AOP、JDBC/Swing、POI、JNDI、ClassLoader、BloomFilter、Log、Script、Socket 等 Java 专属模块不包含在 Hutool-Python 中。
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 📦 安装
|
|
111
|
+
|
|
112
|
+
### 🍊 pip 安装(推荐)
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
pip install hutool-python
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 🍐 从源码安装
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
git clone https://github.com/wgp520/hutool-python.git
|
|
122
|
+
cd hutool-python
|
|
123
|
+
pip install -e .
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 🧪 安装开发依赖
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pip install -e ".[dev]"
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
> **要求**:Python 3.8+
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## 🚀 快速开始
|
|
137
|
+
|
|
138
|
+
### 字符串工具
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from hutool import StrUtil
|
|
142
|
+
|
|
143
|
+
# 判断是否为空白
|
|
144
|
+
StrUtil.is_blank("") # True
|
|
145
|
+
StrUtil.is_blank(" ") # True
|
|
146
|
+
StrUtil.is_blank("hello") # False
|
|
147
|
+
|
|
148
|
+
# 子串操作
|
|
149
|
+
StrUtil.sub_before("abc.jpg", ".") # "abc"
|
|
150
|
+
StrUtil.sub_after("abc.jpg", ".") # "jpg"
|
|
151
|
+
StrUtil.sub_between("a(b)c", "(", ")") # "b"
|
|
152
|
+
|
|
153
|
+
# 命名转换
|
|
154
|
+
StrUtil.to_camel_case("hello_world") # "helloWorld"
|
|
155
|
+
StrUtil.to_snake_case("helloWorld") # "hello_world"
|
|
156
|
+
|
|
157
|
+
# 字符串相似度
|
|
158
|
+
StrUtil.similar("我爱学习", "我爱学") # 0.75
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 日期工具
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from hutool import DateUtil
|
|
165
|
+
|
|
166
|
+
# 获取当前时间
|
|
167
|
+
DateUtil.now() # "2024-01-15 14:30:00"
|
|
168
|
+
DateUtil.today() # "2024-01-15"
|
|
169
|
+
|
|
170
|
+
# 解析日期
|
|
171
|
+
dt = DateUtil.parse("2024-01-15")
|
|
172
|
+
|
|
173
|
+
# 日期偏移
|
|
174
|
+
DateUtil.offset_day(dt, 7) # 7天后
|
|
175
|
+
DateUtil.offset_month(dt, -1) # 1个月前
|
|
176
|
+
|
|
177
|
+
# 日期差
|
|
178
|
+
start = DateUtil.parse("2024-01-01")
|
|
179
|
+
end = DateUtil.parse("2024-12-31")
|
|
180
|
+
DateUtil.between_day(start, end) # 365
|
|
181
|
+
|
|
182
|
+
# 判断闰年
|
|
183
|
+
DateUtil.is_leap_year(2024) # True
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### 集合工具
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from hutool import CollUtil, MapUtil
|
|
190
|
+
|
|
191
|
+
# 分组
|
|
192
|
+
data = [{"type": "a", "val": 1}, {"type": "b", "val": 2}, {"type": "a", "val": 3}]
|
|
193
|
+
CollUtil.group_by(data, key_func=lambda x: x["type"])
|
|
194
|
+
# {"a": [{"type": "a", "val": 1}, {"type": "a", "val": 3}], "b": [...]}
|
|
195
|
+
|
|
196
|
+
# 分页
|
|
197
|
+
CollUtil.page([1, 2, 3, 4, 5], page_num=1, page_size=2) # [1, 2]
|
|
198
|
+
|
|
199
|
+
# Map 操作
|
|
200
|
+
MapUtil.sort({"c": 3, "a": 1, "b": 2}) # {"a": 1, "b": 2, "c": 3}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### ID 生成
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
from hutool import IdUtil
|
|
207
|
+
|
|
208
|
+
IdUtil.random_uuid() # "550e8400-e29b-41d4-a716-446655440000"
|
|
209
|
+
IdUtil.simple_uuid() # "550e8400e29b41d4a716446655440000"
|
|
210
|
+
IdUtil.nano_id() # "V1StGXR8_Z5jdHi6B-myT"
|
|
211
|
+
IdUtil.snowflake_id() # 1480946864314982400
|
|
212
|
+
IdUtil.object_id() # "5f8b2c3d4e5f6a7b8c9d0e1f"
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### HTTP 客户端
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
from hutool.http import HttpUtil
|
|
219
|
+
|
|
220
|
+
# GET 请求
|
|
221
|
+
html = HttpUtil.get("https://example.com")
|
|
222
|
+
|
|
223
|
+
# POST 请求
|
|
224
|
+
result = HttpUtil.post("https://api.example.com/data", json_data={"key": "value"})
|
|
225
|
+
|
|
226
|
+
# 链式调用
|
|
227
|
+
from hutool.http import HttpRequest, HttpResponse
|
|
228
|
+
|
|
229
|
+
resp = (HttpRequest.post("https://api.example.com/data")
|
|
230
|
+
.header("Authorization", "Bearer token")
|
|
231
|
+
.json({"key": "value"})
|
|
232
|
+
.timeout(30)
|
|
233
|
+
.execute())
|
|
234
|
+
|
|
235
|
+
print(resp.is_ok()) # True
|
|
236
|
+
print(resp.to_json()) # {...}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### 加密工具
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
from hutool.crypto import DigestUtil, SecureUtil
|
|
243
|
+
|
|
244
|
+
# 摘要算法
|
|
245
|
+
DigestUtil.md5_hex("hello") # "5d41402abc4b2a76b9719d911017c592"
|
|
246
|
+
DigestUtil.sha256_hex("hello") # "2cf24dba5fb0a30e..."
|
|
247
|
+
|
|
248
|
+
# AES 对称加密
|
|
249
|
+
key = SecureUtil.generate_aes_key()
|
|
250
|
+
encrypted = SecureUtil.encrypt_aes(b"secret data", key, "ECB")
|
|
251
|
+
decrypted = SecureUtil.decrypt_aes(encrypted, key, "ECB")
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### 敏感词过滤
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
from hutool.dfa import SensitiveUtil
|
|
258
|
+
|
|
259
|
+
# 初始化敏感词库
|
|
260
|
+
SensitiveUtil.init(["敏感词", "违禁词", "垃圾信息"])
|
|
261
|
+
|
|
262
|
+
# 检测
|
|
263
|
+
SensitiveUtil.contains("这是一段包含敏感词的文本") # True
|
|
264
|
+
|
|
265
|
+
# 查找
|
|
266
|
+
SensitiveUtil.find_all("这是一段包含敏感词和违禁词的文本") # ["敏感词", "违禁词"]
|
|
267
|
+
|
|
268
|
+
# 替换
|
|
269
|
+
SensitiveUtil.replace("这是一段包含敏感词的文本") # "这是一段包含**的文本"
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### 验证码
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
from hutool.captcha import CaptchaUtil
|
|
276
|
+
|
|
277
|
+
# 线干扰验证码
|
|
278
|
+
captcha = CaptchaUtil.create_line_captcha(width=200, height=80, code_count=5)
|
|
279
|
+
code = captcha.create_code()
|
|
280
|
+
img_bytes = captcha.get_image_bytes()
|
|
281
|
+
|
|
282
|
+
# 算术验证码
|
|
283
|
+
arith = CaptchaUtil.create_arithmetic_captcha(width=200, height=80)
|
|
284
|
+
arith.create_code() # "3+5=?"
|
|
285
|
+
arith.get_result() # "8"
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### 更多工具
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
from hutool.core.util import (
|
|
292
|
+
NumberUtil, ArrayUtil, RandomUtil, HexUtil, HashUtil,
|
|
293
|
+
ReUtil, EscapeUtil, PhoneUtil, IdcardUtil, DesensitizedUtil,
|
|
294
|
+
CoordinateUtil, ZipUtil, XmlUtil, UrlUtil, VersionUtil,
|
|
295
|
+
)
|
|
296
|
+
from hutool.core.codec import Base64
|
|
297
|
+
from hutool.core.io import FileUtil, IoUtil, PathUtil
|
|
298
|
+
from hutool.json_util import JSONUtil
|
|
299
|
+
from hutool.cache import CacheUtil
|
|
300
|
+
from hutool.extra import PinyinUtil, EmojiUtil, TemplateUtil
|
|
301
|
+
from hutool.jwt_util import JWTUtil
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
---
|
|
305
|
+
|
|
306
|
+
## 📁 项目结构
|
|
307
|
+
|
|
308
|
+
```
|
|
309
|
+
hutool-python/
|
|
310
|
+
├── hutool/ # Python 包
|
|
311
|
+
│ ├── __init__.py
|
|
312
|
+
│ ├── core/ # 核心模块(对应 hutool-core)
|
|
313
|
+
│ │ ├── util/ # 核心工具类(25 个)
|
|
314
|
+
│ │ ├── io/ # IO 工具类(6 个)
|
|
315
|
+
│ │ ├── text/ # 文本工具类(3 个)
|
|
316
|
+
│ │ ├── date.py # 日期工具
|
|
317
|
+
│ │ ├── coll.py # 集合工具
|
|
318
|
+
│ │ ├── map.py # Map 工具
|
|
319
|
+
│ │ ├── bean.py # Bean 工具
|
|
320
|
+
│ │ ├── codec.py # 编解码工具
|
|
321
|
+
│ │ ├── net.py # 网络工具
|
|
322
|
+
│ │ ├── math_util.py # 数学工具
|
|
323
|
+
│ │ └── tree.py # 树工具
|
|
324
|
+
│ ├── http/ # HTTP 客户端(基于 httpx)
|
|
325
|
+
│ ├── json_util.py # JSON 工具
|
|
326
|
+
│ ├── crypto/ # 加密工具(基于 cryptography)
|
|
327
|
+
│ ├── cache/ # 缓存工具(FIFO/LFU/LRU/Timed)
|
|
328
|
+
│ ├── captcha/ # 验证码(基于 Pillow)
|
|
329
|
+
│ ├── dfa/ # 敏感词过滤(DFA 算法)
|
|
330
|
+
│ ├── extra/ # 扩展工具(拼音/Emoji/模板/二维码)
|
|
331
|
+
│ ├── cron/ # 定时任务
|
|
332
|
+
│ ├── jwt_util.py # JWT 工具
|
|
333
|
+
│ └── setting/ # 配置工具(YAML/Properties)
|
|
334
|
+
├── tests/ # 测试(47 个测试文件,613 个测试用例)
|
|
335
|
+
├── requirements.txt
|
|
336
|
+
└── setup.py
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
## 🧪 运行测试
|
|
342
|
+
|
|
343
|
+
```bash
|
|
344
|
+
# 安装开发依赖
|
|
345
|
+
pip install pytest
|
|
346
|
+
|
|
347
|
+
# 运行全部测试
|
|
348
|
+
pytest tests/ -v
|
|
349
|
+
|
|
350
|
+
# 运行核心模块测试
|
|
351
|
+
pytest tests/test_core/ -v
|
|
352
|
+
|
|
353
|
+
# 运行单个模块测试
|
|
354
|
+
pytest tests/test_core/test_str_util.py -v
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
---
|
|
358
|
+
|
|
359
|
+
## 🔧 依赖清单
|
|
360
|
+
|
|
361
|
+
| 依赖库 | 用途 | 对应模块 |
|
|
362
|
+
|--------|------|---------|
|
|
363
|
+
| `pendulum>=3.0` | 日期时间处理 | `core/date.py` |
|
|
364
|
+
| `httpx>=0.27` | HTTP 客户端 | `http/` |
|
|
365
|
+
| `cryptography>=42.0` | 加密算法 | `crypto/` |
|
|
366
|
+
| `qrcode[pil]>=7.0` | 二维码生成 | `extra/qr_code_util.py` |
|
|
367
|
+
| `Pillow>=10.0` | 图像处理(验证码) | `captcha/` |
|
|
368
|
+
| `pypinyin>=0.50` | 中文拼音转换 | `extra/pinyin_util.py` |
|
|
369
|
+
| `emoji>=2.0` | Emoji 处理 | `extra/emoji_util.py` |
|
|
370
|
+
| `jinja2>=3.0` | 模板引擎 | `extra/template_util.py` |
|
|
371
|
+
| `pyjwt>=2.8` | JWT 处理 | `jwt_util.py` |
|
|
372
|
+
| `pyyaml>=6.0` | YAML 解析 | `setting/yaml_util.py` |
|
|
373
|
+
| `sortedcontainers>=2.0` | 有序容器 | `cache/` |
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## 📝 Java Hutool 与 Hutool-Python 对照
|
|
378
|
+
|
|
379
|
+
### 方法名映射规则
|
|
380
|
+
|
|
381
|
+
| Java Hutool | Hutool-Python | 示例 |
|
|
382
|
+
|------------|---------------|------|
|
|
383
|
+
| `StrUtil.isBlank()` | `StrUtil.is_blank()` | 驼峰 → 蛇形 |
|
|
384
|
+
| `DateUtil.offsetDay()` | `DateUtil.offset_day()` | 驼峰 → 蛇形 |
|
|
385
|
+
| `new DateTime()` | `DateTime()` | Python 无 `new` |
|
|
386
|
+
| `CollUtil.newArrayList()` | `CollUtil.new_array_list()` | 驼峰 → 蛇形 |
|
|
387
|
+
|
|
388
|
+
### 使用对比
|
|
389
|
+
|
|
390
|
+
```java
|
|
391
|
+
// Java Hutool
|
|
392
|
+
String uuid = IdUtil.randomUUID();
|
|
393
|
+
boolean blank = StrUtil.isBlank(" ");
|
|
394
|
+
DateTime dt = DateUtil.parse("2024-01-15");
|
|
395
|
+
String md5 = SecureUtil.md5("hello");
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
```python
|
|
399
|
+
# Hutool-Python
|
|
400
|
+
uuid = IdUtil.random_uuid()
|
|
401
|
+
blank = StrUtil.is_blank(" ")
|
|
402
|
+
dt = DateUtil.parse("2024-01-15")
|
|
403
|
+
md5 = DigestUtil.md5_hex("hello")
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
## 🏗️ 贡献指南
|
|
409
|
+
|
|
410
|
+
欢迎为 Hutool-Python 贡献代码!请遵循以下原则:
|
|
411
|
+
|
|
412
|
+
1. **保持 Hutool 风格**:类名使用 PascalCase,方法名使用 snake_case
|
|
413
|
+
2. **中文注释**:所有新增方法须包含中文文档注释,格式为 Sphinx 风格(`:param:` / `:return:` / `:raises:`)
|
|
414
|
+
3. **编写测试**:新增方法须附带 pytest 测试用例
|
|
415
|
+
4. **不引入额外依赖**:尽量使用 Python 标准库,仅在必要时引入第三方依赖
|
|
416
|
+
|
|
417
|
+
### 贡献步骤
|
|
418
|
+
|
|
419
|
+
1. Fork 项目
|
|
420
|
+
2. 创建功能分支 (`git checkout -b feature/xxx`)
|
|
421
|
+
3. 提交代码 (`git commit -m 'Add xxx feature'`)
|
|
422
|
+
4. 推送到分支 (`git push origin feature/xxx`)
|
|
423
|
+
5. 创建 Pull Request
|
|
424
|
+
|
|
425
|
+
---
|
|
426
|
+
|
|
427
|
+
## 📖 相关链接
|
|
428
|
+
|
|
429
|
+
- [PyPI](https://pypi.org/project/hutool-python/)
|
|
430
|
+
- [Hutool-Python 在线文档](https://wgp520.github.io/hutool-python/)
|
|
431
|
+
- [Java Hutool 官方仓库](https://github.com/dromara/hutool)
|
|
432
|
+
- [Java Hutool 中文文档](https://doc.hutool.cn/pages/index/)
|
|
433
|
+
- [Hutool API 文档](https://plus.hutool.cn/apidocs/)
|
|
434
|
+
|
|
435
|
+
---
|
|
436
|
+
|
|
437
|
+
## 📄 许可证
|
|
438
|
+
|
|
439
|
+
本项目基于 [木兰宽松许可证, 第2版](https://license.coscl.org.cn/MulanPSL2) 开源,与 Java Hutool 保持一致。
|