Skip to main content

A set of tools that keep Python sweeter.

Project description

SmarTool - Smart Util Tool for Python

简介

SmarTool是一个小而全的Python工具类库,类似Java的Hutool

安装

pip install SmarTool

>>> import SmarTool
>>> SmarTool.hello()
===== Hello, this is SmarTool! =====
 Author: Harpsichord
 Email: tliu1217@163.com
 Install: pip install SmarTool
 Github: https://github.com/Harpsichord1207/SmarTool
====================================

使用

1. Retry - 重试工具

import random
import requests
from SmarTool import retry

# 默认重试5次,每次间隔2秒,所有异常都重试
@retry
def div1(a, b):
    return a / random.choice([0, 1, b])
div1(2, 1)

# 重试10次,每次间隔1秒,仅在出现ZeroDivisionError时重试
@retry(times=10, delay=1, catch_error=ZeroDivisionError)
def div2(a, b):
    return a / random.choice([0, 1, b])
div2(2, 1)

# 在出现TypeError时不重试直接抛出异常
@retry(ignore_error=TypeError)
def div3(a, b):
    return a / random.choice([0, 1, b])
div3(2)

# 出现指定的多个异常时重试
@retry(catch_error=[requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout])
def get():
    return requests.get("https://xxx.com").json()
get()

2. Timeout - 超时工具

import time
from SmarTool import timeout

# 默认超时时间为5秒,函数执行时间超过5秒抛出TimeoutException
@timeout
def f1():
    time.sleep(6)
f1()

# 自定义超时时间为10秒
@timeout(seconds=10)
def f2():
    time.sleep(6)
f2()

3. DTUtil - 日期工具

import datetime
from SmarTool import DTUtil

# 当前日期增加一个月
print(DTUtil.add_month().strftime("%Y-%m-%d"))  # Out: 2022-05-01

# 指定日期增加10个月
date = datetime.datetime.strptime("2022-04-01", "%Y-%m-%d")
print(DTUtil.add_month(date, months=10).strftime("%Y-%m-%d"))  # Out: 2023-02-01

# 获取某个日期当月第一天
date = datetime.datetime.strptime("2022-03-22", "%Y-%m-%d")
print(DTUtil.first_day_of_month(date).strftime("%Y-%m-%d"))  # Out: 2022-03-01

# 获取某个日期当月最后一天
print(DTUtil.last_day_of_month(date).strftime("%Y-%m-%d"))  # Out: 2022-03-31

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

SmarTool-1.0.7.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

SmarTool-1.0.7-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file SmarTool-1.0.7.tar.gz.

File metadata

  • Download URL: SmarTool-1.0.7.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for SmarTool-1.0.7.tar.gz
Algorithm Hash digest
SHA256 125888336fd39cc44b6f98efbc632883c394384fc642d89d55007d501c119e54
MD5 10ec2eaf44a93e49dd5a54e0872a1152
BLAKE2b-256 1eb824c1710f4436626959e0ad2b1f82e5f5a36f70d6c9ae4e43a7001d72f1b0

See more details on using hashes here.

File details

Details for the file SmarTool-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: SmarTool-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for SmarTool-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 81685e67cfff105d1f06089de6263f703b4ace68aa5012ffdc7bf634fc6a9cbc
MD5 d95331c012e30b817fddeab9c45c6e11
BLAKE2b-256 6ca98d444cace3efbd9ef1b7330813f1f7b5e5f359bb7c86afd61f7a04dfbb09

See more details on using hashes here.

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