Skip to main content

pylhb

PyPI version Downloads star Fork License

pylhb 是我在工作过程中陆续整理的一个 Python 工具包,里面放了一些自己反复用到的模块和函数。与其说是一个正式的开源项目,不如说是我自己的“代码杂物间”——只不过把它打包了一下,方便在不同项目之间复用。

由于是个人使用为主,很多设计可能带着比较强的个人习惯,也未必是最优解。
如果您发现了问题或有更好的建议,非常欢迎指正。
申明:操作前请作好备份,本人对所有操作结果拥有所有解释权。

安装

pip install pylhb

🌺命令

Microsoft SQL Server命令:

描述 命令
创建数据库 pylhb mssql create -S localhost\sqlexpress -U sa -P fpsoft@123 -D test -mdf D:\dd\test.mdf -ldf D:\dd\test_log.ldf
附加数据库 pylhb mssql attach -S localhost\sqlexpress -U sa -P fpsoft@123 -D test -mdf D:\dd\test.mdf -ldf D:\dd\test_log.ldf
分离数据库 pylhb mssql detach -S localhost\sqlexpress -U sa -P fpsoft@123 -D test
备份数据库 pylhb mssql backup -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer --file D:\dd\bkfile.bak
备份所有数据库 pylhb mssql backupall -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer --file D:\dd\name.bak
恢复数据库 pylhb mssql restore -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer --file D:\dd\bkfile.bak
清除日志 pylhb mssql dellog -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer
清除null pylhb mssql clearnull -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer
删除数据库 pylhb mssql drop -S localhost\sqlexpress -U sa -P fpsoft@123 -D test --force
执行SQL脚本 pylhb mssql runsql -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer --sql “SELECT * FROM Dt_Users”
执行SQL文件 pylhb mssql runsqlfile -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer --file D:\dd\test.sql
架构对比(即对比更新) pylhb mssql compare -S localhost\sqlexpress -U sa -P fpsoft@123 -D MyCustomer -TS localhost\sqlexpress -TU sa -TP fpsoft@123 -TD Test --comparetofile --file D:\dd\bkfile.bak
启动SQL服务 pylhb mssql startservice --servicename MSSQLSERVER
停止SQL服务 pylhb mssql stopservice --servicename MSSQLSERVER
打开SSMS pylhb mssql open
打开菜单 pylhb mssql menu
帮助 pylhb mssql help

IIS命令:

描述 命令
创建应用程序池 pylhb iis createapppool --poolname MyPool
创建应用程序池(CLR版本) pylhb iis createapppool --poolname MyPool --runtime v4.0 --start32
删除应用程序池 pylhb iis deleteapppool --poolname MyPool
创建网站 pylhb iis createwebsite --sitename MyWebsite --path D:\dd --port 88 --poolname MyPool
创建网站下的应用 pylhb iis createwebsiteapp --sitename MyWebsite --apppath /webapi --path D:\dd --poolname MyPool
删除网站 pylhb iis deletewebsite --sitename MyWebsite
删除网站应用 pylhb iis deletewebsiteapp --sitename MyWebsite --apppath /webapi
检查应用程序池是否存在 pylhb iis checkapppool --poolname MyPool
检查网站是否存在 pylhb iis checkwebsite --sitename MyWebsite
检查网站下在应用是否存在 pylhb iis checkwebsiteapp --sitename MyWebsite --apppath /webapi
启动应用程序池 pylhb iis startapppool --poolname MyPool
停止应用程序池 pylhb iis stopapppool --poolname MyPool
启动网站 pylhb iis startwebsite --sitename MyWebsite
停止网站 pylhb iis stopwebsite --sitename MyWebsite
获取应用程序池状态 pylhb iis getapppoolstate --poolname MyPool
获取网站状态 pylhb iis getwebsitestate --sitename MyWebsite
获取应用程序池列表 pylhb iis getapppoollist
获取网站列表 pylhb iis getwebsitelist
备份IIS pylhb iis backupiis --backupname bk2026
恢复IIS pylhb iis restoreiis --backupname bk2026
打开IIS管理器 pylhb iis open
打开菜单 pylhb iis menu
帮助 pylhb iis help

Download命令:

描述 命令
打开菜单 pylhb download menu
帮助 pylhb download help

