Passed
Push — master ( 8ea497...820800 )
by Max
01:11
created

structured_data._stack_iter.Extend.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
import typing
2
3
T = typing.TypeVar("T")
4
5
6
class Action:
7
    def handle(self, to_process: typing.List[T]):
8
        raise NotImplementedError
9
10
11
class Yield(Action):
12
    def __init__(self, item) -> None:
13
        self.item = item
14
15
    def handle(self, _to_process):
16
        yield self.item
17
18
19
class Extend(Action):
20
    def __init__(self, iterable) -> None:
21
        self.iterable = iterable
22
23
    def handle(self, to_process):
24
        to_process.extend(self.iterable)
25
        yield from ()
26
27
28
def handle(action: typing.Optional[Action], to_process):
29
    if action is not None:
30
        yield from action.handle(to_process)
31
32
33
def stack_iter(first: T, process: typing.Callable[[T], typing.Optional[Action]]):
34
    to_process = [first]
35
    while to_process:
36
        yield from handle(process(to_process.pop()), to_process)
37