七月婷婷在线视频综合-奇米77-奇米狠狠干-奇米久草-国内精品久久影视免费-国内精品免费视频自在线

您現(xiàn)在所在的位置:首頁 >關(guān)于奇酷 > 行業(yè)動態(tài) > 七個好用的裝飾器

七個好用的裝飾器

來源:奇酷教育 發(fā)表于:

七個好用的裝飾器

  七個好用的裝飾器:

 
  1、dispach
  Python 天然支持多態(tài),但使用 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 可以很方便地讓你實現(xiàn)命令行工具。
 
  安裝:
 
  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()
  運(yùn)行結(jié)果:
 
  ? 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
  分布式的任務(wù)隊列,非 Celery 莫屬。
 
  Celery 介紹和詳細(xì)用法,可以參考前文:
 
  開發(fā)環(huán)境下,如何通過一個命令讓 fastapi 和 celery 一起工作
 
  一文搞定 celery 任務(wù)遠(yuǎn)程調(diào)用
 
  from celery import Celery
 
  app = Celery('tasks', broker='pyamqp://guest@localhost//')
 
  @app.task
  def add(x, y):
      return x + y
 
  4、deprecated
  這個相信大家在使用別的包時都遇到過,當(dāng)要下線一個老版本的函數(shù)的時候就可以使用這個裝飾器。
 
  安裝:
 
  pip install Deprecated
  使用:demo4.py
 
  from deprecated import deprecated
  @deprecated ("This function is deprecated, please do not use it")
  def func1():
      pass
 
  func1()
  運(yùn)行效果如下:
 
  ? 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 程序中查找或創(chuàng)建兩個函數(shù)一樣簡單。我們可以用 @concurrent 裝飾需要并行運(yùn)行的函數(shù),用 @synchronized 裝飾調(diào)用并行函數(shù)的函數(shù),使用舉例:
 
  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
 
主站蜘蛛池模板: 免费黄色欧美 | 日本三级香港三级人妇三级 | 美女视频很黄很a免费国产 美女涩涩网站 | 最近最新中文字幕免费高清1 | 久久不卡免费视频 | 日批视频在线免费观看 | 黄色短视频在线免费观看 | 日本精品视频一区二区三区 | 国产成人精品久久 | yy4138殇情理论片一级毛片 | 亚洲一区免费观看 | 一级毛片人与动免费观看 | 狠狠插影院 | 色视频免费观看高清完整 | 久久生活片| 麻豆回家视频区一区二 | 日韩伦理视频 | 国产看片网站 | 国产aⅴ片| 性欧美xxxx视频在线观看 | 成人私人影院在线观看网址 | 韩国理伦伦片在线观看 | 国产一区二区三区日韩 | 日韩欧美亚洲中字幕在线播放 | 激情综合色五月丁香六月亚洲 | 亚洲成人动漫在线 | 一级特级全黄 | 69中国xxxxxxxx18 | a级人体片免费观看网站 | 成成人看片在线 | 亚洲片在线 | www.日韩三级 | 黄视频在线观看www免费 | 最新欧美精品一区二区三区不卡 | 老司机午夜影院 | 日本三级午夜 | 国产成人啪精品视频免费网站软件 | 亚洲黄色影视 | 又大又粗又爽的三级小视频 | 欧美jizz18性欧美 | 久草免费在线播放 |