<commit_before>from functools import reduce
from itertools import chain

from bndl.compute.dataset.base import Dataset, Partition


class ZippedDataset(Dataset):
    def __init__(self, *sources, comb, dset_id=None):
        assert len(sources) > 1
        self.srcparts = [src.parts() for src in sources]
        self.pcount = len(self.srcparts[0])
        assert all(self.pcount == len(srcparts) for srcparts in self.srcparts[1:])
        self.comb = comb
        super().__init__(sources[0].ctx, src=sources, dset_id=dset_id)

    def parts(self):
        return [ZippedPartition(self, i, [srcpart[i] for srcpart in self.srcparts])
                for i in range(self.pcount)]


class ZippedPartition(Partition):
    def __init__(self, dset, idx, children):
        super().__init__(dset, idx)
        self.children = children

    def _preferred_workers(self, workers):
        prefs = [set(child.preferred_workers(workers) or ()) for child in self.children]
        matches = reduce(lambda a, b: a.intersection(b), prefs)
        if matches:
            return matches
        else:
            return set(chain.from_iterable(prefs))

    def _materialize(self, ctx):
        yield from self.dset.comb(*(child.materialize(ctx) for child in self.children))<commit_msg>use the src prop instead of children<commit_after>from functools import reduce
from itertools import chain

from bndl.compute.dataset.base import Dataset, Partition


class ZippedDataset(Dataset):
    def __init__(self, *sources, comb, dset_id=None):
        assert len(sources) > 1
        self.srcparts = [src.parts() for src in sources]
        self.pcount = len(self.srcparts[0])
        assert all(self.pcount == len(srcparts) for srcparts in self.srcparts[1:])
        self.comb = comb
        super().__init__(sources[0].ctx, src=sources, dset_id=dset_id)

    def parts(self):
        return [ZippedPartition(self, i, [srcpart[i] for srcpart in self.srcparts])
                for i in range(self.pcount)]


class ZippedPartition(Partition):
    def __init__(self, dset, idx, children):
        super().__init__(dset, idx, children)

    def _preferred_workers(self, workers):
        prefs = [set(child.preferred_workers(workers) or ()) for child in self.src]
        matches = reduce(lambda a, b: a.intersection(b), prefs)
        if matches:
            return matches
        else:
            return set(chain.from_iterable(prefs))

    def _materialize(self, ctx):
        yield from self.dset.comb(*(child.materialize(ctx) for child in self.src))
{'fraction_non_alphanumeric': 0.11018957345971564, 'fraction_numerical': 0.00315955766192733, 'mean_word_length': 3.072347266881029, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3486662', 'n_tokens_mistral': 786, 'n_tokens_neox': 777, 'n_words': 206}