예제 #1
0
def walk_pair(inner, outer, data, schema, handler):
    """ Traverse a pair of data structures and apply a function to each node. """
    def process_node(inner, k, v1, v2):
        if (not isinstance(v1, collections.Iterable)
            or isinstance(v1, basestring)
            or ((isinstance(v1, collections.Sequence)
                 and not isinstance(v1, basestring)
                 and hasattr(v2, '__call__')))):
            return inner(k, v1, v2)
        if isinstance(v1, collections.Sequence):
            rows = tuple(walk_pair(inner, identity, row, v2, handler) for row in v1)
            rv = tuple(filter(lambda row: row, rows))
        else:
            rv = walk_pair(inner, identity, v1, v2, handler)
        return (k, rv) if rv else None
    if isinstance(data, collections.Sequence):
        if isinstance(schema, collections.Sequence):
            schema_row = first(schema)
        else:
            schema_row = schema
        return outer(tuple(walk_pair(inner, identity, row, schema_row, handler)
                           for row in data))
    nodes = ()
    for (k, v) in data.iteritems():
        if k not in schema:
            if isinstance(k, collections.Sequence) and not isinstance(k, basestring):
                if not set(k).intersection(set(schema)):
                    handler(k)
                    nodes += ()
                else:
                    nodes += (process_node(inner, k, v, schema[last(k)]),)
            else:
                handler(k)
                nodes += ()
        else:
            nodes += (process_node(inner, k, v, schema[k]),)
    return outer(dict(filter(lambda node: node is not None, nodes)))
예제 #2
0
def walk_pair(inner, outer, data, schema, handler):
    """ Traverse a pair of data structures and apply a function to each node. """
    def process_node(inner, k, v1, v2):
        if (not isinstance(v1, collections.Iterable)
            or isinstance(v1, basestring)
            or ((isinstance(v1, collections.Sequence)
                 and not isinstance(v1, basestring)
                 and hasattr(v2, '__call__')))):
            return inner(k, v1, v2)
        if isinstance(v1, collections.Sequence):
            rows = tuple(walk_pair(inner, identity, row, v2, handler) for row in v1)
            rv = tuple(filter(lambda row: row, rows))
        else:
            rv = walk_pair(inner, identity, v1, v2, handler)
        return (k, rv) if rv else None
    if isinstance(data, collections.Sequence):
        if isinstance(schema, collections.Sequence):
            schema_row = first(schema)
        else:
            schema_row = schema
        return outer(tuple(walk_pair(inner, identity, row, schema_row, handler)
                           for row in data))
    nodes = ()
    for (k, v) in data.iteritems():
        if k not in schema:
            if isinstance(k, collections.Sequence) and not isinstance(k, basestring):
                if not set(k).intersection(set(schema)):
                    handler(k)
                    nodes += ()
                else:
                    nodes += (process_node(inner, k, v, schema[last(k)]),)
            else:
                handler(k)
                nodes += ()
        else:
            nodes += (process_node(inner, k, v, schema[k]),)
    return outer(dict(filter(lambda node: node is not None, nodes)))
