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

All Samples(26)  |  Call(25)  |  Derive(0)  |  Import(1)

        def new_constant(node, value):
    if isinstance(value, bool):
        name = "True" if value else "False"
        if sys.version_info >= (3, 4):
            # NameConstant was added to Python 3.4, see
            # http://bugs.python.org/issue16619
            new_node = ast.NameConstant(value=value)
        else:
            new_node = ast.Name(id=name, ctx=ast.Load())
    elif isinstance(value, COMPLEX_TYPES):
        new_node = ast.Num(n=value)
    elif isinstance(value, UNICODE_TYPE):
        if PYTHON3:
            new_node = ast.Str(s=value)
        else:
            new_node = ast.Str(s=value)
    elif isinstance(value, BYTES_TYPE):
        if PYTHON3:
            new_node = ast.Bytes(s=value)
        else:
            new_node = ast.Str(s=value)
    elif value is None:
        if sys.version_info >= (3, 4):
            # NameConstant was added to Python 3.4, see
            # http://bugs.python.org/issue16619
            new_node = ast.NameConstant(value=None)
        else:
            new_node = ast.Name(id="None", ctx=ast.Load())
    elif isinstance(value, tuple):
        elts = [new_constant(node, elt) for elt in value]
        new_node = ast.Tuple(elts=elts, ctx=ast.Load())
    elif isinstance(value, frozenset):
        if all(isinstance(elt, UNICODE_TYPE) for elt in value):
            arg = new_constant(node, UNICODE_TYPE().join(sorted(value)))
        elif all(isinstance(elt, BYTES_TYPE) for elt in value):
            arg = new_constant(node, BYTES_TYPE().join(sorted(value)))
        else:
            elts = [new_constant(node, elt) for elt in value]
            arg = ast.Tuple(elts=elts, ctx=ast.Load())
            copy_lineno(node, arg)
        func = ast.Name(id='frozenset', ctx=ast.Load())
        new_node = ast.Call(func, [arg], [], None, None)
    else:
        raise NotImplementedError("unable to create an AST object for constant: %r" % (value,))
    return copy_lineno(node, new_node)
        


src/a/s/astoptimizer-0.6/astoptimizer/optimizer.py   astoptimizer(Download)
 
from astoptimizer import UNSET
from astoptimizer.ast_tools import (
    copy_lineno,
    new_constant, new_literal, new_call, new_pass,
    def visit_Name(self, node):
        constant = self.load_name(node.id)
        if constant is UNSET:
            return
        return new_constant(node, constant)
        if value is UNSET:
            return
        return new_constant(node, value)
 
    def check_format_args(self, format, values):
        if not self.config.check_result(result):
            return UNSET
        return new_constant(node, result)
 
    def check_binop_cst(self, op, left, right):
        if not self.config.check_result(result):
            return UNSET
        return new_constant(node, result)
 
    def visit_BinOp(self, node):