| Conditions | 10 |
| Total Lines | 61 |
| 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:
Complex classes like Selector.get_parameters() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import logging |
||
| 160 | def get_parameters(self, parameter_name=None): |
||
| 161 | r"""Returns parameters from selected bricks and their descendants. |
||
| 162 | |||
| 163 | Parameters |
||
| 164 | ---------- |
||
| 165 | parameter_name : :class:`Path.ParameterName`, optional |
||
| 166 | If given, only parameters with a `name` attribute equal to |
||
| 167 | `parameter_name` are returned. |
||
| 168 | |||
| 169 | Returns |
||
| 170 | ------- |
||
| 171 | parameters : OrderedDict |
||
| 172 | A dictionary of (`path`, `parameter`) pairs, where `path` is |
||
| 173 | a string representation of the path in the brick hierarchy |
||
| 174 | to the parameter (i.e. the slash-delimited path to the brick |
||
| 175 | that owns the parameter, followed by a dot, followed by the |
||
| 176 | parameter's name), and `parameter` is the Theano variable |
||
| 177 | representing the parameter. |
||
| 178 | |||
| 179 | Examples |
||
| 180 | -------- |
||
| 181 | >>> from blocks.bricks import MLP, Tanh |
||
| 182 | >>> mlp = MLP([Tanh(), Tanh(), Tanh()], [5, 7, 11, 2]) |
||
| 183 | >>> mlp.allocate() |
||
| 184 | >>> selector = Selector([mlp]) |
||
| 185 | >>> selector.get_parameters() # doctest: +NORMALIZE_WHITESPACE |
||
| 186 | OrderedDict([('/mlp/linear_0.W', W), ('/mlp/linear_0.b', b), |
||
| 187 | ('/mlp/linear_1.W', W), ('/mlp/linear_1.b', b), |
||
| 188 | ('/mlp/linear_2.W', W), ('/mlp/linear_2.b', b)]) |
||
| 189 | |||
| 190 | Or, select just the weights of the MLP by passing the parameter |
||
| 191 | name `W`: |
||
| 192 | |||
| 193 | >>> w_select = Selector([mlp]) |
||
| 194 | >>> w_select.get_parameters('W') # doctest: +NORMALIZE_WHITESPACE |
||
| 195 | OrderedDict([('/mlp/linear_0.W', W), ('/mlp/linear_1.W', W), |
||
| 196 | ('/mlp/linear_2.W', W)]) |
||
| 197 | |||
| 198 | """ |
||
| 199 | def recursion(brick): |
||
| 200 | # TODO path logic should be separate |
||
| 201 | result = [ |
||
| 202 | (Path([Path.BrickName(brick.name), |
||
| 203 | Path.ParameterName(parameter.name)]), |
||
| 204 | parameter) |
||
| 205 | for parameter in brick.parameters |
||
| 206 | if not parameter_name or parameter.name == parameter_name] |
||
| 207 | result = OrderedDict(result) |
||
| 208 | for child in brick.children: |
||
| 209 | for path, parameter in recursion(child).items(): |
||
| 210 | new_path = Path([Path.BrickName(brick.name)]) + path |
||
| 211 | if new_path in result: |
||
| 212 | raise ValueError( |
||
| 213 | "Name collision encountered while retrieving " + |
||
| 214 | "parameters." + |
||
| 215 | name_collision_error_message.format(new_path)) |
||
| 216 | result[new_path] = parameter |
||
| 217 | return result |
||
| 218 | result = dict_union(*[recursion(brick) |
||
| 219 | for brick in self.bricks]) |
||
| 220 | return OrderedDict((str(key), value) for key, value in result.items()) |
||
| 221 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.