ph-utils 0.1.2__tar.gz → 0.2.2__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.
- {ph_utils-0.1.2 → ph_utils-0.2.2}/PKG-INFO +7 -1
- {ph_utils-0.1.2 → ph_utils-0.2.2}/README.md +6 -0
- {ph_utils-0.1.2 → ph_utils-0.2.2}/pyproject.toml +2 -1
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/crypto.py +73 -72
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/date.py +100 -26
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/__init__.py +0 -0
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/common.py +0 -0
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/config.py +0 -0
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/id.py +0 -0
- {ph_utils-0.1.2 → ph_utils-0.2.2}/src/ph_utils/logger.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: ph-utils
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: The python3 tool classes
|
|
5
5
|
Author: Tenny
|
|
6
6
|
Author-email: Tenny <tenny.shu@foxmail.com>
|
|
@@ -36,3 +36,9 @@ pip install ph-utils
|
|
|
36
36
|
### 5. [config](https://gitee.com/towardly/ph-utils_py/wikis/config_en): Configure relevant tool classes
|
|
37
37
|
|
|
38
38
|
### 6. [id](https://gitee.com/towardly/ph-utils_py/wikis/id): 生成唯一 ID 工具类
|
|
39
|
+
|
|
40
|
+
## 运行测试
|
|
41
|
+
|
|
42
|
+
```shell
|
|
43
|
+
uv run python tests/crypto_utils.py
|
|
44
|
+
```
|
|
@@ -21,3 +21,9 @@ pip install ph-utils
|
|
|
21
21
|
### 5. [config](https://gitee.com/towardly/ph-utils_py/wikis/config_en): Configure relevant tool classes
|
|
22
22
|
|
|
23
23
|
### 6. [id](https://gitee.com/towardly/ph-utils_py/wikis/id): 生成唯一 ID 工具类
|
|
24
|
+
|
|
25
|
+
## 运行测试
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
uv run python tests/crypto_utils.py
|
|
29
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "ph-utils"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.2"
|
|
4
4
|
description = "The python3 tool classes"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Tenny", email = "tenny.shu@foxmail.com" },
|
|
@@ -30,4 +30,5 @@ build-backend = "uv_build"
|
|
|
30
30
|
[dependency-groups]
|
|
31
31
|
dev = [
|
|
32
32
|
"ruff>=0.14.10",
|
|
33
|
+
"pytest>=8.0",
|
|
33
34
|
]
|
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
# Copyright (c) [2023] [Tenny]
|
|
2
|
-
# [ph-utils] is licensed under Mulan PSL v2.
|
|
3
|
-
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
4
|
-
# You may obtain a copy of Mulan PSL v2 at:
|
|
5
|
-
# http://license.coscl.org.cn/MulanPSL2
|
|
6
|
-
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
7
|
-
# See the Mulan PSL v2 for more details.
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def hash(d: str, method="sha1", upper=False, result="hex"):
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
Args:
|
|
16
|
-
d (str): 待加密的数据
|
|
17
|
-
method (str, optional): 签名算法, md5、sha1、sha256
|
|
18
|
-
upper (bool, optional): 返回的结果是否需要大写. Defaults to False.
|
|
19
|
-
result (str, optional): 返回数据, hex 转换为 HEX 返回
|
|
20
|
-
|
|
21
|
-
Raises:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Returns:
|
|
25
|
-
str: 加密后的结果
|
|
26
|
-
"""
|
|
27
|
-
if
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
# Copyright (c) [2023] [Tenny]
|
|
2
|
+
# [ph-utils] is licensed under Mulan PSL v2.
|
|
3
|
+
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
4
|
+
# You may obtain a copy of Mulan PSL v2 at:
|
|
5
|
+
# http://license.coscl.org.cn/MulanPSL2
|
|
6
|
+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
7
|
+
# See the Mulan PSL v2 for more details.
|
|
8
|
+
import base64
|
|
9
|
+
import hashlib
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def hash(d: str, method="sha1", upper=False, result="hex"):
|
|
13
|
+
"""使用 hashlib 支持的算法进行数据摘要签名
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
d (str): 待加密的数据
|
|
17
|
+
method (str, optional): 签名算法, 如 md5、sha1、sha256、sha512 等 hashlib 支持的算法. Defaults to "sha1".
|
|
18
|
+
upper (bool, optional): 返回的结果是否需要大写. Defaults to False.
|
|
19
|
+
result (str, optional): 返回数据, hex 转换为 HEX 返回
|
|
20
|
+
|
|
21
|
+
Raises:
|
|
22
|
+
ValueError: 不支持的签名算法
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
str: 加密后的结果
|
|
26
|
+
"""
|
|
27
|
+
if not hasattr(hashlib, method):
|
|
28
|
+
raise ValueError(
|
|
29
|
+
f"不支持的签名算法: {method}, 可用算法: {', '.join(hashlib.algorithms_guaranteed)}"
|
|
30
|
+
)
|
|
31
|
+
func = getattr(hashlib, method)
|
|
32
|
+
althorithm = func()
|
|
33
|
+
althorithm.update(d.encode("utf-8"))
|
|
34
|
+
if result == "hex":
|
|
35
|
+
rd = althorithm.hexdigest()
|
|
36
|
+
return rd if upper is False else rd.upper()
|
|
37
|
+
else:
|
|
38
|
+
return althorithm.digest()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def b64encode(data, result="str"):
|
|
42
|
+
"""base64加密
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
data (str): 待base64加密数据
|
|
46
|
+
result (str, optional): 加密返回结果, str、bytes. Defaults to "str".
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
str|bytes: base64加密后的数据
|
|
50
|
+
"""
|
|
51
|
+
if not data:
|
|
52
|
+
return None
|
|
53
|
+
encode_data = data
|
|
54
|
+
if isinstance(encode_data, str):
|
|
55
|
+
encode_data = encode_data.encode()
|
|
56
|
+
encoded = base64.b64encode(encode_data)
|
|
57
|
+
return encoded.decode() if result == "str" else encoded
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def b64decode(data, result="str"):
|
|
61
|
+
"""base64解密
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
data (str|bytes): base64加密后的数据
|
|
65
|
+
result (str, optional): str、bytes. Defaults to "str".
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
str|bytes: base64解密后的数据
|
|
69
|
+
"""
|
|
70
|
+
if not data:
|
|
71
|
+
return None
|
|
72
|
+
decoded = base64.b64decode(data)
|
|
73
|
+
return decoded.decode() if result == "str" else decoded
|
|
@@ -246,51 +246,125 @@ def _plus_year(ori_date: datetime, year: int) -> datetime:
|
|
|
246
246
|
return ori_date.replace(year=new_year)
|
|
247
247
|
|
|
248
248
|
|
|
249
|
-
def add(
|
|
249
|
+
def add(
|
|
250
|
+
ori_date: OriDateType = None,
|
|
251
|
+
*,
|
|
252
|
+
days: int | float = 0,
|
|
253
|
+
months: int = 0,
|
|
254
|
+
years: int = 0,
|
|
255
|
+
weeks: int | float = 0,
|
|
256
|
+
hours: int | float = 0,
|
|
257
|
+
minutes: int | float = 0,
|
|
258
|
+
seconds: int | float = 0,
|
|
259
|
+
microseconds: int | float = 0,
|
|
260
|
+
milliseconds: int | float = 0,
|
|
261
|
+
) -> datetime:
|
|
250
262
|
"""将日期进行加法操作, 通常用于计算几天、几月、几年之后的日期
|
|
251
263
|
|
|
252
264
|
Args:
|
|
253
265
|
ori_date (datetime | str, optional): 待添加的日期. Defaults to None.
|
|
254
|
-
|
|
266
|
+
days (int | float, optional): 天数. Defaults to 0.
|
|
267
|
+
months (int, optional): 月数. Defaults to 0.
|
|
268
|
+
years (int, optional): 年数. Defaults to 0.
|
|
269
|
+
weeks (int | float, optional): 周数. Defaults to 0.
|
|
270
|
+
hours (int | float, optional): 小时数. Defaults to 0.
|
|
271
|
+
minutes (int | float, optional): 分钟数. Defaults to 0.
|
|
272
|
+
seconds (int | float, optional): 秒数. Defaults to 0.
|
|
273
|
+
microseconds (int | float, optional): 微秒数. Defaults to 0.
|
|
274
|
+
milliseconds (int | float, optional): 毫秒数. Defaults to 0.
|
|
255
275
|
|
|
256
276
|
Returns:
|
|
257
277
|
datetime: 添加后的日期
|
|
258
278
|
"""
|
|
259
|
-
if not delta:
|
|
260
|
-
delta = {"days": 0}
|
|
261
279
|
ori_date = parse(ori_date)
|
|
262
|
-
if
|
|
263
|
-
ori_date = _plus_year(ori_date,
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
280
|
+
if years:
|
|
281
|
+
ori_date = _plus_year(ori_date, years)
|
|
282
|
+
if months:
|
|
283
|
+
ori_date = _plus_month(ori_date, months)
|
|
284
|
+
|
|
285
|
+
diff = timedelta(
|
|
286
|
+
days=days,
|
|
287
|
+
weeks=weeks,
|
|
288
|
+
hours=hours,
|
|
289
|
+
minutes=minutes,
|
|
290
|
+
seconds=seconds,
|
|
291
|
+
microseconds=microseconds,
|
|
292
|
+
milliseconds=milliseconds,
|
|
293
|
+
)
|
|
294
|
+
ori_date = ori_date + diff
|
|
271
295
|
return ori_date
|
|
272
296
|
|
|
273
297
|
|
|
274
|
-
def subtract(
|
|
275
|
-
|
|
298
|
+
def subtract(
|
|
299
|
+
ori_date: OriDateType = None,
|
|
300
|
+
*,
|
|
301
|
+
days: int | float = 0,
|
|
302
|
+
months: int = 0,
|
|
303
|
+
years: int = 0,
|
|
304
|
+
weeks: int | float = 0,
|
|
305
|
+
hours: int | float = 0,
|
|
306
|
+
minutes: int | float = 0,
|
|
307
|
+
seconds: int | float = 0,
|
|
308
|
+
microseconds: int | float = 0,
|
|
309
|
+
milliseconds: int | float = 0,
|
|
310
|
+
):
|
|
311
|
+
"""将日期进行减法操作, 通常用于计算几天、几月、几年之前的日期
|
|
276
312
|
|
|
277
313
|
Args:
|
|
278
314
|
ori_date (datetime | str, optional): 原始日期. Defaults to None.
|
|
279
|
-
|
|
315
|
+
days (int | float, optional): 天数. Defaults to 0.
|
|
316
|
+
months (int, optional): 月数. Defaults to 0.
|
|
317
|
+
years (int, optional): 年数. Defaults to 0.
|
|
318
|
+
weeks (int | float, optional): 周数. Defaults to 0.
|
|
319
|
+
hours (int | float, optional): 小时数. Defaults to 0.
|
|
320
|
+
minutes (int | float, optional): 分钟数. Defaults to 0.
|
|
321
|
+
seconds (int | float, optional): 秒数. Defaults to 0.
|
|
322
|
+
microseconds (int | float, optional): 微秒数. Defaults to 0.
|
|
323
|
+
milliseconds (int | float, optional): 毫秒数. Defaults to 0.
|
|
280
324
|
|
|
281
325
|
Returns:
|
|
282
|
-
datetime:
|
|
326
|
+
datetime: 减去之后的日期
|
|
283
327
|
"""
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
328
|
+
return add(
|
|
329
|
+
ori_date,
|
|
330
|
+
days=-days,
|
|
331
|
+
months=-months,
|
|
332
|
+
years=-years,
|
|
333
|
+
weeks=-weeks,
|
|
334
|
+
hours=-hours,
|
|
335
|
+
minutes=-minutes,
|
|
336
|
+
seconds=-seconds,
|
|
337
|
+
microseconds=-microseconds,
|
|
338
|
+
milliseconds=-milliseconds,
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def sub(
|
|
343
|
+
ori_date: OriDateType = None,
|
|
344
|
+
*,
|
|
345
|
+
days: int | float = 0,
|
|
346
|
+
months: int = 0,
|
|
347
|
+
years: int = 0,
|
|
348
|
+
weeks: int | float = 0,
|
|
349
|
+
hours: int | float = 0,
|
|
350
|
+
minutes: int | float = 0,
|
|
351
|
+
seconds: int | float = 0,
|
|
352
|
+
microseconds: int | float = 0,
|
|
353
|
+
milliseconds: int | float = 0,
|
|
354
|
+
):
|
|
292
355
|
"""subtract 函数别名"""
|
|
293
|
-
return subtract(
|
|
356
|
+
return subtract(
|
|
357
|
+
ori_date,
|
|
358
|
+
days=days,
|
|
359
|
+
months=months,
|
|
360
|
+
years=years,
|
|
361
|
+
weeks=weeks,
|
|
362
|
+
hours=hours,
|
|
363
|
+
minutes=minutes,
|
|
364
|
+
seconds=seconds,
|
|
365
|
+
microseconds=microseconds,
|
|
366
|
+
milliseconds=milliseconds,
|
|
367
|
+
)
|
|
294
368
|
|
|
295
369
|
|
|
296
370
|
@overload
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|