| Conditions | 2 |
| Total Lines | 77 |
| Code Lines | 49 |
| 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 |
||
| 34 | def test_phi_creation(app_phi_function, phi_registry, registering_phi_at_the_same_key_error, |
||
| 35 | assert_function_properties_values): |
||
| 36 | |||
| 37 | PhiFunction = app_phi_function |
||
| 38 | |||
| 39 | @PhiFunction.register('pame') |
||
| 40 | def ela(x): |
||
| 41 | """ela Docstring""" |
||
| 42 | return x + 1 |
||
| 43 | |||
| 44 | assert 'pame' in phi_registry |
||
| 45 | assert_function_properties_values(ela, 'ela', 'ela Docstring') |
||
| 46 | |||
| 47 | assert phi_registry.get('pame').__name__ == 'ela' |
||
| 48 | assert phi_registry.get('pame').__doc__ == 'ela Docstring' |
||
| 49 | test_value = 5 |
||
| 50 | assert phi_registry.get('pame')(test_value) == test_value + 1 |
||
| 51 | |||
| 52 | @PhiFunction.register('') |
||
| 53 | def gg(x): |
||
| 54 | return x * 2 |
||
| 55 | assert 'gg' in phi_registry |
||
| 56 | |||
| 57 | @PhiFunction.register() |
||
| 58 | def dd(x): |
||
| 59 | return x * 2 |
||
| 60 | assert 'dd' in phi_registry |
||
| 61 | assert phi_registry.get('dd')(4) == 8 |
||
| 62 | |||
| 63 | @PhiFunction.register('edw') |
||
| 64 | class Edw: |
||
| 65 | def __call__(self, data, **kwargs): |
||
| 66 | return data + 5 |
||
| 67 | assert 'edw' in phi_registry |
||
| 68 | assert phi_registry.get('edw')(3) == 8 |
||
| 69 | |||
| 70 | @PhiFunction.register('') |
||
| 71 | class qw: |
||
| 72 | def __call__(self, data, **kwargs): |
||
| 73 | """Subtract 5 from the input number. |
||
| 74 | |||
| 75 | Args: |
||
| 76 | data ([type]): numerical value |
||
| 77 | |||
| 78 | Returns: |
||
| 79 | [type]: the result of the subtraction |
||
| 80 | """ |
||
| 81 | return data - 5 |
||
| 82 | |||
| 83 | assert 'qw' in phi_registry |
||
| 84 | assert phi_registry.get('qw')(3) == -2 |
||
| 85 | |||
| 86 | assert_function_properties_values(phi_registry.get('qw'), 'qw', qw.__call__.__doc__) |
||
| 87 | |||
| 88 | @PhiFunction.register() |
||
| 89 | class Nai: |
||
| 90 | def __call__(self, data, **kwargs): |
||
| 91 | """Subtract 1 to the input number.""" |
||
| 92 | return data - 1 |
||
| 93 | |||
| 94 | assert 'Nai' in phi_registry |
||
| 95 | |||
| 96 | assert_function_properties_values(phi_registry.get('Nai'), 'Nai', 'Subtract 1 to the input number.') |
||
| 97 | |||
| 98 | assert phi_registry.get('Nai')(3) == 2 |
||
| 99 | |||
| 100 | def gg(x): |
||
| 101 | return x |
||
| 102 | |||
| 103 | test_value = 1 |
||
| 104 | assert gg(test_value) == test_value |
||
| 105 | assert phi_registry.get('gg')(test_value) == test_value * 2 |
||
| 106 | |||
| 107 | with pytest.raises(registering_phi_at_the_same_key_error): |
||
| 108 | PhiFunction.register('')(gg) |
||
| 109 | |||
| 110 | assert phi_registry.get('gg')(test_value) == test_value * 2 |
||
| 111 |