Metadata-Version: 2.1
Name: SmarTool
Version: 1.0.5
Summary: A set of tools that keep Python sweeter.
Home-page: https://github.com/Harpsichord1207/SmarTool
Author: Harpsichord
Author-email: tliu1217@163.com
License: MPL 2.0
Keywords: Python Util Tool Convenience
Platform: any
Classifier: Programming Language :: Python :: 3.8
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
Classifier: Intended Audience :: Developers
Description-Content-Type: text/markdown
License-File: LICENSE

# SmarTool - Smart Util Tool for Python

## 简介

SmarTool是一个小而全的Python工具类库，类似Java的[Hutool][1]。

## 安装

`pip install SmarTool`

## 使用

### 1. Retry - 重试工具

```python
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 - 超时工具

```python
import time
from SmarTool import timeout

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

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

[1]: https://github.com/dromara/hutool


