Code Monkey home page Code Monkey logo

retry.py's Introduction

Hi there ๐Ÿ‘‹

Hello, my name is urain39!

I am a developer who like to writing programs in Python, JavaScript, and C-Like Languages :)

retry.py's People

Watchers

 avatar  avatar  avatar

retry.py's Issues

Bug ref value error

from functools import wraps

def retry(errors, max_count=3, callback=None, is_method=False):
  """
  @param errors: any class which based on `Exception`
  @param max_count: optional, the max retry count
  @param callback: optional, be called with `retry count` before retry
  @param is_method: optional, useful when you want to wrap a method
  Examples:
    @retry(ZeroDivisionError, 3,
           lambda cnt, err: print(cnt, err))
    def _():
      0/0
    _()
  """

  # count = 0
  count = [0]
  print("(outer)count[0] = {0}".format(count[0]))
  # pylint: disable=invalid-name
  def fn_wrapper(fn):
    @wraps(fn)
    def wrapped_fn(*args, **kwargs):
      result = None

      if is_method:
        self = args[0]

      while True:
        try:
          count[0] += 1
          result = fn(*args, **kwargs)
          break
        # pylint: disable=invalid-name
        except errors as err:
          if count[0] <= max_count:
            if callable(callback):
              if is_method:
                callback(self, count[0], err)
              else:  # function
                print("(inner)count[0] = {0}".format(count[0]))
                callback(count[0], err)

            continue
          else:
            raise err

      return result
    return wrapped_fn
  return fn_wrapper

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.