Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(2)  |  Call(2)  |  Derive(0)  |  Import(0)
Modifies the AST tree in place to reduce the duplicated computations.

        def cache_expressions(node):
    "Modifies the AST tree in place to reduce the duplicated computations."
    # Count the occurance of each expression
    counts = defaultdict(int)
    def count_func(pattern, node):
        name = node_name(node)
        if isinstance(name, tuple) and "Operator" in name[0]:
            counts[name] += 1
    tile(node, [Pattern()], count_func)

    # Replace all the expressions with count > 1 with
    # a cached version
    state = {"last_id": 0}
    replacements = {}
    def repl_func(pattern, node):
        # Check for a cached node
        name = node_name(node)
        if name and name in counts and counts[name] > 1:
            if name in replacements:
                return replacements[name]
            else:
                cache_node = ast.CachedNode(node, state["last_id"])
                replacements[name] = cache_node
                state["last_id"] += 1
                return cache_node

    tile(node, [Pattern()], repl_func)
    return node
        


src/p/y/pypred-0.3.6/pypred/merge.py   pypred(Download)
    # Cache any common expressions
    if settings.cache_expr:
        cache.cache_expressions(ast)
 
    return ast

src/p/y/pypred-HEAD/pypred/merge.py   pypred(Download)
    # Cache any common expressions
    if settings.cache_expr:
        cache.cache_expressions(ast)
 
    return ast