Completed
Pull Request — master (#43)
by Wojtek
10:50
created

GroupingStrategy   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_items_from_the_same_group() 0 18 4
A __init__() 0 4 1
A get_actual_numbers_of_groups() 0 10 2
A define_group() 0 16 4
1
"""Class represents grouping strategy."""
2
STAGE_NOT_IN_STRATEGY_ = "Stage not considered in strategy."
3
4
5
class GroupingStrategy(object):
6
    """Class represents grouping strategy.
7
    Attributes:
8
        ordred_stages (list): List of stages which represent order in strategy.
9
        groups (dict): Contains informations about groups.
10
                        Key is stage and value is number of group.
11
    """
12
13
    def __init__(self, ordered_stages):
14
        """Constructor."""
15
        self.ordered_stages = ordered_stages
16
        self.groups = {}
17
18
    def define_group(self, group_of_stages):
19
        """Add group of stages. Those stages will be in the same group.
20
21
        Raises:
22
            ValueError: If stage couldn't be added to new group.
23
24
        Args:
25
            group_of_stages (tuple): group of stages"""
26
        group_number = self.get_actual_numbers_of_groups()
27
        for stage in group_of_stages:
28
            if stage in self.groups.keys():
29
                raise ValueError("Stage already added.")
30
            elif stage not in self.ordered_stages:
31
                raise ValueError("Stage not considered in strategy.")
32
            else:
33
                self.groups[stage] = group_number
34
35
    def get_actual_numbers_of_groups(self):
36
        """Returns how many groups already.
37
38
        Returns:
39
            amount (int) : amount of groups
40
            """
41
        if len(self.groups) == 0:
42
            return 0
43
        else:
44
            return sorted(self.groups.values())[-1] + 1
45
46
    def get_items_from_the_same_group(self, stage):
47
        """Returns list of stages from group in which is already stage.
48
        Returns:
49
            list_of_stages (list): list of stages from group
50
51
        Raises:
52
            ValueError: If stage is not in strategy.
53
54
        Args:
55
            stage (AbstractStage): stage"""
56
        if stage not in self.groups:
57
            raise ValueError(STAGE_NOT_IN_STRATEGY_)
58
        group_number = self.groups[stage]
59
        stages_from_group = []
60
        for cur_stage in self.groups:
61
            if self.groups[cur_stage] == group_number:
62
                stages_from_group.append(cur_stage)
63
        return stages_from_group
64