GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

_test_group()   F
last analyzed

Complexity

Conditions 17

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
dl 0
loc 56
rs 3.402
c 1
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 _test_group() 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
# Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
# - Redistributions of source code must retain the above copyright
8
#   notice, this list of conditions and the following disclaimer.
9
# - Redistributions in binary form must reproduce the above copyright
10
#   notice, this list of conditions and the following disclaimer in the
11
#   documentation and/or other materials provided with the distribution.
12
# - Neither the name of Salesforce.com nor the names of its contributors
13
#   may be used to endorse or promote products derived from this
14
#   software without specific prior written permission.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
from distributions.tests.util import (
29
    assert_all_close,
30
    list_models,
31
    import_model,
32
)
33
34
35
MODULES = {}
36
for spec in list_models():
37
    MODULES.setdefault(spec['name'], []).append(import_model(spec))
38
39
40
def test_model():
41
    for name in MODULES:
42
        yield _test_model, name
43
44
45
def _test_model(name):
46
    modules = MODULES[name]
47
    assert_all_close([m.NAME for m in modules], err_msg='Model.__name__')
48
    EXAMPLES = [e for m in modules for e in m.EXAMPLES]
49
    for EXAMPLE in EXAMPLES:
50
        raw_shared = EXAMPLE['shared']
51
        shareds = [module.Shared.from_dict(raw_shared) for module in modules]
52
        dumped = [m.dump() for m in shareds]
53
        assert_all_close(dumped, err_msg='shared_dump')
54
55
56
def test_group():
57
    for name in MODULES:
58
        yield _test_group, name
59
60
61
def _test_group(name):
62
    modules = MODULES[name]
63
    EXAMPLES = [e for m in modules for e in m.EXAMPLES]
64
    for EXAMPLE in EXAMPLES:
65
        values = EXAMPLE['values'][:]
66
        raw_shared = EXAMPLE['shared']
67
        temp_shared = modules[0].Shared.from_dict(raw_shared)
68
        for value in values:
69
            temp_shared.add_value(value)
70
        temp_shared.realize()
71
        raw_shared = temp_shared.dump()
72
73
        shareds = [module.Shared.from_dict(raw_shared) for module in modules]
74
        groups = [
75
            module.Group.from_values(shared)
76
            for module, shared in zip(modules, shareds)]
77
        modules_shareds_groups = zip(modules, shareds, groups)
78
79
        for value in values:
80
            for module, shared, group in modules_shareds_groups:
81
                group.add_value(shared, value)
82
            dumped = [g.dump() for g in groups]
83
            assert_all_close(dumped, err_msg='group_dump')
84
85
        for module, shared, group in modules_shareds_groups:
86
            value = group.sample_value(shared)
87
            shared.add_value(value)
88
            values.append(value)
89
90
        for value in values:
91
            scores = [
92
                group.score_value(shared, value)
93
                for module, shared, group in modules_shareds_groups
94
            ]
95
            for module, shared, group in modules_shareds_groups:
96
                print "------------------"
97
                print module
98
                print shared.dump()
99
                print group.dump()
100
            print value
101
            assert_all_close(scores, err_msg='score_value')
102
103
        scores = [
104
            group.score_data(shared)
105
            for module, shared, group in modules_shareds_groups]
106
        assert_all_close(scores, err_msg='score_data')
107
108
        for module, shared, group in modules_shareds_groups:
109
            dumped = group.dump()
110
            group.init(shared)
111
            group.load(dumped)
112
113
        scores = [
114
            group.score_data(shared)
115
            for module, shared, group in modules_shareds_groups]
116
        assert_all_close(scores, err_msg='score_data')
117
118
119
def test_plus_group():
120
    for name in MODULES:
121
        yield _test_plus_group, name
122
123
124
def _test_plus_group(name):
125
    modules = MODULES[name]
126
    modules = [m for m in modules if hasattr(m.Shared, 'plus_group')]
127
    EXAMPLES = [e for m in modules for e in m.EXAMPLES]
128
    for EXAMPLE in EXAMPLES:
129
        raw_shared = EXAMPLE['shared']
130
        values = EXAMPLE['values']
131
        shareds = [module.Shared.from_dict(raw_shared) for module in modules]
132
        groups = [
133
            module.Group.from_values(shared, values)
134
            for module, shared in zip(modules, shareds)]
135
        dumped = [m.plus_group(g).dump() for m, g in zip(shareds, groups)]
136
        assert_all_close(dumped, err_msg='shared._plus_group(group)')
137