Skip to main content

This is a simple open-source Python toolkit. It is currently in the testing phase and only implements simplified handling of a few specific pandas commands, as well as the ability to set up a basic Flask server with one click.

Project description

mzy_py

简易 Python 工具包 | 一键化常见操作

Python Version Version License

简介

这是一个简单的开源 Python 工具包。目前处于测试阶段,已实现数据处理、网络请求、数据可视化、机器学习简化调用等功能,一行代码搞定常见操作。

安装

pip install mzy-py

或者从源码安装:

git clone https://github.com/MaZiYan/mzy_py.git
cd mzy_py
pip install -e .

快速开始

from mzy_py import *

# 查看所有函数文档
show_docs()

# 数据处理
df = du_csv("data.csv")
df = du_excel("data.xlsx")
data = du_json("config.json")
to_json({"name": "mzy"}, "output.json")

# 数据可视化
quick_plot(df, kind="line", title="示例图表")

# 机器学习
X_train, X_test, y_train, y_test = train_test_split(X, y)
pred = knn(X_train, y_train, X_test, k=5)

# 网络相关
download_file("https://example.com/file.zip")
run_html_server(html_file="index.html")
serve_folder(folder="./public")

# 其他工具
QRcodegen_txt("https://github.com")
compress_img("input.jpg", "output.jpg", quality=60)

功能详解

📊 数据处理

函数 说明
du_csv(filepath) 读取 CSV 文件,返回 DataFrame
du_excel(filepath) 读取 Excel 文件,返回 DataFrame
du_json(filepath) 读取 JSON 文件,返回 dict 或 list
to_json(data, filepath) 将数据写入 JSON 文件
lianjie(a, b) 按列拼接两个 DataFrame

示例:

from mzy_py import du_csv, du_excel, du_json, to_json

# 读取 CSV
df = du_csv("data.csv")
print(df.head())

# 读取 Excel
df = du_excel("data.xlsx")

# 读取 JSON
data = du_json("config.json")
print(data)

# 写入 JSON
to_json({"name": "mzy", "version": "0.3.0"}, "output.json")

🌐 网络相关

函数 说明
download_file(url, save_path, show_progress) 下载文件(支持进度条)
run_html_server(html_file, port, host, ...) 启动 HTML 文件服务器
serve_folder(folder, port, host, ...) 启动文件夹托管服务器

示例:

from mzy_py import download_file, run_html_server, serve_folder

# 下载文件(自动显示进度条)
path = download_file("https://example.com/file.zip")

# 下载到指定目录
path = download_file("https://example.com/file.zip", "C:/Downloads/")

# 下载到指定文件
path = download_file("url", "D:/files/custom_name.zip")

# 启动 HTML 服务器
run_html_server(html_file="index.html", port=8080)

# 允许局域网访问
run_html_server(html_file="index.html", host="0.0.0.0", port=5000)

# 托管整个文件夹
serve_folder(folder="./public", port=8080)

run_html_server / serve_folder 参数:

参数 默认值 说明
html_file / folder "index.html" / "." 文件或文件夹路径
port 5000 端口号
host "127.0.0.1" 监听地址("0.0.0.0" 允许局域网访问)
auto_open_browser True 启动后自动打开浏览器
quit_keys "q" 按键退出(输入 q 回车退出)
log_level 0 日志级别(0=静默, 1=简洁, 2=详细)

📊 数据可视化

函数 说明
quick_plot(data, kind, title, ...) 一键生成图表
save_plot(fig, save_path, dpi) 保存图表到文件

支持的图表类型: line, bar, scatter, hist, box, pie, area

示例:

from mzy_py import quick_plot, save_plot
import pandas as pd

# 折线图
df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [10, 20, 15, 25]})
quick_plot(df, kind="line", title="折线图示例")

# 柱状图
quick_plot([10, 20, 30, 40], kind="bar", title="柱状图")

# 散点图
quick_plot(df, kind="scatter", title="散点图")

# 保存图表
fig = quick_plot([1, 2, 3], kind="line")
save_plot(fig, "output.png", dpi=200)

quick_plot 参数:

