Skip to main content

梦无矶的python工具库,不定时更新...

Project description

MWJ Tools - 实用的Python工具库

MWJ Tools是一个实用的Python工具库,提供了丰富的工具类来简化日常开发任务。目前包含日期时间处理和表格数据处理两大核心功能模块。

安装

uv add mwj_tools

功能特性

1. 日期时间处理工具 (DateTimeUtils)

提供时间计算、转换、格式化等常用功能:

  • 获取当前时间
  • 时间加减计算(支持天、小时、分钟、月份)
  • 时间戳转换
  • 计算时间差
  • 计算未来日期
  • 时间格式化
  • 周末判断
  • 获取周范围

使用示例

from mwj_tools import DateTimeUtils

# 获取当前时间
current_time = DateTimeUtils.now()
formatted_time = DateTimeUtils.now('%Y-%m-%d %H:%M:%S')

# 时间加减计算
future_time = DateTimeUtils.add_time(days=5, hours=3)
past_time = DateTimeUtils.add_time(months=-2)

# 时间戳转换
timestamp = DateTimeUtils.to_timestamp()
dt_from_timestamp = DateTimeUtils.from_timestamp(timestamp)

# 计算时间差
diff = DateTimeUtils.time_difference('2025-01-01', unit='days')

# 获取未来日期
future_date = DateTimeUtils.future_date(30)

# 格式化时间
formatted = DateTimeUtils.format_time('2025-07-15 10:30:00', '%Y年%m月%d日')

# 判断是否为周末
is_weekend = DateTimeUtils.is_weekend()

# 获取周范围
week_range = DateTimeUtils.get_week_range()

2. 表格数据处理工具 (TableUtils)

提供数据读取、清洗、转换、分析等常用功能:

  • 读取多种格式表格文件(CSV、Excel、JSON)
  • 保存表格到文件
  • 数据筛选
  • 数据分组聚合
  • 表格合并
  • 数据清洗(处理缺失值)
  • 生成数据描述统计
  • 创建数据透视表

使用示例

from mwj_tools import TableUtils
import pandas as pd

# 读取表格文件
df = TableUtils.read_table('data.csv')  # 自动识别格式
df = TableUtils.read_table('data.xlsx', file_type='excel')

# 保存表格文件
TableUtils.save_table(df, 'output.csv')

# 数据筛选
filtered_df = TableUtils.filter_data(df, {
    'age': ('>=', 18),
    'city': 'Beijing',
    'name': ('contains', 'John')
})

# 数据聚合
aggregated_df = TableUtils.aggregate_data(df, 
    group_by=['department'], 
    aggregations={'salary': ['mean', 'sum'], 'age': 'max'}
)

# 表格合并
merged_df = TableUtils.merge_tables(df1, df2, on='id', how='left')

# 数据清洗
cleaned_df = TableUtils.clean_data(df, strategy='fill', fill_value=0)

# 生成数据描述统计
stats = TableUtils.describe_data(df)

# 创建数据透视表
pivot_df = TableUtils.pivot_table(df, 
    index='category', 
    columns='month', 
    values='sales', 
    aggfunc='sum'
)

项目结构

mwj_tools/
├── src/
│   └── mwj_tools/
│       ├── __init__.py
│       ├── datetime_utils.py      # 日期时间处理工具
│       └── table_utils.py         # 表格数据处理工具
├── tests/
│   ├── test_datetime_utils.py
│   └── test_table_utils.py
├── examples/
│   ├── datetime_example.py
│   └── table_example.py
├── README.md
└── pyproject.toml

开发与测试

运行测试

# 运行所有测试
uv run pytest tests/

# 运行特定模块测试
uv run pytest tests/test_datetime_utils.py
uv run pytest tests/test_table_utils.py

示例代码

查看 examples/ 目录获取更多使用示例。

依赖

  • python >= 3.13
  • openpyxl
  • pandas
  • python-dateutil

作者

许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

贡献

欢迎提交 Issue 和 Pull Request 来帮助改进此项目。

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

mwj_tools-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

mwj_tools-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mwj_tools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e8724130391f0a686b33aff0b017cc62f1d2f844194ca11dcc1fef86f3713c5
MD5 7d109291d9644b49785b7f04135058cd
BLAKE2b-256 48c49bbf4b89521e12777280ed8069376226c292d310622480664e04fed19f32

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mwj_tools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1aa942216da611f61ae7fc24b003d13104e8f13cb8bb2b5743898bc987963e0a
MD5 d1d945b9e785fd68d635ac68c20c43e8
BLAKE2b-256 275cbc7ddb3a5526b90b7956f67a8c7b87aad3023cdefad4457542543c1dbf70

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