| Conditions | 21 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 generate_helpstring_result() 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 25 | def generate_helpstring_result(aliases, filter_="", pack="", limit=0, offset=0): |
||
| 26 | """ |
||
| 27 | List help strings from a collection of alias objects. |
||
| 28 | |||
| 29 | :param aliases: The list of aliases |
||
| 30 | :type aliases: ``list`` of :class:`st2common.models.api.action.ActionAliasAPI` |
||
| 31 | :param filter_: A search pattern. |
||
| 32 | :type filter_: ``string`` |
||
| 33 | :param pack: Name of a pack |
||
| 34 | :type pack: ``string`` |
||
| 35 | :param limit: The number of help strings to return in the list. |
||
| 36 | :type limit: ``integer`` |
||
| 37 | :param offset: The offset in the list to start returning help strings. |
||
| 38 | :type limit: ``integer`` |
||
| 39 | |||
| 40 | :return: A list of aliases help strings. |
||
| 41 | :rtype: ``list`` of ``list`` |
||
| 42 | """ |
||
| 43 | matches = {} |
||
| 44 | count = 0 |
||
| 45 | if not (isinstance(limit, int) and isinstance(offset, int)): |
||
| 46 | raise TypeError |
||
| 47 | for alias in aliases: |
||
| 48 | # Skip disable aliases. |
||
| 49 | if not alias.enabled: |
||
| 50 | continue |
||
| 51 | # Skip packs which don't explicitly match the requested pack. |
||
| 52 | if pack != alias.pack and pack != "": |
||
| 53 | continue |
||
| 54 | for format_ in alias.formats: |
||
| 55 | display, _ = normalise_alias_format_string(format_) |
||
| 56 | if display: |
||
| 57 | # Skip help strings not containing keyword. |
||
| 58 | if not re.search(filter_, display, flags=re.IGNORECASE): |
||
| 59 | continue |
||
| 60 | # Skip over help strings not within the requested offset/limit range. |
||
| 61 | if (offset == 0 and limit > 0) and count >= limit: |
||
| 62 | count += 1 |
||
|
|
|||
| 63 | continue |
||
| 64 | elif (offset > 0 and limit == 0) and count < offset: |
||
| 65 | count += 1 |
||
| 66 | continue |
||
| 67 | elif (offset > 0 and limit > 0) and (count < offset or count >= offset + limit): |
||
| 68 | count += 1 |
||
| 69 | continue |
||
| 70 | if alias.pack not in matches: |
||
| 71 | matches[alias.pack] = [] |
||
| 72 | matches[alias.pack].append({ |
||
| 73 | "display": display, |
||
| 74 | "description": alias.description |
||
| 75 | }) |
||
| 76 | count += 1 |
||
| 77 | return {"available": count, "helpstrings": matches} |
||
| 78 |