| 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 |  |  |             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: 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 |  |  |         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.ordered_stages: | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             if self.groups[cur_stage] == group_number: | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 stages_from_group.append(cur_stage) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         return stages_from_group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def get_first_item_from_next_group(self, group_number): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         """Returns first items from next group. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         If actual group is last, then None will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if group_number >= self.get_actual_numbers_of_groups(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             for actual_stage in self.ordered_stages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 if self.groups[actual_stage] == group_number + 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     return actual_stage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             raise ValueError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def get_items_from_group(self, group_number): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         """Returns list of stages from group with 'group_number'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 list: list of stages from group ordered as in self.ordered_stages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             Raises: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 ValueError: If wrong number of groups. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 group_number (int): number of group""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if group_number not in range(0, self.get_actual_numbers_of_groups()): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             raise ValueError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         stages_to_return = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         for current_stage in self.ordered_stages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             if self.groups[current_stage] == group_number: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 stages_to_return.append(current_stage) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |         return stages_to_return | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |