Passed
Push — mpeta ( 1841cb...62640f )
by Konstantinos
03:46
created

datapoint_files.datapoint_files_to_test()   F

Complexity

Conditions 28

Size

Total Lines 148
Code Lines 120

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 120
nop 2
dl 0
loc 148
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like datapoint_files.datapoint_files_to_test() 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
import pytest
2
3
4
@pytest.fixture
5
def test_datapoints_full_file_path():
6
    import os
7
    return lambda file_name: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'dts', file_name)
8
9
10
@pytest.fixture
11
def sample_json(test_datapoints_full_file_path):
12
    return test_datapoints_full_file_path('sample-data.jsonlines')
13
14
@pytest.fixture
15
def sample_collaped_json(test_datapoints_full_file_path):
16
    return test_datapoints_full_file_path('sample-data-collapsed.jsonlines')
17
18
19
@pytest.fixture
20
def datapoint_files_to_test(sample_collaped_json, sample_json):
21
    import pandas as pd
22
    import numpy as np
23
    return {
24
        'data_1': {
25
            'data_path': sample_collaped_json,
26
            'nb_rows': 100,
27
            'nb_columns': 46,
28
            'type_distros': {
29
                'type': {str: 100},
30
                'flavors': {list: 98, type(None): 2},
31
            },
32
            'value_distros': {
33
                'type': {'hybrid': 48, 'sativa': 19, 'indica': 33},
34
            },
35
            'row': {
36
                0: {
37
                    'flavors': [lambda v: v == ["Chemical", "Pine", "Diesel"],
38
                                lambda v: type(v) == list,
39
                                ],
40
                    'type': [lambda v: v == 'hybrid',
41
                             lambda v: type(v) == str,
42
                             ],
43
                },
44
                7: {
45
                    'flavors': [lambda v: v == ["Earthy", "Pungent", "Sweet"],
46
                                lambda v: type(v) == list,
47
                                ],
48
                    'type': [lambda v: v == 'hybrid',
49
                             lambda v: type(v) == str,
50
                             ],
51
                },
52
                76: {
53
                    'flavors': [lambda v: v is None,
54
                                lambda v: pd.isnull(v),
55
                                lambda v: isinstance(v, type(None)),
56
                                ],
57
                },
58
                87: {
59
                    'flavors': [lambda v: v is None,
60
                                lambda v: type(v) == type(None),
61
                                ],
62
                },
63
            },
64
            'column_names': (
65
                'flavors',
66
                'name',
67
                'description',
68
                'image_urls',
69
                'parents',
70
                '_id',
71
                'type',
72
                'image_paths',
73
                'Aroused',
74
                'Creative',
75
                'Energetic',
76
                'Euphoric',
77
                'Focused',
78
                'Giggly',
79
                'Happy',
80
                'Hungry',
81
                'Relaxed',
82
                'Sleepy',
83
                'Talkative',
84
                'Tingly',
85
                'Uplifted',
86
                'Cramps',
87
                'Depression',
88
                'Eye Pressure',
89
                'Fatigue',
90
                'Headaches',
91
                'Inflammation',
92
                'Insomnia',
93
                'Lack of Appetite',
94
                'Muscle Spasms',
95
                'Nausea',
96
                'Pain',
97
                'Seizures',
98
                'Spasticity',
99
                'Stress',
100
                'Anxious',
101
                'Dizzy',
102
                'Dry Eyes',
103
                'Dry Mouth',
104
                'Headache',
105
                'Paranoid',
106
                'difficulty',
107
                'flowering',
108
                'height',
109
                'stretch',
110
                'yield',
111
            ),
112
        },
113
        'data_2': {
114
            'data_path': sample_json,
115
            'nb_rows': 100,
116
            'nb_columns': 12,
117
            'type_distros': {
118
                'type': {str: 100},
119
                'flavors': {list: 98, float: 2},
120
            },
121
            'value_distros': {
122
                'type': {'hybrid': 48, 'sativa': 19, 'indica': 33},
123
            },
124
            'row': {
125
                0: {
126
                    'flavors': [lambda v: v == ["Chemical", "Pine", "Diesel"],
127
                                lambda v: type(v) == list,
128
                                ],
129
                    'type': [lambda v: v == 'hybrid',
130
                             lambda v: type(v) == str
131
                             ],
132
                },
133
                7: {
134
                    'flavors': [lambda v: v == ["Earthy", "Pungent", "Sweet"],
135
                                lambda v: type(v) == list,
136
                                ],
137
                    'type': [lambda v: v == 'hybrid',
138
                             lambda v: type(v) == str
139
                             ],
140
                },
141
                76: {
142
                    'flavors': [lambda v: np.isnan(v),
143
                                lambda v: pd.isnull(v),
144
                                lambda v: type(v) == float,
145
                                ],
146
                },
147
                87: {
148
                    'flavors': [lambda v: np.isnan(v),
149
                                lambda v: pd.isnull(v),
150
                                lambda v: type(v) == float,
151
                                ],
152
                },
153
            },
154
            'column_names': (
155
                'flavors',
156
                'name',
157
                'medical',
158
                'description',
159
                'image_urls',
160
                'parents',
161
                'negatives',
162
                'grow_info',
163
                '_id',
164
                'type',
165
                'image_paths',
166
                'effects',
167
            ),
168
        },
169
    }
170