Factory function to create a decorator that applies one or more labels. Parameters ---------- label : string or sequence One or more labels that will be applied by the decorator to the functions it decorates. Labels are attributes of the decorated function with their va
(label, ds=None)
| 83 | |
| 84 | |
| 85 | def make_label_dec(label, ds=None): |
| 86 | """Factory function to create a decorator that applies one or more labels. |
| 87 | |
| 88 | Parameters |
| 89 | ---------- |
| 90 | label : string or sequence |
| 91 | One or more labels that will be applied by the decorator to the functions |
| 92 | it decorates. Labels are attributes of the decorated function with their |
| 93 | value set to True. |
| 94 | |
| 95 | ds : string |
| 96 | An optional docstring for the resulting decorator. If not given, a |
| 97 | default docstring is auto-generated. |
| 98 | |
| 99 | Returns |
| 100 | ------- |
| 101 | A decorator. |
| 102 | |
| 103 | Examples |
| 104 | -------- |
| 105 | |
| 106 | A simple labeling decorator: |
| 107 | |
| 108 | >>> slow = make_label_dec('slow') |
| 109 | >>> slow.__doc__ |
| 110 | "Labels a test as 'slow'." |
| 111 | |
| 112 | And one that uses multiple labels and a custom docstring: |
| 113 | |
| 114 | >>> rare = make_label_dec(['slow','hard'], |
| 115 | ... "Mix labels 'slow' and 'hard' for rare tests.") |
| 116 | >>> rare.__doc__ |
| 117 | "Mix labels 'slow' and 'hard' for rare tests." |
| 118 | |
| 119 | Now, let's test using this one: |
| 120 | >>> @rare |
| 121 | ... def f(): pass |
| 122 | ... |
| 123 | >>> |
| 124 | >>> f.slow |
| 125 | True |
| 126 | >>> f.hard |
| 127 | True |
| 128 | """ |
| 129 | |
| 130 | warnings.warn("The function `make_label_dec` is deprecated since IPython 4.0", |
| 131 | DeprecationWarning, stacklevel=2) |
| 132 | if isinstance(label, str): |
| 133 | labels = [label] |
| 134 | else: |
| 135 | labels = label |
| 136 | |
| 137 | # Validate that the given label(s) are OK for use in setattr() by doing a |
| 138 | # dry run on a dummy function. |
| 139 | tmp = lambda : None |
| 140 | for label in labels: |
| 141 | setattr(tmp,label,True) |
| 142 |