| Conditions | 15 |
| Total Lines | 82 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 15.225 |
| 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:
Complex classes like etlt.helper.Type2CondenseHelper.Type2CondenseHelper._distinct() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | 1 | from typing import Any, Dict, List, Optional, Set, Tuple |
|
| 16 | @staticmethod |
||
| 17 | def _distinct(row1: Tuple[int, int], row2: Tuple[int, int]) -> Optional[List[Tuple[int, int]]]: |
||
| 18 | """ |
||
| 19 | Returns a list of distinct (or none overlapping) intervals if two intervals are overlapping. Returns None if |
||
| 20 | the two intervals are none overlapping. The list can have 2 or 3 intervals. |
||
| 21 | |||
| 22 | :param row1: The first interval. |
||
| 23 | :param row2: The second interval. |
||
| 24 | """ |
||
| 25 | 1 | relation = Allen.relation(row1[0], row1[1], row2[0], row2[1]) |
|
| 26 | |||
| 27 | 1 | if relation is None: |
|
| 28 | # One of the 2 intervals is invalid. |
||
| 29 | return [] |
||
| 30 | |||
| 31 | 1 | if relation == Allen.X_BEFORE_Y: |
|
| 32 | # row1: |----| |
||
| 33 | # row2: |-----| |
||
| 34 | 1 | return None # [(row1[0], row1[1]), (row2[0], row2[1])] |
|
| 35 | |||
| 36 | 1 | if relation == Allen.X_BEFORE_Y_INVERSE: |
|
| 37 | # row1: |-----| |
||
| 38 | # row2: |----| |
||
| 39 | 1 | return None # [(row2[0], row2[1]), (row1[0], row1[1])] |
|
| 40 | |||
| 41 | 1 | if relation == Allen.X_MEETS_Y: |
|
| 42 | # row1: |-------| |
||
| 43 | # row2: |-------| |
||
| 44 | 1 | return None # [(row1[0], row1[1]), (row2[0], row2[1])] |
|
| 45 | |||
| 46 | 1 | if relation == Allen.X_MEETS_Y_INVERSE: |
|
| 47 | # row1: |-------| |
||
| 48 | # row2: |-------| |
||
| 49 | 1 | return None # [(row2[0], row2[1]), (row1[0], row1[1])] |
|
| 50 | |||
| 51 | 1 | if relation == Allen.X_OVERLAPS_WITH_Y: |
|
| 52 | # row1: |-----------| |
||
| 53 | # row2: |----------| |
||
| 54 | 1 | return [(row1[0], row2[0] - 1), (row2[0], row1[1]), (row1[1] + 1, row2[1])] |
|
| 55 | |||
| 56 | 1 | if relation == Allen.X_OVERLAPS_WITH_Y_INVERSE: |
|
| 57 | # row1: |----------| |
||
| 58 | # row2: |-----------| |
||
| 59 | 1 | return [(row2[0], row1[0] - 1), (row1[0], row2[1]), (row2[1] + 1, row1[1])] |
|
| 60 | |||
| 61 | 1 | if relation == Allen.X_STARTS_Y: |
|
| 62 | # row1: |------| |
||
| 63 | # row2: |----------------| |
||
| 64 | return [(row1[0], row1[1]), (row1[1] + 1, row2[1])] |
||
| 65 | |||
| 66 | 1 | if relation == Allen.X_STARTS_Y_INVERSE: |
|
| 67 | # row1: |----------------| |
||
| 68 | # row2: |------| |
||
| 69 | 1 | return [(row2[0], row2[1]), (row2[1] + 1, row1[1])] |
|
| 70 | |||
| 71 | 1 | if relation == Allen.X_DURING_Y: |
|
| 72 | # row1: |------| |
||
| 73 | # row2: |----------------| |
||
| 74 | 1 | return [(row2[0], row1[0] - 1), (row1[0], row1[1]), (row1[1] + 1, row2[1])] |
|
| 75 | |||
| 76 | 1 | if relation == Allen.X_DURING_Y_INVERSE: |
|
| 77 | # row1: |----------------| |
||
| 78 | # row2: |------| |
||
| 79 | 1 | return [(row1[0], row2[0] - 1), (row2[0], row2[1]), (row2[1] + 1, row1[1])] |
|
| 80 | |||
| 81 | 1 | if relation == Allen.X_FINISHES_Y: |
|
| 82 | # row1: |------| |
||
| 83 | # row2: |----------------| |
||
| 84 | 1 | return [(row2[0], row1[0] - 1), (row1[0], row1[1])] |
|
| 85 | |||
| 86 | 1 | if relation == Allen.X_FINISHES_Y_INVERSE: |
|
| 87 | # row1: |----------------| |
||
| 88 | # row2: |------| |
||
| 89 | 1 | return [(row1[0], row2[0] - 1), (row2[0], row2[1])] |
|
| 90 | |||
| 91 | 1 | if relation == Allen.X_EQUAL_Y: |
|
| 92 | # row1: |----------------| |
||
| 93 | # row2: |----------------| |
||
| 94 | 1 | return None # [(row1[0], row1[1])] |
|
| 95 | |||
| 96 | # We got all 13 relation in Allen's interval algebra covered. |
||
| 97 | raise ValueError('Unexpected relation {0}'.format(relation)) |
||
| 98 | |||
| 153 |