Conditions | 7 |
Total Lines | 64 |
Lines | 64 |
Ratio | 100 % |
Changes | 1 | ||
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 |
||
70 | View Code Duplication | def has_entries(*keys_valuematchers, **kv_args): |
|
|
|||
71 | """Matches if dictionary contains entries satisfying a dictionary of keys |
||
72 | and corresponding value matchers. |
||
73 | |||
74 | :param matcher_dict: A dictionary mapping keys to associated value matchers, |
||
75 | or to expected values for |
||
76 | :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
77 | |||
78 | Note that the keys must be actual keys, not matchers. Any value argument |
||
79 | that is not a matcher is implicitly wrapped in an |
||
80 | :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for |
||
81 | equality. |
||
82 | |||
83 | Examples:: |
||
84 | |||
85 | has_entries({'foo':equal_to(1), 'bar':equal_to(2)}) |
||
86 | has_entries({'foo':1, 'bar':2}) |
||
87 | |||
88 | ``has_entries`` also accepts a list of keyword arguments: |
||
89 | |||
90 | .. function:: has_entries(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]]) |
||
91 | |||
92 | :param keyword1: A keyword to look up. |
||
93 | :param valueMatcher1: The matcher to satisfy for the value, or an expected |
||
94 | value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
95 | |||
96 | Examples:: |
||
97 | |||
98 | has_entries(foo=equal_to(1), bar=equal_to(2)) |
||
99 | has_entries(foo=1, bar=2) |
||
100 | |||
101 | Finally, ``has_entries`` also accepts a list of alternating keys and their |
||
102 | value matchers: |
||
103 | |||
104 | .. function:: has_entries(key1, value_matcher1[, ...]) |
||
105 | |||
106 | :param key1: A key (not a matcher) to look up. |
||
107 | :param valueMatcher1: The matcher to satisfy for the value, or an expected |
||
108 | value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
||
109 | |||
110 | Examples:: |
||
111 | |||
112 | has_entries('foo', equal_to(1), 'bar', equal_to(2)) |
||
113 | has_entries('foo', 1, 'bar', 2) |
||
114 | |||
115 | """ |
||
116 | if len(keys_valuematchers) == 1: |
||
117 | try: |
||
118 | base_dict = keys_valuematchers[0].copy() |
||
119 | for key in base_dict: |
||
120 | base_dict[key] = wrap_matcher(base_dict[key]) |
||
121 | except AttributeError: |
||
122 | raise ValueError('single-argument calls to has_entries must pass a dict as the argument') |
||
123 | else: |
||
124 | if len(keys_valuematchers) % 2: |
||
125 | raise ValueError('has_entries requires key-value pairs') |
||
126 | base_dict = {} |
||
127 | for index in range(int(len(keys_valuematchers) / 2)): |
||
128 | base_dict[keys_valuematchers[2 * index]] = wrap_matcher(keys_valuematchers[2 * index + 1]) |
||
129 | |||
130 | for key, value in kv_args.items(): |
||
131 | base_dict[key] = wrap_matcher(value) |
||
132 | |||
133 | return IsDictContainingEntries(base_dict) |
||
134 |