成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

七個好用的裝飾器

開發 后端
本文主要分享七個好用的裝飾器,方便你擼代碼。一起來看看吧。

1、dispach

Python 天然支持多態,但使用 dispatch 可以讓你的代碼更加容易閱讀。

安裝:

pip install multipledispatch

使用:

>>> from multipledispatch import dispatch
>>> @dispatch(int, int)
... def add(x, y):
... return x + y
>>> @dispatch(object, object)
... def add(x, y):
... return "%s + %s" % (x, y)
>>> add(1, 2)
3
>>> add(1, 'hello')
'1 + hello'

2、click

click 可以很方便地讓你實現命令行工具。

安裝:

pip install click

使用:demo2.py :

import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()

運行結果:

? python demo2.py --count=3 --name=joih
Hello joih!
Hello joih!
Hello joih!
? python demo2.py --count=3
Your name: somenzz
Hello somenzz!
Hello somenzz!
Hello somenzz!

3、celery

分布式的任務隊列,非 Celery 莫屬。

from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def add(x, y):
return x + y

4、deprecated

這個相信大家在使用別的包時都遇到過,當要下線一個老版本的函數的時候就可以使用這個裝飾器。

安裝:

pip install Deprecated

使用:demo4.py

from deprecated import deprecated
@deprecated ("This function is deprecated, please do not use it")
def func1():
pass
func1()

運行效果如下:

? python demo4.py
demo4.py:6: DeprecationWarning: Call to deprecated function (or staticmethod) func1. (This function is deprecated, please do not use it)
func1()

5、deco.concurrent

安裝:

pip install deco

使用 DECO 就像在 Python 程序中查找或創建兩個函數一樣簡單。我們可以用 @concurrent 裝飾需要并行運行的函數,用 @synchronized 裝飾調用并行函數的函數,使用舉例:

from deco import concurrent, synchronized  
@concurrent # We add this for the concurrent function
def process_url(url, data):
#Does some work which takes a while
return result
@synchronized # And we add this for the function which calls the concurrent function
def process_data_set(data):
results = {}
for url in urls:
results[url] = process_url(url, data)
return results

6、cachetools

緩存工具

安裝:

pip install cachetools

使用:

from cachetools import cached, LRUCache, TTLCache
# speed up calculating Fibonacci numbers with dynamic programming
@cached(cache={})
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
# cache least recently used Python Enhancement Proposals
@cached(cache=LRUCache(maxsize=32))
def get_pep(num):
url = 'http://www.python.org/dev/peps/pep-%04d/' % num
with urllib.request.urlopen(url) as s:
return s.read()
# cache weather data for no longer than ten minutes
@cached(cache=TTLCache(maxsize=1024, ttl=600))
def get_weather(place):
return owm.weather_at_place(place).get_weather()

 7、retry

重試裝飾器,支持各種各樣的重試需求。

安裝:

pip install tenacity

使用:

import random
from tenacity import retry
@retry
def do_something_unreliable():
if random.randint(0, 10) > 1:
raise IOError("Broken sauce, everything is hosed!!!111one")
else:
return "Awesome sauce!"
@retry(stop=stop_after_attempt(7))
def stop_after_7_attempts():
print("Stopping after 7 attempts")
raise Exception
@retry(stop=stop_after_delay(10))
def stop_after_10_s():
print("Stopping after 10 seconds")
raise Exception
@retry(stop=(stop_after_delay(10) | stop_after_attempt(5)))
def stop_after_10_s_or_5_retries():
print("Stopping after 10 seconds or 5 retries")
raise Exception
責任編輯:龐桂玉 來源: Python編程時光
相關推薦

2025-04-14 08:35:00

Python類裝飾器裝飾器

2022-06-30 15:12:48

數據分析工具大數據

2024-05-10 12:33:06

flask裝飾器

2023-09-04 15:58:34

服務器數據中心

2015-06-11 13:34:54

編程編程階段

2021-11-17 15:28:06

LinuxLinux命令

2022-09-21 11:47:15

CIO虛假敏捷

2024-11-06 14:26:40

2022-11-18 14:33:39

2018-05-17 13:59:28

IT顧問

2023-07-14 14:53:38

人工智能prompt

2011-03-02 09:34:58

AppFuse

2010-02-02 11:49:03

刀片服務器

2010-09-10 12:07:32

重點網絡協議

2010-11-09 10:28:50

簡歷

2014-01-21 08:56:10

人物管理

2014-03-12 15:23:20

2022-08-02 20:22:01

SaaS安全網絡攻擊

2022-08-11 11:43:01

CISO首席信息安全官

2023-06-04 17:13:26

.NET開發應用程序
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲一区二区三区在线播放 | 国产欧美一区二区三区久久人妖 | 国产免费一区二区三区 | 亚洲精品av在线 | 欧洲亚洲视频 | 免费观看色 | 999精品视频在线观看 | 五月天激情综合网 | 一区二区三区视频免费看 | 91国语清晰打电话对白 | 一级特黄a大片 | 日本免费小视频 | 日韩精品一区二区三区视频播放 | 最新国产精品视频 | 中文字幕日韩欧美一区二区三区 | 请别相信他免费喜剧电影在线观看 | 亚洲精品久久久一区二区三区 | 欧美日韩成人影院 | 黄色视频a级毛片 | 国产分类视频 | 一区二区免费在线观看 | 91久久久久久久久 | 久草在线青青草 | 国产精品99久久久久久宅男 | 91操操操 | 久久99国产精品 | 久久亚洲视频网 | 国产一区二区三区 | 亚洲精品成人 | 成人超碰| 在线视频三区 | 久久精品91久久久久久再现 | 国产激情免费视频 | 国产成人99久久亚洲综合精品 | 黄色大片免费播放 | 欧美涩涩网 | 羞羞色视频 | 精品美女视频在免费观看 | 成人激情视频在线播放 | 亚洲精品中文字幕av | 久久国产欧美日韩精品 |