Created
June 25, 2013 06:22
-
-
Save anonymous/5856364 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Added by Chaobin Tang <[email protected]> | |
''' | |
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