| Conditions | 5 |
| Total Lines | 54 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 | import pytest |
||
| 25 | def tabular_interfaces_contracts(): |
||
| 26 | return { |
||
| 27 | 'retriever': { |
||
| 28 | 'column': '(identifier, data)', |
||
| 29 | 'row': '(identifier, data)', |
||
| 30 | 'nb_columns': '(data)', |
||
| 31 | 'nb_rows': '(data)', |
||
| 32 | 'get_numerical_attributes': '(data)', |
||
| 33 | }, |
||
| 34 | 'iterator': { |
||
| 35 | 'columnnames': '(data)', |
||
| 36 | 'itercolumns': '(data)', |
||
| 37 | 'iterrows': '(data)', |
||
| 38 | }, |
||
| 39 | 'mutator': { |
||
| 40 | 'add_column': '(datapoints, values, new_attribute, **kwargs)', |
||
| 41 | }, |
||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | @pytest.fixture |
||
| 46 | def member_names(): |
||
| 47 | def get_member_names(_object): |
||
| 48 | return list(x[0] for x in inspect.getmembers(_object, predicate=lambda x: any([inspect.ismethod(x), inspect.isfunction(x)]))) |
||
| 49 | return get_member_names |
||
| 50 | |||
| 51 | |||
| 52 | @pytest.fixture |
||
| 53 | def create_instances(tabular_operators): |
||
| 54 | def _create_operators(*interface_ids): |
||
| 55 | return tuple(tabular_operators[interface_id]() for interface_id in interface_ids) |
||
| 56 | return _create_operators |
||
| 57 | |||
| 58 | |||
| 59 | @pytest.fixture |
||
| 60 | def assert_correct_signatures(tabular_interfaces_contracts, member_names, tabular_operators_reverse): |
||
| 61 | def _assert_correct_signatures(instance): |
||
| 62 | interface_id = tabular_operators_reverse[type(instance)] |
||
| 63 | expected_implemented_methods_names = tabular_interfaces_contracts[interface_id].keys() |
||
| 64 | runtime_members = member_names(instance) |
||
| 65 | assert all(member in runtime_members and str(inspect.signature(getattr(instance, member))) == tabular_interfaces_contracts[interface_id][member] for member in expected_implemented_methods_names) |
||
| 66 | return _assert_correct_signatures |
||
| 67 | |||
| 68 | |||
| 69 | @pytest.fixture |
||
| 70 | def assert_correct_delegate_behaviour(tabular_interfaces_contracts, tabular_operators_reverse): |
||
| 71 | def _assert_correct_delegate_behaviour(instance1, instance2): |
||
| 72 | instance1_type = type(instance1) |
||
| 73 | assert instance1_type == type(instance2) |
||
| 74 | assert id(instance1._delegate) != id(instance2._delegate) |
||
| 75 | |||
| 76 | for function in tabular_interfaces_contracts[tabular_operators_reverse[instance1_type]]: |
||
| 77 | assert id(getattr(instance1, function)) != id(getattr(instance2, function)) |
||
| 78 | assert id(getattr(instance1._delegate, function)) != id(getattr(instance2._delegate, function)) |
||
| 79 | return _assert_correct_delegate_behaviour |
||
| 92 |