Skip to main content

lightsmile's personal Python util libs

Project description

lightUtils

本项目仅为自己的Python常用工具类。

其中有些是自己写的,有的则是网上找的,由于有些还需要copy代码到相应需求处才能使用,比如说放在gist或其他代码片段保存工具并不太方便,所以这里也构建一个库,用的时候先install然后直接import就好了。

功能

  1. 彩色日志
  2. 获取系统可用tcp端口
  3. 从文件中逐行获取json对象
  4. 发送邮件
  5. 将时间间隔(s为单位)转换为日时分秒表示
  6. 将迭代器转化为批量数据返回形式
  7. 执行系统命令并获取执行结果
  8. 检查文件是否存在以及是否对应相应拓展名
  9. 获取文件名,不含拓展名

使用

1.彩色日志

示例

from lightutils import logger

logger.info("info")
logger.warning('warning')
logger.debug('debug')
logger.error('error')

效果如图: 效果

2.获取系统可用tcp端口

示例

from lightutils import get_free_tcp_port

port = get_free_tcp_port()
print(port)

执行结果为:

49783
<class 'int'>

3.从文件中逐行获取json对象

从文件中逐行获取json对象,

示例

from lightutils import read_json_line

for obj in read_json_line('test.json'):
    print(obj)

执行结果为:

{'info': {'word': '丘为', 'means': [['寻西山隐者不遇', '寻西山隐者不遇'], ['左掖梨花', '左掖梨花']]}, 'type': 'ambiguous'}
{'info': {'word': '丘为', 'means': [['寻西山隐者不遇', '寻西山隐者不遇'], ['左掖梨花', '左掖梨花']]}, 'type': 'ambiguous'}

原文件内容为:

{"info": {"word": "丘为", "means": [["寻西山隐者不遇", "寻西山隐者不遇"], ["左掖梨花", "左掖梨花"]]}, "type": "ambiguous"}
{asdf}
{"info": {"word": "丘为", "means": [["寻西山隐者不遇", "寻西山隐者不遇"], ["左掖梨花", "左掖梨花"]]}, "type": "ambiguous"}

错误输出日志error.log内容如下:

line2: {asdf}

4.发送邮件

使用Python发送一封邮件

示例

from lightutils import send_email_notification

to = "iamlightsmile@qq.com"
subject = "just a test"
contents = ["the test of lightUtils's send_email_notification function"]
result = send_email_notification(to, subject, contents)
if result:
    print("发送成功!")
else:
    print("发送失败!")

运行结果:

发送成功!

效果截图:

UTOOLS1580802152439.png

5.将时间间隔(s为单位)转换为日时分秒表示

使用示例

from lightutils import time_convert

print(time_convert(10000000.234))
print(time_convert(1000000.0))
print(time_convert(100000.0))
print(time_convert(10000.0))
print(time_convert(1000.0))
print(time_convert(100.0))
print(time_convert(10.0))
print(time_convert(1.0))
print(time_convert(0.0))

运行结果:

115天17小时46分钟40.23秒
11天13小时46分钟40.0秒
1天3小时46分钟40.0秒
2小时46分钟40.0秒
16分钟40.0秒
1分钟40.0秒
10.0秒
1.0秒
0.0秒

6.将迭代器转化为批量数据返回形式

使用示例

from lightutils import batch

if __name__ == '__main__':
    for item in batch(range(100)):
        print(item)

运行结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

7.执行系统命令并获取执行结果

使用示例

from lightutils import execute_cmd

if __name__ == '__main__':
    state, info = execute_cmd('ls')
    if state:
        print(info)

运行结果:

chunk_demo.py
cmd_demo.py
error.log
json_file_demo.py
log_demo.py
port_demo.py
send_email_demo.py
test.json
time_convert_demo.py

8.检查文件是否存在以及是否对应相应拓展名

使用示例

from lightutils import check_file

if __name__ == '__main__':
    # check_file('fuck.txt', 'txt')
    check_file('file_demo.py', 'txt')
    # check_file('file_demo.py', 'py')

运行效果

Traceback (most recent call last):
  File "E:/Projects/myProjects/lightUtils/examples/file_demo.py", line 10, in <module>
    check_file('file_demo.py', 'txt')
  File "E:\Projects\myProjects\lightUtils\lightutils\common\file.py", line 25, in check_file
    raise NotSpecifiedFileException('xx.' + ext)
lightutils.common.file.NotSpecifiedFileException: 并非指定的文件错误,应该是形如:xx.txt

9.获取文件名,不含拓展名

使用示例

from lightutils import get_file_name

if __name__ == '__main__':
    print(get_file_name("hello_world.py"))

运行效果

hello_world

参考

  1. lightless233/colorlog: Python彩色log模块封装
  2. Getting a random free tcp port in python using sockets
  3. kootenpv/yagmail: Send email in Python conveniently for gmail using yagmail
  4. Python最良心的邮件发送库--yagmail_Detector_的博客-CSDN博客
  5. Python用QQ邮箱发送邮件时授权码问题_wateryouyo的博客-CSDN博客
  6. python - how to split an iterable in constant-size chunks - Stack Overflow
  7. python获取系统信息模块详解 - 编程语言 - 亿速云
  8. Python中调用命令行的几种方法 - 简书

打赏

如果该项目对您有所帮助,欢迎打赏~

UTOOLS1578660899400.jpg

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

lightUtils-0.1.9.2.tar.gz (8.1 kB view hashes)

Uploaded Source

Built Distribution

lightUtils-0.1.9.2-py3-none-any.whl (11.2 kB view hashes)

Uploaded Python 3

Supported by

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