Conditions | 5 |
Total Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 coala_decorators.decorators import generate_eq, generate_repr |
||
54 | @classmethod |
||
55 | def multiple(cls, *args): |
||
56 | """ |
||
57 | Creates a set of multiple instances of a class. |
||
58 | |||
59 | Should not be instances of ``PackageRequirement``, as this is an |
||
60 | abstract class: |
||
61 | |||
62 | >>> PackageRequirement.multiple(('pip', 'coala_decorators', '0.1.0'),) |
||
63 | Traceback (most recent call last): |
||
64 | ... |
||
65 | NotImplementedError |
||
66 | |||
67 | It can only be used for requirements of the same manager. For example, |
||
68 | consider a manager ``XYZRequirement`` that inherits from |
||
69 | PackageRequirement. This subclass will have the manager set to XYZ: |
||
70 | |||
71 | >>> class XYZRequirement(PackageRequirement): |
||
72 | ... manager = 'xyz' |
||
73 | ... def __init__(self, package, version=""): |
||
74 | ... PackageRequirement.__init__(self, package, version) |
||
75 | |||
76 | This is the case where you would provide strings only, to specify the |
||
77 | latest version automatically: |
||
78 | |||
79 | >>> REQUIREMENTS = XYZRequirement.multiple( |
||
80 | ... "package1", "package2") |
||
81 | |||
82 | And if you choose to mix them, specifying version for some and for some |
||
83 | not: |
||
84 | |||
85 | >>> REQUIREMENTS = XYZRequirement.multiple( |
||
86 | ... 'package1', ('package2', '2.0')) |
||
87 | |||
88 | Lists are also valid arguments: |
||
89 | |||
90 | >>> REQUIREMENTS = XYZRequirement.multiple( |
||
91 | ... ['package1', '1.0'],) |
||
92 | |||
93 | In case you provide too many arguments into the tuple, an error will be |
||
94 | raised: |
||
95 | |||
96 | >>> REQUIREMENTS = XYZRequirement.multiple( |
||
97 | ... 'package1', ('package2', '2.0', 'package3')) |
||
98 | Traceback (most recent call last): |
||
99 | ... |
||
100 | TypeError: Too many elements provided. |
||
101 | |||
102 | :param args: In the subclasses, the ``manager`` is already |
||
103 | specified, so they hould be iterables with two |
||
104 | elements: ``('packageName', 'version')`` or strings: |
||
105 | ``'packageName'`` if latest version is wanted. |
||
106 | :return: A tuple containing instances of the subclass. |
||
107 | :raises TypeError: In case the iterables contain more than two |
||
108 | elements. |
||
109 | """ |
||
110 | if cls == PackageRequirement: |
||
111 | raise NotImplementedError |
||
112 | else: |
||
113 | reqs = [] |
||
114 | for requirement in args: |
||
115 | if isinstance(requirement, str): |
||
116 | reqs.append(cls(requirement)) |
||
117 | elif len(requirement) == 2: |
||
118 | name, version = requirement |
||
119 | reqs.append(cls(name, version)) |
||
120 | else: |
||
121 | raise TypeError('Too many elements provided.') |
||
122 | return set(reqs) |
||
123 | |||
139 |