28 lines
609 B
Python
28 lines
609 B
Python
from itertools import tee
|
|
from typing import Iterable
|
|
|
|
|
|
def inspect(prefix="inspect", embed=False):
|
|
"""Can be used to inspect compositions of generator functions by placing inbetween two functions."""
|
|
|
|
def inner(x):
|
|
|
|
if isinstance(x, Iterable) and not isinstance(x, dict) and not isinstance(x, tuple):
|
|
x, y = tee(x)
|
|
y = list(y)
|
|
else:
|
|
y = x
|
|
|
|
l = f" {len(y)} items" if isinstance(y, list) else ""
|
|
|
|
print(f"{prefix}{l}:", y)
|
|
|
|
if embed:
|
|
import IPython
|
|
|
|
IPython.embed()
|
|
|
|
return x
|
|
|
|
return inner
|