Exemple #1
0
 def __init__(self, data, fieldnames=None):
     first_value, iterator = iterpeek(data)
     if isinstance(first_value, dict):
         if not fieldnames:
             fieldnames = list(first_value.keys())
         super(DataSource, self).__init__(iterator, fieldnames)
     else:
         if fieldnames:
             iterator = itertools.chain([fieldnames], iterator)
         super(DataSource, self).__init__(iterator)
Exemple #2
0
def _require_callable(data, function):
    if data is NOVALUE:
        return Invalid(None)  # <- EXIT!

    def wrapped(element):
        try:
            if isinstance(element, BaseElement):
                returned_value = function(element)
            else:
                returned_value = function(*element)
        except Exception:
            returned_value = False  # Raised errors count as False.

        if returned_value == True:
            return None  # <- EXIT!

        if returned_value == False:
            return Invalid(element)  # <- EXIT!

        if isinstance(returned_value, BaseDifference):
            return returned_value  # <- EXIT!

        callable_name = function.__name__
        message = \
            '{0!r} returned {1!r}, should return True, False or a difference instance'
        raise TypeError(message.format(callable_name, returned_value))

    if isinstance(data, BaseElement):
        return wrapped(data)  # <- EXIT!

    results = (wrapped(elem) for elem in data)
    diffs = (diff for diff in results if diff)
    first_element, diffs = iterpeek(diffs)
    if first_element:  # If not empty, return diffs.
        return diffs
    return None