Conditions | 8 |
Total Lines | 65 |
Lines | 65 |
Ratio | 100 % |
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 hamcrest.core.base_matcher import BaseMatcher |
||
90 | View Code Duplication | def has_properties(*keys_valuematchers, **kv_args): |
|
|
|||
91 | """Matches if an object has properties satisfying all of a dictionary |
||
92 | of string property names and corresponding value matchers. |
||
93 | |||
94 | :param matcher_dict: A dictionary mapping keys to associated value matchers, |
||
95 | or to expected values for |
||
96 | :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
97 | |||
98 | Note that the keys must be actual keys, not matchers. Any value argument |
||
99 | that is not a matcher is implicitly wrapped in an |
||
100 | :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for |
||
101 | equality. |
||
102 | |||
103 | Examples:: |
||
104 | |||
105 | has_properties({'foo':equal_to(1), 'bar':equal_to(2)}) |
||
106 | has_properties({'foo':1, 'bar':2}) |
||
107 | |||
108 | ``has_properties`` also accepts a list of keyword arguments: |
||
109 | |||
110 | .. function:: has_properties(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]]) |
||
111 | |||
112 | :param keyword1: A keyword to look up. |
||
113 | :param valueMatcher1: The matcher to satisfy for the value, or an expected |
||
114 | value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
115 | |||
116 | Examples:: |
||
117 | |||
118 | has_properties(foo=equal_to(1), bar=equal_to(2)) |
||
119 | has_properties(foo=1, bar=2) |
||
120 | |||
121 | Finally, ``has_properties`` also accepts a list of alternating keys and their |
||
122 | value matchers: |
||
123 | |||
124 | .. function:: has_properties(key1, value_matcher1[, ...]) |
||
125 | |||
126 | :param key1: A key (not a matcher) to look up. |
||
127 | :param valueMatcher1: The matcher to satisfy for the value, or an expected |
||
128 | value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
129 | |||
130 | Examples:: |
||
131 | |||
132 | has_properties('foo', equal_to(1), 'bar', equal_to(2)) |
||
133 | has_properties('foo', 1, 'bar', 2) |
||
134 | |||
135 | """ |
||
136 | if len(keys_valuematchers) == 1: |
||
137 | try: |
||
138 | base_dict = keys_valuematchers[0].copy() |
||
139 | for key in base_dict: |
||
140 | base_dict[key] = wrap_shortcut(base_dict[key]) |
||
141 | except AttributeError: |
||
142 | raise ValueError('single-argument calls to has_properties must pass a dict as the argument') |
||
143 | else: |
||
144 | if len(keys_valuematchers) % 2: |
||
145 | raise ValueError('has_properties requires key-value pairs') |
||
146 | base_dict = {} |
||
147 | for index in range(int(len(keys_valuematchers) / 2)): |
||
148 | base_dict[keys_valuematchers[2 * index]] = wrap_shortcut(keys_valuematchers[2 * index + 1]) |
||
149 | |||
150 | for key, value in kv_args.items(): |
||
151 | base_dict[key] = wrap_shortcut(value) |
||
152 | |||
153 | return all_of(*[has_property(property_name, property_value_matcher) for \ |
||
154 | property_name, property_value_matcher in base_dict.items()]) |
||
155 |