1
|
|
|
"""Class represents grouping strategy.""" |
2
|
1 |
|
STAGE_NOT_IN_STRATEGY_ = "Stage not considered in strategy." |
3
|
|
|
|
4
|
|
|
|
5
|
1 |
|
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 information about groups. |
10
|
|
|
Key is stage and value is number of group. |
11
|
|
|
""" |
12
|
|
|
|
13
|
1 |
|
def __init__(self, ordered_stages): |
14
|
|
|
"""Constructor.""" |
15
|
1 |
|
self.ordered_stages = ordered_stages |
16
|
1 |
|
self.groups = {} |
17
|
|
|
|
18
|
1 |
|
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
|
1 |
|
group_number = self.get_actual_numbers_of_groups() |
27
|
1 |
|
for stage in group_of_stages: |
28
|
1 |
|
if stage in self.groups.keys(): |
29
|
1 |
|
raise ValueError("Stage already added.") |
30
|
1 |
|
elif stage not in self.ordered_stages: |
31
|
1 |
|
raise ValueError("Stage not considered in strategy.") |
32
|
|
|
else: |
33
|
1 |
|
self.groups[stage] = group_number |
34
|
|
|
|
35
|
1 |
|
def get_actual_numbers_of_groups(self): |
36
|
|
|
"""Returns how many groups already. |
37
|
|
|
|
38
|
|
|
Returns: |
39
|
|
|
int: amount of groups |
40
|
|
|
""" |
41
|
1 |
|
if len(self.groups) == 0: |
42
|
1 |
|
return 0 |
43
|
|
|
else: |
44
|
1 |
|
return sorted(self.groups.values())[-1] + 1 |
45
|
|
|
|
46
|
1 |
|
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: list of stages from group ordered as in self.ordered_stages |
50
|
|
|
|
51
|
|
|
Raises: |
52
|
|
|
ValueError: If stage is not in strategy. |
53
|
|
|
|
54
|
|
|
Args: |
55
|
|
|
stage (AbstractStage): stage""" |
56
|
1 |
|
if stage not in self.groups: |
57
|
1 |
|
raise ValueError(STAGE_NOT_IN_STRATEGY_) |
58
|
1 |
|
group_number = self.groups[stage] |
59
|
1 |
|
stages_from_group = [] |
60
|
1 |
|
for cur_stage in self.ordered_stages: |
61
|
1 |
|
if self.groups[cur_stage] == group_number: |
62
|
1 |
|
stages_from_group.append(cur_stage) |
63
|
1 |
|
return stages_from_group |
64
|
|
|
|
65
|
1 |
|
def get_items_from_group(self, group_number): |
66
|
|
|
"""Returns list of stages from group with 'group_number'. |
67
|
|
|
Returns: |
68
|
|
|
list: list of stages from group ordered as in |
69
|
|
|
self.ordered_stages |
70
|
|
|
|
71
|
|
|
Raises: |
72
|
|
|
ValueError: If wrong number of groups. |
73
|
|
|
|
74
|
|
|
Args: |
75
|
|
|
group_number (int): number of group""" |
76
|
1 |
|
if group_number not in range(0, self.get_actual_numbers_of_groups()): |
77
|
1 |
|
raise ValueError |
78
|
1 |
|
stages_to_return = [] |
79
|
1 |
|
for current_stage in self.ordered_stages: |
80
|
1 |
|
if self.groups[current_stage] == group_number: |
81
|
1 |
|
stages_to_return.append(current_stage) |
82
|
|
|
return stages_to_return |
83
|
|
|
|