Untitled

 avatar
unknown
python
2 years ago
1.6 kB
5
Indexable
def split_task(self, successor_tasks=None):
    # Check if batch_size is defined and less than quantity
    if self.batch_size and self.batch_size < self.quantity:
        num_batches = self.quantity // self.batch_size
        remaining = self.quantity % self.batch_size
        # Create batches
        batches = []
        for i in range(num_batches):
            new_task = self.copy(deep=True)
            new_task.quantity = self.batch_size
            new_task.id = f"{self.id}_batch_{i+1}"
            if i > 0:  # not the first batch, so predecessor is the previous batch
                new_task.predecessors = [batches[-1]]
            batches.append(new_task)
        # Handle remaining quantity
        if remaining > 0:
            new_task = self.copy(deep=True)
            new_task.quantity = remaining
            new_task.id = f"{self.id}_batch_{num_batches + 1}"
            new_task.predecessors = [batches[-1]]  # predecessor is the previous batch
            batches.append(new_task)
        # Update successors' predecessors
        if successor_tasks:
            # If the number of batches is the same, assign corresponding batches
            if len(batches) == len(successor_tasks):
                for batch, task in zip(batches, successor_tasks):
                    task.predecessors = [batch]
            else:  # Else, assign all batches to all successors
                for task in successor_tasks:
                    task.predecessors = batches
        return batches
    else:
        return [self]
Editor is loading...