Skip to main content

一个简单实用的 Python 工具包

Project description

yiwen-package

一个简单实用的 Python 工具包,提供常用的字符串、列表、文件和时间处理工具函数。

安装

pip install yiwen-package

功能特性

  • 字符串工具 - 命名转换、截断、前缀/后缀处理
  • 列表工具 - 分块、扁平化、去重、分组
  • 文件工具 - JSON 读写、目录管理、文件大小格式化
  • 时间工具 - 时间戳、日期格式化、相对时间

快速开始

from yiwen_package import (
    camel_to_snake,
    chunk,
    read_json,
    format_datetime,
    time_ago
)

# 字符串工具
camel_to_snake("MyClassName")  # "my_class_name"

# 列表工具
chunk([1, 2, 3, 4, 5], 2)  # [[1, 2], [3, 4], [5]]

# 文件工具
data = read_json("config.json")

# 时间工具
from datetime import datetime
format_datetime(datetime.now())  # "2023-11-06 14:30:45"

API 文档

字符串工具 (string_utils)

is_empty(s: str) -> bool

检查字符串是否为空或只包含空白字符。

is_empty("")        # True
is_empty("   ")     # True
is_empty("hello")   # False

truncate(s: str, length: int, suffix: str = '...') -> str

截断字符串到指定长度。

truncate("hello world", 8)              # "hello..."
truncate("hello world", 8, suffix=">>") # "hello >>"

remove_prefix(s: str, prefix: str) -> str

移除字符串的前缀。

remove_prefix("hello_world", "hello_")  # "world"

remove_suffix(s: str, suffix: str) -> str

移除字符串的后缀。

remove_suffix("hello_world", "_world")  # "hello"

camel_to_snake(s: str) -> str

将驼峰命名转换为下划线命名。

camel_to_snake("CamelCase")   # "camel_case"
camel_to_snake("myClassName") # "my_class_name"

snake_to_camel(s: str, capitalize_first: bool = False) -> str

将下划线命名转换为驼峰命名。

snake_to_camel("snake_case")                        # "snakeCase"
snake_to_camel("snake_case", capitalize_first=True) # "SnakeCase"

列表工具 (list_utils)

chunk(lst: List[T], size: int) -> List[List[T]]

将列表分割成指定大小的块。

chunk([1, 2, 3, 4, 5], 2)  # [[1, 2], [3, 4], [5]]

flatten(nested_list: List[Any]) -> List[Any]

展平嵌套列表。

flatten([[1, 2], [3, [4, 5]]])  # [1, 2, 3, 4, 5]

unique(lst: List[T], key=None) -> List[T]

去重,保持原始顺序。

unique([1, 2, 2, 3, 1])  # [1, 2, 3]

# 使用 key 函数
data = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}, {'id': 1, 'name': 'c'}]
unique(data, key=lambda x: x['id'])  # [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}]

group_by(items: Iterable[T], key_func) -> dict

按指定函数分组。

group_by([1, 2, 3, 4, 5, 6], lambda x: x % 2)
# {0: [2, 4, 6], 1: [1, 3, 5]}

文件工具 (file_utils)

read_json(file_path: Union[str, Path]) -> Any

读取 JSON 文件。

data = read_json("config.json")

write_json(file_path: Union[str, Path], data: Any, indent: int = 2) -> None

写入 JSON 文件。

write_json("output.json", {"key": "value"})

ensure_dir(dir_path: Union[str, Path]) -> Path

确保目录存在,如果不存在则创建。

ensure_dir("data/output")  # 创建 data/output 目录

get_file_size(file_path: Union[str, Path]) -> int

获取文件大小(字节)。

get_file_size("file.txt")  # 1024

format_file_size(size_bytes: int) -> str

格式化文件大小为人类可读格式。

format_file_size(1024)           # "1.00 KB"
format_file_size(1024 * 1024)    # "1.00 MB"

时间工具 (time_utils)

now_timestamp() -> int

获取当前时间戳(秒)。

now_timestamp()  # 1699200000

now_timestamp_ms() -> int

获取当前时间戳(毫秒)。

now_timestamp_ms()  # 1699200000000

format_datetime(dt: Optional[datetime] = None, fmt: str = '%Y-%m-%d %H:%M:%S') -> str

格式化日期时间。

from datetime import datetime
format_datetime(datetime(2023, 11, 6, 14, 30, 45))  # "2023-11-06 14:30:45"
format_datetime(fmt='%Y/%m/%d')  # "2023/11/06"

parse_datetime(date_str: str, fmt: str = '%Y-%m-%d %H:%M:%S') -> datetime

解析日期时间字符串。

parse_datetime("2023-11-06 14:30:45")  # datetime(2023, 11, 6, 14, 30, 45)

time_ago(dt: datetime) -> str

返回相对时间描述。

from datetime import datetime, timedelta
dt = datetime.now() - timedelta(hours=3)
time_ago(dt)  # "3小时前"

add_days(dt: datetime, days: int) -> datetime

增加或减少天数。

from datetime import datetime
dt = datetime(2023, 11, 6)
add_days(dt, 5)   # datetime(2023, 11, 11)
add_days(dt, -5)  # datetime(2023, 11, 1)

开发

安装开发依赖

pip install -e ".[dev]"

运行测试

pytest

查看测试覆盖率

pytest --cov=yiwen_package --cov-report=html

许可证

MIT License - 详见 LICENSE 文件

作者

yw.hao (yiwenlemo@gmail.com)

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

yiwen_package-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

yiwen_package-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file yiwen_package-0.1.1.tar.gz.

File metadata

  • Download URL: yiwen_package-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for yiwen_package-0.1.1.tar.gz
Algorithm Hash digest
SHA256 72fcfcef5943554a4257319f66df4d4c7edb5a01b41da0eaec6ad1640a71ad1f
MD5 f6fc20174d8d213e3485b307ee79457a
BLAKE2b-256 6fe07e188f0afebb8a1ed8713baedc7c04206c18301ba58c5afc1f0b66765ea2

See more details on using hashes here.

File details

Details for the file yiwen_package-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for yiwen_package-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48b1fadd45168b6cbddd85a66f40747b85aba5df461babf294d660dd06a35a4f
MD5 f6b1212544e34245bf088e7f67d5f90e
BLAKE2b-256 8f498ac766c4b0e0e9a9e3e07ea3efa9081010701fffb88cafd251a48ba58236

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