Email命令:

描述 命令
打开QQ邮箱 pylhb email openqqmail
打开HotMail pylhb email openhotmail
发送邮件 pylhb email send --sendmail lhb@qq.com --authcode aaaaaaaaa --tomail cmerp@hotmail.com --file D:\dd\test.rar
打开菜单 pylhb email menu
帮助 pylhb email help

QRCode命令:

描述 命令
生成二维码 pylhb qrcode save --data http://www.fsyixinlc.com --desc 扫一扫查看更多信息 --file qrcode.png
显示二维码 pylhb qrcode show --data http://www.fsyixinlc.com
打开菜单 pylhb qrcode menu
帮助 pylhb qrcode help

🌺mymssql模块

通过ODBC访问Microsoft SQL Server。

注意:

ODBC Driver 17 for SQL Server下载:
https://learn.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver16
注意,如果是ODBC Driver 18 for SQL Server,那实例化时记得传driver.

使用示例:

from pylhb import MyMSSQL

if __name__ == "__main__":
    server="127.0.0.1"
    user="sa"
    password="fpsoft@123"
    database="MyCustomer"
    # 实例化
    #mssql=MyMSSQL(server=server,database=database)
    mssql=MyMSSQL(server=server,user=user,password=password,database=database)
    # 连接数据库
    (successed,msg)=mssql.connect()
    # print(successed)
    # print(msg)

    # Demo1:查询数据
    sql="SELECT TOP 2 P_CusName,P_Tel FROM Dt_Customers WITH(NOLOCK)"
    print("🌸Demot1:获取客户:")
    humans=mssql.get(sql)
    print(humans)

    # Demo2:执行无参存储过程
    # (successed,msg) = mssql.execProc("Usp_TestNoArgs")
    # print("🌸Demot2:执行无参存储过程(Usp_TestNoArgs):")
    # print(successed,msg)

    # Demo3:执行带参存储过程
    # (successed,msg) = mssql.execProc("Usp_TestWithArgs",(99,"1号机"))
    # print("🌸Demot3:执行带参存储过程(Usp_TestWithArgs):")
    # print(successed,msg)

    # Demo4:执行存储过程并返回数据
    # (successed,msg,datas) = mssql.execProcGet("Usp_Test",("",))
    # print("🌸Demot4:执行存储过程并返回数据(Usp_Test):")
    # print(successed,msg,datas)

    # Demo5:Insert
    # user1 = {"P_UserName": "张三", "P_Age": 25, "P_Email": "Zhang3@example.com"}
    # user2 = {"P_UserName": "李四", "P_Age": 20, "P_Email": "Li4@example.com"}
    # user3 = {"P_UserName": "王五", "P_Age": 18, "P_Email": "Wang5@example.com"}
    # (successed,msg)=mssql.insert("Dt_User",user1)
    # (successed,msg)=mssql.insert("Dt_User",user2)
    # (successed,msg)=mssql.insert("Dt_User",user3)
    # print(successed,msg)

    # Demo6:Update
    # updateData = {"P_Age": 31,"P_Email":"Zhang3@QQ.com"}
    # (successed,msg)=mssql.update("Dt_User", updateData, "P_UserName = ?",('张三',))
    # print(successed,msg)

    # Demo7:Delete
    # (successed,msg)=mssql.delete("Dt_User", "P_UserName = ?", ("王五",))
    # print(successed,msg)

    # Demo8:Select
    # cols=("P_UserName","P_Age")
    # cols=None
    # (successed,msg,data)=mssql.select("Dt_User",cols,"P_UserName = ?",("张三",))
    # print(successed,msg,data)

    # 提交事务
    mssql.commit()
    # 关闭
    mssql.close()

🌺myconfig模块

通过configparser读取配置文件。

使用示例:

from pylhb import MyConfig

if __name__ == "__main__":
    config = MyConfig("config.ini")
    config.set("main", "host", "127.0.0.1")
    print(config.get("main", "host"))

🌺mysqlite模块

通过sqlite3访问SQLite数据库。

使用示例:

