97超碰碰碰_91热播_91国产免费视频_91视频合集_国产精品白丝jk白祙_国产精品久免费的黄网站

您現在所在的位置:首頁 >關于奇酷 > 行業動態 > 七個好用的裝飾器

七個好用的裝飾器

來源:奇酷教育 發表于:

七個好用的裝飾器

  七個好用的裝飾器:

 
  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 莫屬。
 
  Celery 介紹和詳細用法,可以參考前文:
 
  開發環境下,如何通過一個命令讓 fastapi 和 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
 
主站蜘蛛池模板: 久久亚洲精品成人综合 | 色一区二区 | 久久99精品久久久久久秒播放器 | 国产1区2区在线观看 | 国产 高清 在线 | 99久久久无码国产精品 | www色com | 中文字幕亚洲无线码在一区 | 三级网战 | 国产xxxx色视频在线观看14 | 免费看又黄又爽又猛的视频软件 | 中国一级做a爱片免费 | 色一情一乱一伦一区二区三区 | 中文字幕a∨在线乱码免费看 | 黄网站免费看 | 午夜视频免费 | 亚洲精品熟女国产 | 啪啪免费 | 日韩精品一区二区三区免费视频 | 一区二区三区视频在线 | 青青青手机在线视频 | 成人午夜国产内射主播 | 久久理论片 | 国产成人精品成人a在线观看 | 97精品伊人久久大香线蕉 | 久久亚洲国产中v天仙www | 国产一区在线观看视频 | 又紧又大又爽精品一区二区 | 久久综合久久香蕉网欧美 | 亚洲av无码乱码国产麻豆穿越 | 四虎免费在线 | 四虎永久在线精品影院 | 婷婷电影院 | 亚洲乱妇老熟女爽到高潮的片 | 日本一级片免费 | 色网站在线 | 国产成人综合久久精品尤物 | 国产目拍亚洲精品一区麻豆 | 国产乱人伦精品一区二区在线观看 | 成人免费一区二区三区 | 日韩精品久久无码人妻中文字幕 |