
import select

def run(routines):
    while routines:
        for r in routines:
            try:
                result = r.next()
            except StopIteration:
                routines.remove(r)


import select

class Executor:
    def __init__(self):
        self.routines = {}

    def add(self, routine):
        self.routines[routine] = True

    def _remove(self, routine):
        self.routines.pop(routine, None)

    def __call__(self):
        while self.routines:
            for r in tuple(self.routines):
                try:
                    r.next()
                except StopIteration:
                    self._remove(r)

def run(routines):
    ex = Executor()
    for r in routines: ex.add(r)
    r()

Convert coroutine executor to OOP
[Scripts] Convert coroutine executor to OOP

  
  import select
  
+ class Executor:
+     def __init__(self):
+         self.routines = {}
+ 
+     def add(self, routine):
+         self.routines[routine] = True
+ 
+     def _remove(self, routine):
+         self.routines.pop(routine, None)
+ 
+     def __call__(self):
+         while self.routines:
+             for r in tuple(self.routines):
+                 try:
+                     r.next()
+                 except StopIteration:
+                     self._remove(r)
+ 
  def run(routines):
-     while routines:
+     ex = Executor()
-         for r in routines:
+     for r in routines: ex.add(r)
+     r()
-             try:
-                 result = r.next()
-             except StopIteration:
-                 routines.remove(r)
  
Convert coroutine executor to OOP
## Code Before:

import select

def run(routines):
    while routines:
        for r in routines:
            try:
                result = r.next()
            except StopIteration:
                routines.remove(r)

## Instruction:
Convert coroutine executor to OOP
## Code After:

import select

class Executor:
    def __init__(self):
        self.routines = {}

    def add(self, routine):
        self.routines[routine] = True

    def _remove(self, routine):
        self.routines.pop(routine, None)

    def __call__(self):
        while self.routines:
            for r in tuple(self.routines):
                try:
                    r.next()
                except StopIteration:
                    self._remove(r)

def run(routines):
    ex = Executor()
    for r in routines: ex.add(r)
    r()