from pylhb import MySQLite
if __name__ == "__main__":
    # 创建数据库实例
    db = MySQLite("test.db")

    # 连接数据库
    db.connect()

    # 创建表
    columns = {
        "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
        "name": "TEXT NOT NULL",
        "age": "INTEGER",
        "email": "TEXT"
    }
    db.createTable("users", columns)

    # 插入数据
    user1 = {"name": "张三", "age": 25, "email": "zhangsan@example.com"}
    user2 = {"name": "李四", "age": 30, "email": "lisi@example.com"}
    user3 = {"name": "王五", "age": 28, "email": "wangwu@example.com"}

    db.insert("users", user1)
    db.insert("users", user2)
    db.insert("users", user3)

    # 查询所有数据
    print("所有用户:")
    users = db.select("users")
    for user in users:
        print(user)

    # 条件查询
    print("\n年龄大于28的用户:")
    users = db.select("users", where="age > ?", params=(28,))
    for user in users:
        print(user)

    # 判断是否存在
    ok, exists = db.exist("users", "id = ?", (1,))
    print(ok,exists)

    # 更新数据
    update_data = {"age": 31}
    db.update("users", update_data, "name = ?", ("李四",))

    # 查询特定列
    print("\n用户姓名和邮箱:")
    users = db.select("users", columns=["name", "email"])
    for user in users:
        print(user)

    # 删除数据
    db.delete("users", "name = ?", ("王五",))

    # 再次查询所有数据
    print("\n删除后的所有用户:")
    users = db.select("users")
    for user in users:
        print(user)

    # 关闭连接
    db.close()

🌺mylog模块

本地日志操作。

使用示例:

from pylhb import MyLog

if __name__ == "__main__":
    myLog= MyLog()
    myLog.add("This is a test log entry.")

🌺mysqlite模块

SQLite操作

使用示例:

from pylhb import SQLite

if __name__ == "__main__":
    # 创建数据库实例
    db = SQLite("test.db")

    # 连接数据库
    db.connect()

    # 创建表
    columns = {
        "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
        "name": "TEXT NOT NULL",
        "age": "INTEGER",
        "email": "TEXT"
    }
    db.createTable("users", columns)

    # 插入数据
    user1 = {"name": "张三", "age": 25, "email": "zhangsan@example.com"}
    user2 = {"name": "李四", "age": 30, "email": "lisi@example.com"}
    user3 = {"name": "王五", "age": 28, "email": "wangwu@example.com"}

    db.insert("users", user1)
    db.insert("users", user2)
    db.insert("users", user3)

    # 查询所有数据
    print("所有用户:")
    users = db.select("users")
    for user in users:
        print(user)

    # 条件查询
    print("\n年龄大于28的用户:")
    users = db.select("users", where="age > ?", params=(28,))
    for user in users:
        print(user)

    # 更新数据
    update_data = {"age": 31}
    db.update("users", update_data, "name = ?", ("李四",))

    # 查询特定列
    print("\n用户姓名和邮箱:")
    users = db.select("users", columns=["name", "email"])
    for user in users:
        print(user)

    # 删除数据
    db.delete("users", "name = ?", ("王五",))

    # 再次查询所有数据
    print("\n删除后的所有用户:")
    users = db.select("users")
    for user in users:
        print(user)

    # 关闭连接
    db.close()

🌺mycipher模块

加解密

使用示例:

from pylhb import MyAsymClipher,MyXORCipher,MyFernetCipher,MyBlowfishCipher

if __name__ == "__main__":
  # 非对称加密
  ac=MyAsymClipher()
  print(ac.md5("Mr.Lee"))
  print(ac.sha256("Mr.Lee"))
  # XOR对称加解密
  xor=MyXORCipher(key)
  xor.encrypt("") # 加密
  xor.decrypt("") # 解密
  # Fernet对称加解密
  ft=MyFernetCipher(key)
  ft.encrypt("") # 加密
  ft.decrypt("") # 解密
  # Blowfish对称加解密
  bf=MyBlowfishCipher(key)
  bf.encrypt("") # 加密
  bf.decrypt("") # 解密

🌺mysnowflake模块

雪花ID

使用示例:

from pylhb import MySnowflake

if __name__ == "__main__":
    workerID = 1 # 机器ID
    datacenterID = 1 # 数据中心ID
    snowflake = MySnowflake(workerID, datacenterID)
    # 生成10个ID
    for _ in range(10):
        print(snowflake.generateID())

