Python _ast 模块,NameConstant() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用_ast.NameConstant()

项目:sublimeTextConfig    作者:luoye-fe    | 项目源码 | 文件源码
def convert_to_value(item):
    if isinstance(item, ast.Str):
        return item.s
    elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
        return item.s
    elif isinstance(item, ast.Tuple):
        return tuple(convert_to_value(i) for i in item.elts)
    elif isinstance(item, ast.Num):
        return item.n
    elif isinstance(item, ast.Name):
        result = VariableKey(item=item)
        constants_lookup = {
            'True': True,
            'False': False,
            'None': None,
        }
        return constants_lookup.get(
            result.name,
            result,
        )
    elif (not PY33) and isinstance(item, ast.NameConstant):
        # None, True, False are nameconstants in python3, but names in 2
        return item.value
    else:
        return UnhandledKeyType()
项目:blackmamba    作者:zrzka    | 项目源码 | 文件源码
def convert_to_value(item):
    if isinstance(item, ast.Str):
        return item.s
    elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
        return item.s
    elif isinstance(item, ast.Tuple):
        return tuple(convert_to_value(i) for i in item.elts)
    elif isinstance(item, ast.Num):
        return item.n
    elif isinstance(item, ast.Name):
        result = VariableKey(item=item)
        constants_lookup = {
            'True': True,
            'False': False,
            'None': None,
        }
        return constants_lookup.get(
            result.name,
            result,
        )
    elif (not PY33) and isinstance(item, ast.NameConstant):
        # None, True, False are nameconstants in python3, but names in 2
        return item.value
    else:
        return UnhandledKeyType()
项目:py_gpumap    作者:ipachev    | 项目源码 | 文件源码
def visit_Assign(self, node):
        if len(node.targets) > 1:
            raise SyntaxError("GPUMAP: multiple assignment not supported")

        target_types = map(lambda target: type(target), node.targets)
        if tuple in target_types or list in target_types:
            raise SyntaxError("GPUMAP: No unpacking allowed")


        target = node.targets[0]
        # assignment into object field
        output = ""

        value = self.visit(node.value)

        # assignment into variable
        if isinstance(target, _ast.Name):
            # assignment into new variable
            # not sure about the type just yet..

            # see if it's a primitive
            if target.id not in self.local_vars:
                # binops and boolops return primitives
                if isinstance(node.value, _ast.Num) or isinstance(node.value, _ast.Compare) or isinstance(node.value, _ast.BinOp) \
                        or isinstance(node.value, _ast.BoolOp) or isinstance(node.value, _ast.NameConstant):
                    output += "auto "

                # check if referenced list contains primitives
                elif isinstance(node.value, _ast.Subscript):
                    list_name = value[:value.find("[")]
                    try:
                        idx = self.func_repr.args.index(list_name)
                        t = self.func_repr.arg_types[idx]
                        item_type = t[t.find("<") + 1: t.find(">")]
                        if item_type in map(lambda t: t.__name__, primitive_map.keys()):
                            output += "auto "
                        else:
                            output += "auto&& "
                    except:
                        raise RuntimeError("THIS SHOULD NEVER HAPPEN")
                else:
                    # check if it's an existing variable
                    try:
                        idx = self.func_repr.args.index(value)
                        t = self.func_repr.arg_types[idx]
                        if t in primitive_map:
                            output += "auto "
                        else:
                            output += "auto&& "
                    except ValueError:
                        output += "auto&& "
                self.local_vars[target.id] = None
        output += self.visit(target)
        output += " = " + value
        return output
项目:bandit-ss    作者:zeroSteiner    | 项目源码 | 文件源码
def _get_literal_value(self, literal):
        '''Utility function to turn AST literals into native Python types

        :param literal: The AST literal to convert
        :return: The value of the AST literal
        '''
        if isinstance(literal, _ast.Num):
            literal_value = literal.n

        elif isinstance(literal, _ast.Str):
            literal_value = literal.s

        elif isinstance(literal, _ast.List):
            return_list = list()
            for li in literal.elts:
                return_list.append(self._get_literal_value(li))
            literal_value = return_list

        elif isinstance(literal, _ast.Tuple):
            return_tuple = tuple()
            for ti in literal.elts:
                return_tuple = return_tuple + (self._get_literal_value(ti),)
            literal_value = return_tuple

        elif isinstance(literal, _ast.Set):
            return_set = set()
            for si in literal.elts:
                return_set.add(self._get_literal_value(si))
            literal_value = return_set

        elif isinstance(literal, _ast.Dict):
            literal_value = dict(zip(literal.keys, literal.values))

        elif isinstance(literal, _ast.Ellipsis):
            # what do we want to do with this?
            literal_value = None

        elif isinstance(literal, _ast.Name):
            literal_value = literal.id

        # NOTE(sigmavirus24): NameConstants are only part of the AST in Python
        # 3. NameConstants tend to refer to things like True and False. This
        # prevents them from being re-assigned in Python 3.
        elif six.PY3 and isinstance(literal, _ast.NameConstant):
            literal_value = str(literal.value)

        # NOTE(sigmavirus24): Bytes are only part of the AST in Python 3
        elif six.PY3 and isinstance(literal, _ast.Bytes):
            literal_value = literal.s

        else:
            literal_value = None

        return literal_value