예제 #3
0
def run_last():
    list_a = list(range(10001))
    list_b = list(range(15001))
    list_c = list(range(20001))
    list_d = list(range(25001))
    list_e = list(range(30001))
    list_f = list(range(35001))
    list_g = list(range(40001))
    list_h = list(range(45001))
    list_i = list(range(50001))
    list_j = list(range(55001))
    list_k = list(range(60001))
    list_l = list(range(65001))
    list_m = list(range(70001))
    list_n = list(range(75001))
    list_o = list(range(80001))
    list_p = list(range(85001))
    list_q = list(range(90001))
    list_r = list(range(95001))
    list_s = list(range(100001))

    print("LAST")
    time_a = print(timeit.timeit(lambda: last(list_a)))
    time_b = print(timeit.timeit(lambda: last(list_b)))
    time_c = print(timeit.timeit(lambda: last(list_c)))
    time_d = print(timeit.timeit(lambda: last(list_d)))
    time_e = print(timeit.timeit(lambda: last(list_e)))
    time_f = print(timeit.timeit(lambda: last(list_f)))
    time_g = print(timeit.timeit(lambda: last(list_g)))
    time_h = print(timeit.timeit(lambda: last(list_h)))
    time_i = print(timeit.timeit(lambda: last(list_i)))
    time_j = print(timeit.timeit(lambda: last(list_j)))
    time_k = print(timeit.timeit(lambda: last(list_k)))
    time_l = print(timeit.timeit(lambda: last(list_l)))
    time_m = print(timeit.timeit(lambda: last(list_m)))
    time_n = print(timeit.timeit(lambda: last(list_n)))
    time_o = print(timeit.timeit(lambda: last(list_o)))
    time_p = print(timeit.timeit(lambda: last(list_p)))
    time_q = print(timeit.timeit(lambda: last(list_q)))
    time_r = print(timeit.timeit(lambda: last(list_r)))
    time_s = print(timeit.timeit(lambda: last(list_s)))
예제 #4
0
def times():
    list_a = list(range(10001))
    list_b = list(range(15001))
    list_c = list(range(20001))
    list_d = list(range(25001))
    list_e = list(range(30001))
    list_f = list(range(35001))
    list_g = list(range(40001))
    list_h = list(range(45001))
    list_i = list(range(50001))
    list_j = list(range(55001))
    list_k = list(range(60001))
    list_l = list(range(65001))
    list_m = list(range(70001))
    list_n = list(range(75001))
    list_o = list(range(80001))
    list_p = list(range(85001))
    list_q = list(range(90001))
    list_r = list(range(95001))
    list_s = list(range(100001))

    time_a = timeit.timeit(lambda: last(list_a))
    time_b = timeit.timeit(lambda: last(list_b))
    time_c = timeit.timeit(lambda: last(list_c))
    time_d = timeit.timeit(lambda: last(list_d))
    time_e = timeit.timeit(lambda: last(list_e))
    time_f = timeit.timeit(lambda: last(list_f))
    time_g = timeit.timeit(lambda: last(list_g))
    time_h = timeit.timeit(lambda: last(list_h))
    time_i = timeit.timeit(lambda: last(list_i))
    time_j = timeit.timeit(lambda: last(list_j))
    time_k = timeit.timeit(lambda: last(list_k))
    time_l = timeit.timeit(lambda: last(list_l))
    time_m = timeit.timeit(lambda: last(list_m))
    time_n = timeit.timeit(lambda: last(list_n))
    time_o = timeit.timeit(lambda: last(list_o))
    time_p = timeit.timeit(lambda: last(list_p))
    time_q = timeit.timeit(lambda: last(list_q))
    time_r = timeit.timeit(lambda: last(list_r))
    time_s = timeit.timeit(lambda: last(list_s))

    return [
        round(time_a, 2),
        round(time_b, 2),
        round(time_c, 2),
        round(time_d, 2),
        round(time_e, 2),
        round(time_f, 2),
        round(time_g, 2),
        round(time_h, 2),
        round(time_i, 2),
        round(time_j, 2),
        round(time_k, 2),
        round(time_l, 2),
        round(time_m, 2),
        round(time_n, 2),
        round(time_o, 2),
        round(time_p, 2),
        round(time_q, 2),
        round(time_r, 2),
        round(time_s, 2)
    ]
예제 #5
0
 def sanitize_key(k, v):
     if is_seq(k):
         return (last(k), v)
     return (k, v)
예제 #6
0
def test_last_ok():
    result = functions.last(['first', 'middle', 'last'])
    assert 'last' == result
예제 #7
0
 def sanitize_key(k, v):
     if is_seq(k):
         return (last(k), v)
     return (k, v)