|
| 1 | +"""********************************************************************************************************************************************************** |
| 2 | +Decorators are an extremely powerful concept in Python. They allow you to modify the behavior of a function without changing the code of the function |
| 3 | +itself. This chapter will lay the foundational concepts needed to thoroughly understand decorators (functions as objects, scope, and closures), |
| 4 | +and give you a good introduction into how decorators are used and defined |
1 | 5 |
|
| 6 | +Functions are objects |
| 7 | +===================== |
| 8 | +
|
| 9 | +**********************************************************************************************************************************************************""" |
| 10 | +## Building a command line data app |
| 11 | + |
| 12 | +# Add the missing function references to the function map |
| 13 | +function_map = { |
| 14 | + 'mean': mean, |
| 15 | + 'std': std, |
| 16 | + 'minimum': minimum, |
| 17 | + 'maximum': maximum |
| 18 | +} |
| 19 | + |
| 20 | +data = load_data() |
| 21 | +print(data) |
| 22 | + |
| 23 | +func_name = get_user_input() |
| 24 | + |
| 25 | +# Call the chosen function and pass "data" as an argument |
| 26 | +function_map[func_name](data) |
| 27 | +"""!!! |
| 28 | +By adding the functions to a dictionary, you can select the function based on the user's input.""" |
| 29 | +#````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` |
| 30 | +## Reviewing your co-worker's code |
| 31 | + |
| 32 | +# Call has_docstring() on the load_and_plot_data() function |
| 33 | +ok = has_docstring(load_and_plot_data) |
| 34 | + |
| 35 | +"""if doesnt have doc string """ |
| 36 | +if not ok: |
| 37 | + print("load_and_plot_data() doesn't have a docstring!") |
| 38 | +else: |
| 39 | + print("load_and_plot_data() looks ok") |
| 40 | + |
| 41 | +# load_and_plot_data() looks ok |
| 42 | + |
| 43 | +## Reviewing your co-worker's code 2 |
| 44 | + |
| 45 | +# Call has_docstring() on the as_2D() function |
| 46 | +"""check if 2D has docstring""" |
| 47 | +ok = has_docstring(as_2D) |
| 48 | + |
| 49 | +if not ok: |
| 50 | + print("as_2D() doesn't have a docstring!") |
| 51 | +else: |
| 52 | + print("as_2D() looks ok") |
| 53 | + |
| 54 | +# as_2D() looks ok |
| 55 | + |
| 56 | +## Reviewing your co-worker's code 3 |
| 57 | + |
| 58 | +# Call has_docstring() on the log_product() function |
| 59 | +ok = has_docstring(log_product) |
| 60 | + |
| 61 | +if not ok: |
| 62 | + print("log_product() doesn't have a docstring!") |
| 63 | +else: |
| 64 | + print("log_product() looks ok") |
| 65 | + |
| 66 | +# log_product() doesn't have a docstring! |
| 67 | + |
| 68 | +"""!!! |
| 69 | +co-worker forgot to write a docstring for log_product(), |
| 70 | +To pass a function as an argument to another function, you had to determine which one you were calling and which one you were referencing.""" |
0 commit comments