Commit 1bb8731f authored by steinbac's avatar steinbac

added material on decorators

parent 70372b62
......@@ -12,9 +12,9 @@ def main():
help='summarize the integers (default: find the max)')
args = parser.parse_args()
myv = geom_vector(args.integers)
my_summary_metric = geom_vector(args.integers)
print(myv.norm(args.summarize))
print(my_summary_metric.norm(args.summarize))
if __name__ == '__main__':
......
def greet(name):
return "hello "+name
def main():
greet_someone = greet
print(greet_someone("John and Mary"))
if __name__ == '__main__':
main()
def greet(name):
def get_message():
return "Hello "
return get_message()+name
def main():
print(greet("John and Mary"))
if __name__ == '__main__':
main()
def greet(name):
return "Hello "+name
def call_func(func):
other_name = "John"
return func(other_name)
def main():
print(call_func(greet))
if __name__ == '__main__':
main()
def compose_greet_function():
def get_message():
return "Hello there!"
return get_message
def main():
greet = compose_greet_function()
print(greet())
if __name__ == '__main__':
main()
def compose_greet_function(name):
my_end = "."
def get_message():
value = "Hello "+name+my_end
return value
return get_message
def main():
greet = compose_greet_function("John and Mary")
print(greet())
if __name__ == '__main__':
main()
def get_text(name):
return "today's hacky hour is about {0}".format(name)
def decorate_with_p(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def main():
my_text = decorate_with_p(get_text)
print(my_text("decorators in python"))
if __name__ == '__main__':
main()
def get_text(name):
return "today's hacky hour is about {0}".format(name)
def decorate_with_p(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def main():
my_get_text = decorate_with_p(get_text)
print(my_get_text("decorators in python"))
if __name__ == '__main__':
main()
def decorate_with_p(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
@decorate_with_p
def get_text(name):
return "today's hacky hour is about {0}".format(name)
def main():
print(get_text("decorators in python"))
if __name__ == '__main__':
main()
def decorate_with_p(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def decorate_with_strong(func):
def func_wrapper(name):
return "<strong>{0}</strong>".format(func(name))
return func_wrapper
def decorate_with_div(func):
def func_wrapper(name):
return "<div>{0}</div>".format(func(name))
return func_wrapper
@decorate_with_p
@decorate_with_div
@decorate_with_strong
def get_text(name):
""" test me """
return "today's hacky hour is about {0}".format(name)
def main():
print(get_text("decorators in python"))
if __name__ == '__main__':
main()
def tags(tag_name):
def tag_decorator(func):
def func_wrapper(name):
return "<{0}>{1}</{0}>".format(tag_name, func(name))
return func_wrapper
return tag_decorator
@tags("div")
def get_text(name):
return "Hello "+name
def main():
print(get_text("John and Mary!"))
if __name__ == '__main__':
main()
from functools import wraps
def tags(tag_name):
def tag_decorator(func):
@wraps(func)
def func_wrapper(name):
return "<{0}>{1}</{0}>".format(tag_name, func(name))
return func_wrapper
return tag_decorator
@tags("div")
def get_text(name):
""" a function to greet someone. Expects <name> argument as string. """
return "Hello "+name
def main():
print(get_text("John and Mary!"))
print(get_text.__doc__)
if __name__ == '__main__':
main()
# Decorators in Python
This session discussed the use and mis-use of decorators in python. The video recording of this session is available at https://indico.mpi-cbg.de/event/101/.
## Target audience
Anyone that has an idea how to write a function in python and how input parameters can be specified.
## Further Reading
The material is based on [code ship's guide to function decorators](https://www.thecodeship.com/patterns/guide-to-python-function-decorators/).
## Attendees: 4
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment