| Conditions | 9 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from inspect import ismethod, getfullargspec |
||
| 109 | @classmethod |
||
| 110 | def from_function(cls, func, omit=frozenset()): |
||
| 111 | """ |
||
| 112 | Creates a FunctionMetadata object from a function. Please note that any |
||
| 113 | variable argument lists are not supported. If you do not want the |
||
| 114 | first (usual named 'self') argument to appear please pass the method of |
||
| 115 | an actual INSTANCE of a class; passing the method of the class isn't |
||
| 116 | enough. Alternatively you can add "self" to the omit set. |
||
| 117 | |||
| 118 | :param func: The function. If __metadata__ of the unbound function is |
||
| 119 | present it will be copied and used, otherwise it will be |
||
| 120 | generated. |
||
| 121 | :param omit: A set of parameter names that are to be ignored. |
||
| 122 | :return: The FunctionMetadata object corresponding to the given |
||
| 123 | function. |
||
| 124 | """ |
||
| 125 | if hasattr(func, "__metadata__"): |
||
| 126 | metadata = copy(func.__metadata__) |
||
| 127 | metadata.omit = omit |
||
| 128 | return metadata |
||
| 129 | |||
| 130 | doc = func.__doc__ |
||
| 131 | if doc is None: |
||
| 132 | doc = "" |
||
| 133 | doc_comment = DocumentationComment.from_docstring(doc) |
||
| 134 | |||
| 135 | non_optional_params = OrderedDict() |
||
| 136 | optional_params = OrderedDict() |
||
| 137 | |||
| 138 | argspec = getfullargspec(func) |
||
| 139 | args = argspec.args if argspec.args is not None else () |
||
| 140 | defaults = argspec.defaults if argspec.defaults is not None else () |
||
| 141 | num_non_defaults = len(args) - len(defaults) |
||
| 142 | for i, arg in enumerate(args): |
||
| 143 | # Implicit self argument or omitted explicitly |
||
| 144 | if i < 1 and ismethod(func): |
||
| 145 | continue |
||
| 146 | |||
| 147 | if i < num_non_defaults: |
||
| 148 | non_optional_params[arg] = ( |
||
| 149 | doc_comment.param_dict.get(arg, cls.str_nodesc), |
||
| 150 | argspec.annotations.get(arg, None)) |
||
| 151 | else: |
||
| 152 | optional_params[arg] = ( |
||
| 153 | doc_comment.param_dict.get(arg, cls.str_nodesc) + " (" + |
||
| 154 | cls.str_optional.format(str(defaults[i-num_non_defaults])) |
||
| 155 | + ")", |
||
| 156 | argspec.annotations.get(arg, None), |
||
| 157 | defaults[i-num_non_defaults]) |
||
| 158 | |||
| 159 | return cls(name=func.__name__, |
||
| 160 | desc=doc_comment.desc, |
||
| 161 | retval_desc=doc_comment.retval_desc, |
||
| 162 | non_optional_params=non_optional_params, |
||
| 163 | optional_params=optional_params, |
||
| 164 | omit=omit) |
||
| 165 |