Skip to main content

A practical Python utility library for strings, dicts and time.

Project description

wyl-py-utils

一个实用的 Python 工具库,提供字符串、字典、时间相关的常用工具函数。

安装

pip install wyl-py-utils
# 或者用 uv
uv add wyl-py-utils

使用

from wyl_py_utils import camel_to_snake, deep_merge, now_str

字符串工具

camel_to_snake - 驼峰转下划线

from wyl_py_utils import camel_to_snake

camel_to_snake("helloWorld")       # 'hello_world'
camel_to_snake("getElementById")   # 'get_element_by_id'
camel_to_snake("HTMLParser")       # 'html_parser'

snake_to_camel - 下划线转驼峰

from wyl_py_utils import snake_to_camel

snake_to_camel("hello_world")                    # 'helloWorld'
snake_to_camel("hello_world", upper_first=True)  # 'HelloWorld'(大驼峰)
snake_to_camel("get_element_by_id")              # 'getElementById'

truncate - 截断字符串

from wyl_py_utils import truncate

truncate("这是一段很长的文字需要被截断", 5)         # '这是一段很...'
truncate("Hi", 10)                                # 'Hi'(不超长则原样返回)
truncate("Hello World", 5, suffix="…")            # 'Hello…'(自定义省略符)

random_string - 生成随机字符串

from wyl_py_utils import random_string

random_string()          # 'aB3kF9xM2pQ7wR1s'(默认16位,字母+数字)
random_string(8)         # 'xK3mP9qW'(指定长度)
random_string(4, "abc")  # 'abca'(指定字符集)

mask_string - 字符串脱敏

from wyl_py_utils import mask_string

mask_string("13800138000")                     # '138****8000'
mask_string("wangyanli@example.com", 2, 4)     # 'wa*************m.com'
mask_string("6225880123456789", 4, 4)          # '6225********6789'(银行卡号)

字典工具

deep_merge - 深度合并字典

from wyl_py_utils import deep_merge

deep_merge(
    {"a": 1, "b": {"x": 1}},
    {"b": {"y": 2}, "c": 3}
)
# {'a': 1, 'b': {'x': 1, 'y': 2}, 'c': 3}

pick - 挑选指定 key

类似 JS 的 lodash.pick

from wyl_py_utils import pick

pick({"a": 1, "b": 2, "c": 3}, ["a", "c"])  # {'a': 1, 'c': 3}
pick({"a": 1}, ["a", "x"])                    # {'a': 1}(不存在的 key 会跳过)

omit - 排除指定 key

类似 JS 的 lodash.omit

from wyl_py_utils import omit

omit({"a": 1, "b": 2, "c": 3}, ["b"])  # {'a': 1, 'c': 3}

flatten_dict - 嵌套字典展平

from wyl_py_utils import flatten_dict

flatten_dict({"a": 1, "b": {"x": 2, "y": {"z": 3}}})
# {'a': 1, 'b.x': 2, 'b.y.z': 3}

flatten_dict({"a": {"b": 1}}, sep="/")  # {'a/b': 1}(自定义分隔符)

时间工具

now_str - 获取当前时间字符串

from wyl_py_utils import now_str

now_str()                          # '2026-04-24 21:00:00'
now_str("%Y/%m/%d")                # '2026/04/24'(自定义格式)
now_str("%H:%M")                   # '21:00'

timestamp_to_str - 时间戳转字符串

自动识别秒级和毫秒级时间戳

from wyl_py_utils import timestamp_to_str

timestamp_to_str(1700000000)        # '2023-11-14 22:13:20'(秒级)
timestamp_to_str(1700000000000)     # '2023-11-14 22:13:20'(毫秒级,自动识别)
timestamp_to_str(1700000000, "%Y-%m-%d")  # '2023-11-14'

str_to_timestamp - 字符串转时间戳

from wyl_py_utils import str_to_timestamp

str_to_timestamp("2023-11-14 22:13:20")  # 1700000000

time_ago - 友好时间展示

类似微博、朋友圈的时间展示

from wyl_py_utils import time_ago
from datetime import datetime, timedelta

time_ago(datetime.now())                                    # '刚刚'
time_ago(datetime.now() - timedelta(minutes=5))             # '5分钟前'
time_ago(datetime.now() - timedelta(hours=3))               # '3小时前'
time_ago(datetime.now() - timedelta(days=7))                # '7天前'
time_ago(1700000000)                                        # '2年前'(支持时间戳)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wyl_py_utils-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wyl_py_utils-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file wyl_py_utils-0.1.0.tar.gz.

File metadata

  • Download URL: wyl_py_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wyl_py_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f79f50e3d5c44ac0ff97657ccaa70c6324d79ab4a607d3a75fd002401bbff0bf
MD5 5e8817738deb57f14a49cccc06c51ac7
BLAKE2b-256 fef4071863888f73329592190c109449d35f21c6844c7f62f6b7e0c55a9ced0d

See more details on using hashes here.

File details

Details for the file wyl_py_utils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wyl_py_utils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wyl_py_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f537b546ad219d0e61815ed1498beeb87c50c0292e89040638131a81f77aeb2c
MD5 4145a3e3e479c9926ab18b6c6a2309f8
BLAKE2b-256 467958aa355e49fff0059fca5cc25b588b5a5a48c33262e87e176b90b11c8712

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page