🌺mydevice模块

设备管理

使用示例:

from pylhb import MyDevice

if __name__ == "__main__":
  d=MyDevice()
  print(d.getHostName())
  print(d.getDeviceID())
  print(d.getMAC())
  print(d.getLocalIP())

🌺myspinner模块

进度条

使用示例:

import time
from pylhb import SpinnerStyle,MySpinner

if __name__ == "__main__":
    # 盲文风格(最漂亮)
    spinner = MySpinner(SpinnerStyle.BRAILLE, "初始化任务...")
    spinner.start()
    time.sleep(1.5)
    spinner.setText("正在下载资源...")
    time.sleep(2)
    spinner.setText("解析数据中...")
    time.sleep(2)
    spinner.setText("生成结果...")
    time.sleep(1.5)
    spinner.stop("任务全部成功完成!")

    # 中断测试
    spinner2 = MySpinner(SpinnerStyle.CLASSIC, "准备测试...")
    spinner2.start()
    time.sleep(1)
    spinner2.setText("即将中断...")
    time.sleep(1)
    spinner2.stop("手动终止成功",showIcon=False)

🌺myaccess模块

Microsoft Access 数据库操作

使用示例:

from pylhb import MyAccess

if __name__ == "__main__":
    db = AccessDB(r"C:\MyDatabase.accdb", password="123456")
    db.connect()
    # 查询
    rows = db.select("SELECT * FROM Employees WHERE Age > ?", (30,))

    db.useTransaction()
    # 插入数据
    db.insert("Employees", {
            "Name": "张三",
            "Age": 28,
            "Department": "研发部",
            "Salary": 8500.00
        })

    # 更新数据
    db.update("Employees", {"Department": "IT"}, "Department = ?", ("Support",))

    # 删除数据
    db.delete("Employees", "EmployeeID = ?", (999,))

    db.commit()
    db.close()

🌺myqrcode模块

二维码生成

使用示例:

from pylhb import MyQRCode

if __name__ == "__main__":
    demo = 'https://weixin.qq.com/'
    qr = MyQRCode(demo, errorCorrection='M')
    qr.save(desc='扫一扫添加微信', fontName='微软雅黑', fontSize=48,fileName="qrcode.png")
    qr.toAscii()

🌺License

本项目基于 MIT 许可证开源。MIT 是一种宽松的开源许可证,允许任何人免费使用、修改、分发本软件,包括用于商业用途。详见 LICENSE 文件。

Download files

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

Source Distribution

pylhb-2.5.3.tar.gz (72.5 kB view details)

Uploaded Source

Built Distribution

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

pylhb-2.5.3-py3-none-any.whl (83.6 kB view details)

Uploaded Python 3

File details

Details for the file pylhb-2.5.3.tar.gz.

File metadata

  • Download URL: pylhb-2.5.3.tar.gz
  • Upload date:
  • Size: 72.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","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 pylhb-2.5.3.tar.gz
Algorithm Hash digest
SHA256 3f663c7d4c4d365a497ff72f6fca6496e7542e89c2def1517f7610fff8e8a882
MD5 3b7db1e8ad62cd2e6af661565899dbad
BLAKE2b-256 0a53e15788f5571cb541eb3e5f8cc4bc5b7b48fd0fec831aa3a1079f1058beaf

See more details on using hashes here.

File details

Details for the file pylhb-2.5.3-py3-none-any.whl.

File metadata

  • Download URL: pylhb-2.5.3-py3-none-any.whl
  • Upload date:
  • Size: 83.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","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 pylhb-2.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e23a30dfc73469be9533ec331d8af327b32eca6a671cafe21abe9f1590966fb1
MD5 312f99adc14b590105b2f7cdbf8fa2f0
BLAKE2b-256 752c940f727964e8f386a72c9db427c7a7ef9fd0d5b5ff4bfdae1682d02f0584

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.5.3 This release

2 files

2.5.2

2 files

2.5.1

2 files

2.5.0

2 files

2.4.2

2 files

2.4.0

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.9.0

2 files

1.8.8

2 files

1.8.7

2 files

1.8.6

2 files

1.8.5

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.6.9

2 files

1.6.8

2 files

1.6.6

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page