Skip to content

Instantly share code, notes, and snippets.

@jdevera
Created October 17, 2011 10:52
Show Gist options
  • Save jdevera/1292386 to your computer and use it in GitHub Desktop.
Save jdevera/1292386 to your computer and use it in GitHub Desktop.
Python Testcase printing decorator
def print_doc(f):
"""
Prints the class and method name, followed by the first paragraph of the docstring.
This is intended to be used as a decorator
"""
def get_first_paragrap_in_single_line(doc):
lines = [ l.strip() for l in doc.strip().split('\n') ]
if '' in lines:
lines = lines[:lines.index('')]
return " ".join(lines)
def g(*args,**kwargs):
classname = args[0].__class__.__name__
methodname = f.__name__
first_doc_line = ""
if f.__doc__ is not None:
first_doc_line = get_first_paragrap_in_single_line(f.__doc__)
print "\n%s.%s: %s" % (classname, methodname, first_doc_line)
return f(*args,**kwargs)
g.__name__ = f.__name__
g.__doc__ = f.__doc__
if hasattr(g,'__annotations__'):
g.__annotations__ = f.__annotations__
return g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment