Skip to content

Instantly share code, notes, and snippets.

Created June 25, 2013 06:22
Show Gist options
  • Save anonymous/5856364 to your computer and use it in GitHub Desktop.
Save anonymous/5856364 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Added by Chaobin Tang <cbtchn@gmail.com>
'''
This module shows two very simple implicities
of Python
'''
import doctest
def count(a, elems=[]):
'''
>>> count(1)
1
>>> count(2)
1
>>> count(3)
1
'''
elems.append(a)
print len(elems)
def retry(func=None, exceptions=(Exception,), reruns=3):
'''
>>> def f(): a+b
>>> f = retry(exceptions=(NameError))(f)
>>> f()
'''
def decorator(func):
def wrapped(*args, **kwargs):
while reruns > 0:
try:
return func(*args, **kwargs)
except exceptions:
if reruns == 1:
raise Exception('Tried %s times' % reruns)
reruns -= 1
return wrapped
return decorator if (func is None) else decorator(func)
def main():
doctest.testmod()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment