Skip to main content

small tool

Project description

一个python工具库


安装

pip install smawe-tools

核心函数

以下函数都可以从smawe_tools包中进行导入
例如: from smawe_tools import retry

  • text_conversion(s):
    功能: 文本转换
    描述: 如果s为空, 则返回0.
    参数s: str 返回值: 整数 列如:
        两千零一 -> 2001
        十万零一百 -> 100100

  • get_ip(url, domain):
    获取目标网站url或者域名domain的ip地址
    返回包含ip地址的列表

  • get_pubnet_ip():
    获取本机的公网ip(也就是上网时所使用的ip)
    return: str

  • retry(stop_max_attempt_number=None, wait_random_min=None, wait_random_max=None, retry_on_exception=None):
    0.3.6中添加了实例方法, 类方法, 静态方法的支持
    发生异常进行重试,默认进行1次重试且每次重试前睡眠0-1s的随机时间,超过最大重试次数后还发生异常,则抛出MaxRetryError异常 重试期间如果正常返回结果或没发生异常,则不进行重试。 stop_max_attempt_number: 停止时的最大重试次数,超出次数后还发生异常,则抛出MaxRetryError异常
    wait_random_min:随机等待的最小时间(单位毫秒)
    wait_random_max: 随机等待的最大时间(单位毫秒)
    retry_on_exception: 要重试的异常类型,默认为Exception

  • modify_encoding():
    此函数要从smawe_tools.settings模块进行导入, 如:

    from smawe_tools.settings import modify_encoding 
    # 直接调用即可
    modify_encoding()  
    

    调用此函数可以修正以下此类的错误:
    UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 188608: illegal multibyte sequence


启用默认日志
默认日志级别为logging.INFO

>>> import smawe_tools.settings as settings
>>> import logging
>>> settings.ENABLED_LOG = True
>>> logging.info("test")
2023-04-04 18:10:39,183:test.py:MainThread:INFO:test

可以自行使用text_conversion函数进行扩展

示例

>>> from smawe_tools import text_conversion
>>> print(text_conversion("两千"))
2000
>>> print(text_conversion("两千万零一"))
20000001

retry使用方法
retry_exception没传参默认为Exception, 也就是发生Exception异常自动进行重试
注意事项
0.3.4中重构了retry函数, 已经支持任意传参,例如:
@retry(3, 1000, 2000, Exception)
@retry(3, wait_random_min=1000, wait_random_max=2000, retry_exception=Exception)
@retry(3, 1000, wait_random_max=2000, retry_exception=Exception)
@retry(3, 1000, 2000, retry_exception=Exception)
@retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=2000, retry_exception=Exception)

示例1

>>> from smawe_tools import retry
>>> @retry()
... def test():
...     print(1)
...     print(2)
...     raise Exception()
...
>>> test()
>>>
>>> @retry(3, 1000, 2000) # retry_exception没传参数,所以这里是Exception
... def test():
...    pass
...
>>> @retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=3000) # retry_exception没传参数,所以这里也是Exception
... def test():
...     pass
... # 如果发生异常,则进行重试,每次重试前休眠1-3s的随机时间
...
>>>

示例2

>>> from smawe_tools import retry
>>> @retry(3, 1000, 2000, ValueError)
... def test(a, b):
...     print(1)
...     print(2)
...     print(a, b)
...     raise ValueError()
...
>>> test(1, 2) #发生ValueError就进行重试
>>> @retry(3, 1000, 2000, retry_exception=IndexError)
... def test(a, b):
...     print(1)
...     print(2)
...     print(a, b)
...     raise ValueError()
...
>>> test(1, 2) #发生IndexError就进行重试

此包提供了一个smawe_tools.utils模块, 用于将日志记录发送到QQ邮箱
简单使用如下:

import logging
from smawe_tools.utils import ErrorLogger

error_logger = ErrorLogger(
    from_addr="xxxx@qq.com",
    to_addrs=["xyzxxxx@qq.com", "abcdefg@qq.com"],
    subject="python test",
    password="xxxxxxxxx", #这里是QQ授权码
    handler_level=logging.INFO,
    logger_level=logging.INFO
)

# 这里已经将日志记录发送到QQ邮箱了
error_logger.info("test message")
# 然后你的QQ邮箱会收到这样类似的消息
# test.py <module> INFO: (45)test message...

高级邮件api, 默认支持qq, 163邮箱, 可发送文本和多个附件

from smawe_tools.utils import EmailHelper

user = 'xxxx@163.com'
password = 'xxxxxx'
# file可以是路径或者一个文件对象或者文件内容
file = open('xxx.txt')
email_helper = EmailHelper(user=user, password=password)
email_helper.send_mail('This is test mail.', to=['xxxx@qq.com'], subject='python test', file=file, file_name='filename')

此包还提供了一个对配置进行读取和保存的子模块smawe_tools.config
简单使用如下:

from smawe_tools.config import Config

config = Config()
# 切换节,设置选项和值
config.switch_to_section("s1").set("k1", "v1")
config.switch_to_section("s2").set("k2", "v2")
config.switch_to_section("s3").set("k3", "v3")
# 切换到默认节"DEFAULT"
config.switch_to_section("DEFAULT").set("accept", "true")
# 保存配置
config.save_config("test.ini")

读取配置

from smawe_tools.config import Config

config = Config()
config.read_config("test.ini")
# 获取配置中的值
print(config.get("s1", "k1"))
print(config.get("s1", "kk1", fallback="vv2"))
# 获取默认节中的值, 将其转为布尔值
print(config.get_boolean("s1", "accept"))

# 将其它值转为布尔值
config.boolean_states = {"access": True}

config.set("s1", "can", "access")
print(config.switch_to_section("s1").get_boolean("can"))

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

smawe_tools-0.4.3.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

smawe_tools-0.4.3-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file smawe_tools-0.4.3.tar.gz.

File metadata

  • Download URL: smawe_tools-0.4.3.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for smawe_tools-0.4.3.tar.gz
Algorithm Hash digest
SHA256 ba9d1f2bab952f05cf05a883889e0b22a546755d39646293065c48b860861e01
MD5 e9338372fe89ca7c5a08b0c804ee734b
BLAKE2b-256 0005af60d8a1025fe2b8c56b2c47e3d92caeb9ba2870724c1ff2f58bc8bcb793

See more details on using hashes here.

File details

Details for the file smawe_tools-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: smawe_tools-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for smawe_tools-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b666fd66b3bf49593827c7fc644fcf47ead7b90ea9a5f2ad37826078fad7a2ef
MD5 51d2a7d5b3c2e7d66631d7b2d754c785
BLAKE2b-256 50b5e682ffd4beb90d14c7f6440610bbbb60641bcdf08ec234aa2df9f1c288e4

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