def _map_v2()

in src/chug/wds/filters.py [0:0]


def _map_v2(data, f, handler=wds.reraise_exception):
    """ Map samples.

    This function differs from wds.map, it only adds '__key__' back to sample if it exists.

    """
    for sample in data:
        try:
            result = f(sample)
        except Exception as exn:
            if handler(exn):
                continue
            else:
                break
        if result is None:
            continue
        if isinstance(sample, dict) and isinstance(result, dict) and "__key__" in sample:
            result["__key__"] = sample.get("__key__")
        yield result