参数 说明
data 绘图数据(DataFrame, Series, list, dict)
kind 图表类型:line, bar, scatter, hist, box, pie, area
title 图表标题
xlabel / ylabel X/Y 轴标签
figsize 图形尺寸,默认 (10, 6)
save_path 保存路径(如指定则保存图片)
show 是否显示图表,默认 True

🤖 机器学习

函数 说明
train_test_split(X, y, test_size, random_state) 数据集分割
knn(X_train, y_train, X_test, k) K近邻分类
linear_regression(X_train, y_train, X_test) 线性回归
kmeans(X, n_clusters) K-Means 聚类
accuracy_score(y_true, y_pred) 计算准确率

示例:

from mzy_py import train_test_split, knn, linear_regression, kmeans, accuracy_score

# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# K近邻分类
pred = knn(X_train, y_train, X_test, k=5)
acc = accuracy_score(y_test, pred)
print(f"准确率: {acc:.2%}")

# 线性回归
pred = linear_regression(X_train, y_train, X_test)

# K-Means 聚类
labels = kmeans(data, n_clusters=3)

🖼️ 图片处理

函数 说明
QRcodegen_txt(text, save_path) 生成二维码图片
compress_img(tar_src, res_dst, quality) JPEG 图片压缩

示例:

from mzy_py import QRcodegen_txt, compress_img

# 生成二维码
QRcodegen_txt("https://github.com/MaZiYan")

# 压缩图片
compress_img("input.jpg", "output.jpg", quality=60)

🔧 常用工具

函数 说明
dayin(content) 打印内容(简化 print)
plus_mzy(a, b) 两个数相加
minus_mzy(a, b) 两个数相减
print_info() 打印包信息
show_docs() 在浏览器中显示精美文档

示例:

from mzy_py import dayin, plus_mzy, minus_mzy, print_info, show_docs

# 打印
dayin("Hello World!")

# 数学运算
result = plus_mzy(1, 2)    # 返回 3
result = minus_mzy(5, 3)    # 返回 2

# 打印包信息
print_info()

# 在浏览器中显示文档
show_docs()

版本更新

v0.3.0 (最新)

  • ✅ 重构 run_html_server 函数,修复 Ctrl+C 无法正常关闭的问题
  • ✅ 新增 auto_open_browser 参数,启动后自动打开浏览器
  • ✅ 新增 quit_keys 参数,支持按键退出
  • ✅ 新增 log_level 参数,控制日志输出级别
  • ✅ 新增命令行支持:python -m mzy_py.server
  • ✅ 改进启动信息显示,更清晰展示服务器地址
  • ✅ 新增 du_json()to_json(),简化 JSON 文件读写
  • ✅ 新增 download_file(),支持下载文件(带进度条)
  • ✅ 新增 serve_folder(),托管整个文件夹
  • ✅ 新增 quick_plot(),一键生成图表(支持 line/bar/scatter/hist/box/pie)
  • ✅ 新增 save_plot(),保存图表到文件
  • ✅ 新增机器学习简化函数:train_test_split, knn, linear_regression, kmeans, accuracy_score
  • ✅ 新增 show_docs(),在浏览器中显示所有函数的精美文档

v0.2.9

  • 放开 numpy 版本限制,兼容 numpy 1.x 和 2.x
  • 支持 Python 3.13

项目信息


贡献

欢迎提交 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

mzy_py-0.3.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

mzy_py-0.3.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file mzy_py-0.3.0.tar.gz.

File metadata

  • Download URL: mzy_py-0.3.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mzy_py-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6451afd77637029485753f4c3417e8e603387893d07e5cd63a5e007c0fb86cc2
MD5 caa6f474562ef21064231b1bceb6e803
BLAKE2b-256 78aae704e59599938fdf923b7cfc20cb0f8b8124d3a8c321eb838b306fb8814c

See more details on using hashes here.

File details

Details for the file mzy_py-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mzy_py-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mzy_py-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47dbe2c2c4b334040acc05621628f16026965123fedfbc26fd1d27943d2aab98
MD5 d02cf38facd05decc2ca22a76267fce3
BLAKE2b-256 d88f65e40c78c41a565776732b4176fa6880e059ff161fe3e2c55ddc98a8dc62

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