Spaces:
Runtime error
Runtime error
File size: 1,054 Bytes
4f8ad24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import random
from typing import Iterator, Optional
from .base import BaseDataSource
from ..model import ImageItem
class ComposedDataSource(BaseDataSource):
def __init__(self, *sources: BaseDataSource):
self.sources = sources
def _iter(self) -> Iterator[ImageItem]:
for source in self.sources:
yield from iter(source)
def _iter_from(self) -> Iterator[ImageItem]:
yield from self._iter()
class ParallelDataSource(BaseDataSource):
def __init__(self, *sources: BaseDataSource, seed: Optional[int] = None):
self.sources = sources
self.random = random.Random(seed)
def _iter(self) -> Iterator[ImageItem]:
iters = [iter(source) for source in self.sources]
while len(iters) > 0:
id_ = self.random.choice(range(len(iters)))
iter_ = iters[id_]
try:
yield next(iter_)
except StopIteration:
iters.pop(id_)
def _iter_from(self) -> Iterator[ImageItem]:
yield from self._iter()
|