Python ast 模块,Index() 实例源码

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

项目:py2rb    作者:naitoh    | 项目源码 | 文件源码
def visit_Subscript(self, node):
        self._is_string_symbol = False
        name = self.visit(node.value)
        if isinstance(node.slice, (ast.Index)):
            for arg in self._function_args:
                if arg == ("**%s" % name):
                    self._is_string_symbol = True
            index = self.visit(node.slice)
            self._is_string_symbol = False
            return "%s[%s]" % (name, index)
            #return "%s%s" % (name, index)
        else:
            # ast.Slice
            index = self.visit(node.slice)
            if ',' in index:
                """ [See visit_Slice]
                <Python> [8, 9, 10, 11, 12, 13, 14][1:6:2]
                <Ruby>   [8, 9, 10, 11, 12, 13, 14][1...6].each_slice(2).map(&:first)
                """
                indexs = index.split(',')
                return "%s[%s].%s" % (name, indexs[0], indexs[1])
            else:
                return "%s[%s]" % (name, index)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_subscript(self):
        sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
                            ast.Load())
        self.expr(sub, "must have Load context")
        x = ast.Name("x", ast.Load())
        sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
                            ast.Load())
        self.expr(sub, "must have Load context")
        s = ast.Name("x", ast.Store())
        for args in (s, None, None), (None, s, None), (None, None, s):
            sl = ast.Slice(*args)
            self.expr(ast.Subscript(x, sl, ast.Load()),
                      "must have Load context")
        sl = ast.ExtSlice([])
        self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
        sl = ast.ExtSlice([ast.Index(s)])
        self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
项目:tabkit    作者:yandex-tabkit    | 项目源码 | 文件源码
def make_subscript(varname, idx, ctx=None, lineno=0, col_offset=0):
    ctx = ctx or ast.Load()
    return ast.Subscript(
        value = ast.Name(
            id = varname,
            ctx = ast.Load(),
            lineno = lineno,
            col_offset = col_offset,
        ),
        slice = ast.Index(
            value = ast.Num(
                n = idx,
                lineno = lineno,
                col_offset = col_offset,
            ),
            lineno = lineno,
            col_offset = col_offset,
        ),
        ctx = ctx,
        lineno = lineno,
        col_offset = col_offset,
    )
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_subscript(self):
        sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
                            ast.Load())
        self.expr(sub, "must have Load context")
        x = ast.Name("x", ast.Load())
        sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
                            ast.Load())
        self.expr(sub, "must have Load context")
        s = ast.Name("x", ast.Store())
        for args in (s, None, None), (None, s, None), (None, None, s):
            sl = ast.Slice(*args)
            self.expr(ast.Subscript(x, sl, ast.Load()),
                      "must have Load context")
        sl = ast.ExtSlice([])
        self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
        sl = ast.ExtSlice([ast.Index(s)])
        self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
项目:hatlog    作者:alehander42    | 项目源码 | 文件源码
def flatten_subscript(self, node):
        value = self.flatten(node.value)
        node_type = self.new_type()
        if isinstance(node.slice, ast.Index):
            index = self.flatten(node.slice.value)
            self.nodes.append(('z_index', [value, index], node_type))
        else:
            lower = self.flatten(node.slice.lower) if node.slice.lower else None
            upper = self.flatten(node.slice.upper) if node.slice.upper else None
            if lower and upper is None:
                upper = lower
            elif lower is None and upper:
                lower = upper
            else:
                raise ValueError('hatlog expects only slice like [:x], [x:] or [x:y]')
            self.nodes.append(('z_slice', [value, lower, upper], node_type))
        return node_type
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_subscript(self):
        sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
                            ast.Load())
        self.expr(sub, "must have Load context")
        x = ast.Name("x", ast.Load())
        sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
                            ast.Load())
        self.expr(sub, "must have Load context")
        s = ast.Name("x", ast.Store())
        for args in (s, None, None), (None, s, None), (None, None, s):
            sl = ast.Slice(*args)
            self.expr(ast.Subscript(x, sl, ast.Load()),
                      "must have Load context")
        sl = ast.ExtSlice([])
        self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
        sl = ast.ExtSlice([ast.Index(s)])
        self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
项目:metapensiero.pj    作者:azazel75    | 项目源码 | 文件源码
def Call_new(t, x):
    """Translate ``Foo(...)`` to ``new Foo(...)`` if function name starts
    with a capital letter.
    """
    def getNameString(x):
        if isinstance(x, ast.Name):
            return x.id
        elif isinstance(x, ast.Attribute):
            return str(x.attr)
        elif isinstance(x, ast.Subscript):
            if isinstance(x.slice, ast.Index):
                return str(x.slice.value)

    NAME_STRING = getNameString(x.func)

    if (NAME_STRING and re.search(r'^[A-Z]', NAME_STRING)):
        # TODO: generalize args mangling and apply here
        # assert not any([x.keywords, x.starargs, x.kwargs])
        subj = x
    elif isinstance(x.func, ast.Name) and x.func.id == 'new':
        subj = x.args[0]
    else:
        subj = None
    if subj:
        return Call_default(t, subj, operator='new ')
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_Subscript(self, ctx, e):
        slice_ = e.slice 
        if isinstance(slice_, ast.Ellipsis):
            raise _errors.TyError("stringing slice cannot be an Ellipsis.", e)
        elif isinstance(slice_, ast.ExtSlice):
            raise _errors.TyError("stringing slice can only have one dimension.", e)
        elif isinstance(slice_, ast.Index):
            ctx.ana(slice_.value, _numeric.num)
        else: # if isinstance(slice_, ast.Slice):
            lower, upper, step = slice_.lower, slice_.upper, slice_.step
            if lower is not None:
                ctx.ana(lower, _numeric.num)
            if upper is not None:
                ctx.ana(upper, _numeric.num)
            if not _is_None(step):
                ctx.ana(step, _numeric.num)
        return self
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def translate_Subscript(self, ctx, e):
        translation = astx.copy_node(e)
        translation.value = ctx.translate(e.value)
        slice_ = e.slice
        slice_translation = astx.copy_node(slice_)
        if isinstance(slice_, ast.Index):
            slice_translation.value = ctx.translate(slice_.value)
        else:
            lower, upper, step = slice_.lower, slice_.upper, slice_.step
            slice_translation.lower = ctx.translate(lower) if lower is not None else None
            slice_translation.upper = ctx.translate(upper) if upper is not None else None
            if not _is_None(step):
                slice_translation.step = ctx.translate(step)
            else:
                slice_translation.step = None
        translation.slice = slice_translation
        return translation
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def translate_pat_Call_constructor(self, ctx, pat, scrutinee_trans):
        lbl = pat.func.id

        tag_loc = ast.Subscript(
            value=scrutinee_trans,
            slice=ast.Index(value=ast.Num(n=0)))
        lbl_condition = ast.Compare(
            left=tag_loc,
            ops=[ast.Eq()],
            comparators=[ast.Str(s=lbl)])

        arg = pat.args[0]
        arg_scrutinee = ast.Subscript(
            value=scrutinee_trans,
            slice=ast.Index(value=ast.Num(n=1)))
        arg_condition, binding_translations = ctx.translate_pat(arg, arg_scrutinee)

        condition = ast.BoolOp(
            op=ast.And(),
            values=[lbl_condition, arg_condition])

        return condition, binding_translations
项目:tailbiter    作者:darius    | 项目源码 | 文件源码
def visit_Subscript(self, t):
        self(t.value)
        if isinstance(t.slice, ast.Index):
            if   isinstance(t.ctx, ast.Load):  pass
            elif isinstance(t.ctx, ast.Store): pass
            else: assert False, "Only loads and stores are supported: %r" % (t,)
            self(t.slice.value)
        else:
            assert False, "Only simple subscripts are supported: %r" % (t,)
项目:Lyra    作者:caterinaurban    | 项目源码 | 文件源码
def visit_Subscript(self, node, types=None, typ=None):
        pp = ProgramPoint(node.lineno, node.col_offset)
        if isinstance(node.slice, ast.Index):
            target = self.visit(node.value, types, typ)
            key = self.visit(node.slice.value, types, typ)
            return SubscriptionAccess(pp, target, key)
        elif isinstance(node.slice, ast.Slice):
            return SliceStmt(pp, self._ensure_stmt_visit(node.value, pp, *args, **kwargs),
                             self._ensure_stmt_visit(node.slice.lower, pp, *args, **kwargs),
                             self._ensure_stmt_visit(node.slice.step, pp, *args, **kwargs) if node.slice.step else None,
                             self._ensure_stmt_visit(node.slice.upper, pp, *args, **kwargs))
        else:
            raise NotImplementedError(f"The statement {str(type(node.slice))} is not yet translatable to CFG!")
项目:fz    作者:llllllllll    | 项目源码 | 文件源码
def __getitem__(self, key):
        keyname, key, constants = _normalize_arg(key, self._constants)
        return __class__(
            '%s[%s]' % (self._pname, keyname),
            ast.Subscript(
                value=self._tree,
                slice=ast.Index(value=key),
                ctx=ast.Load(),
            ),
            constants,
        )
项目:fatoptimizer    作者:vstinner    | 项目源码 | 文件源码
def visit_Subscript(self, node):
        if not self.config.constant_folding:
            return

        if isinstance(node.slice, ast.Slice):
            new_node = self.subscript_slice(node)
            if new_node is not None:
                return new_node

        elif isinstance(node.slice, ast.Index):
            new_node = self.subscript_index(node)
            if new_node is not None:
                return new_node
项目:dfkernel    作者:dataflownb    | 项目源码 | 文件源码
def visit_Subscript(self, node):
        super().generic_visit(node)
        if (isinstance(node.value, ast.Name) and
                    node.value.id == "Out" and
                isinstance(node.slice, ast.Index) and
                isinstance(node.slice.value, ast.Name)):
            return ast.copy_location(ast.Subscript(
                value=ast.Name(id=node.value.id, ctx=node.value.ctx),
                slice=ast.Index(value=ast.Str(s=node.slice.value.id)),
                ctx=node.ctx), node)
        return node
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def subscript_to_tuple(subscript):
    '''Convert a subscripted name of the form Name[(i1, ..., in)] to a
    tuple ('Name', i1, ..., in).

    '''
    def err():
        raise ValueError('Unexpected kind of slice: {}'.format(astunparse.unparse(subscript)))

    # Get subscript name.
    if isinstance(subscript.value, ast.Name):
        name = subscript.value.id
    else:
        err()

    # Get indices.
    if isinstance(subscript.slice, ast.Index):
        if isinstance(subscript.slice.value, ast.Num):
            indices = [subscript.slice.value]
        elif isinstance(subscript.slice.value, ast.Tuple):
            indices = subscript.slice.value.elts
        else:
            err()
    else:
        err()

    # Convert indices to python numbers.
    int_indices = []
    for i in indices:
        if isinstance(i, ast.Num):
            int_indices.append(i.n)
        else:
            err()

    return tuple([name] + int_indices)
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def extend_subscript_for_input(node, extension):
    if isinstance(node.slice, ast.Index):
        node = copy.deepcopy(node)
        idx = node.slice.value
        if isinstance(idx, ast.Tuple):
            new_idx = ast.Tuple([extension] + idx.elts, ast.Load())
        else:
            new_idx = ast.Tuple([extension, idx], ast.Load())
        node.slice.value = new_idx
    else:
        raise Exception("Unhandled node indexing: '%s'" % (unparse(node).rstrip()))
    return node
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def add_input_indices(root, input_vars, index_var):
    class AddInputIndicesVisitor(ast.NodeTransformer):
        def visit_Subscript(self, node):
            if get_var_name(node) in input_vars:
                return extend_subscript_for_input(node, index_var)
            return node

        def visit_Name(self, node):
            if node.id in input_vars:
                return ast.Subscript(node, ast.Index(index_var), node.ctx)
            return node

    vis = AddInputIndicesVisitor()
    root = vis.visit(root)
    return ast.fix_missing_locations(root)
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def generate_io_stmt(input_idx, var_name, value, func_name):
    if value is None:
        return []
    elif isinstance(value, list):
        return [ast.Expr(
                    ast.Call(
                        ast.Attribute(
                            ast.Subscript(
                                ast.Name(var_name, ast.Load()),
                                ast.Index(
                                    ast.Tuple([ast.Num(input_idx),
                                               ast.Num(val_idx)],
                                               ast.Load())),
                                ast.Load()),
                            func_name,
                            ast.Load()),
                        [ast.Num(val)],
                        [], None, None))
                for val_idx, val in enumerate(value)
                if val is not None]
    else:
        return [ast.Expr(
                    ast.Call(
                        ast.Attribute(
                            ast.Subscript(
                                ast.Name(var_name, ast.Load()),
                                ast.Index(ast.Num(input_idx)),
                                ast.Load()),
                            func_name,
                            ast.Load()),
                        [ast.Num(value)],
                        [], None, None))]
项目:TerpreT    作者:51alg    | 项目源码 | 文件源码
def get_index(subscript_node):
    index_node = cast(subscript_node.slice, ast.Index)
    return name_or_number(index_node.value)
项目:pydiatra    作者:jwilk    | 项目源码 | 文件源码
def visit_Subscript(self, node):
        func = None
        if isinstance(node.value, ast.Call):
            call = node.value
            if isinstance(call.func, ast.Name):
                func = call.func.id
            elif isinstance(node.value.func, ast.Attribute):
                func = call.func.attr
        if func == 'mkstemp':
            if isinstance(node.slice, ast.Index):
                index = node.slice.value
                if isinstance(index, ast.Num) and index.n == 1:
                    yield self.tag(node, 'mkstemp-file-descriptor-leak')
        for t in self.generic_visit(node):
            yield t
项目:hazzy    作者:KurtJacobson    | 项目源码 | 文件源码
def __init__(self, operators=None, functions=None, names=None):
        """
            Create the evaluator instance.  Set up valid operators (+,-, etc)
            functions (add, random, get_val, whatever) and names. """

        if not operators:
            operators = DEFAULT_OPERATORS
        if not functions:
            functions = DEFAULT_FUNCTIONS
        if not names:
            names = DEFAULT_NAMES

        self.operators = operators
        self.functions = functions
        self.names = names

        self.nodes = {
            ast.Num: self._eval_num,
            ast.Str: self._eval_str,
            ast.Name: self._eval_name,
            ast.UnaryOp: self._eval_unaryop,
            ast.BinOp: self._eval_binop,
            ast.BoolOp: self._eval_boolop,
            ast.Compare: self._eval_compare,
            ast.IfExp: self._eval_ifexp,
            ast.Call: self._eval_call,
            ast.keyword: self._eval_keyword,
            ast.Subscript: self._eval_subscript,
            ast.Attribute: self._eval_attribute,
            ast.Index: self._eval_index,
            ast.Slice: self._eval_slice,
        }

        # py3k stuff:
        if hasattr(ast, 'NameConstant'):
            self.nodes[ast.NameConstant] = self._eval_nameconstant
        elif isinstance(self.names, dict) and "None" not in self.names:
            self.names["None"] = None
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def visit_Subscript(self, node):
        pp = ProgramPoint(node.lineno, node.col_offset)
        if isinstance(node.slice, ast.Index):
            return IndexStmt(pp, self._ensure_stmt_visit(node.value, pp), self._ensure_stmt_visit(node.slice.value, pp))
        elif isinstance(node.slice, ast.Slice):
            return SliceStmt(pp, self._ensure_stmt_visit(node.value, pp),
                             self._ensure_stmt_visit(node.slice.lower, pp),
                             self._ensure_stmt_visit(node.slice.step, pp) if node.slice.step else None,
                             self._ensure_stmt_visit(node.slice.upper, pp))
        else:
            raise NotImplementedError(f"The statement {str(type(node.slice))} is not yet translatable to CFG!")
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def infer_subscript(node, context, solver):
    """Infer expressions like: x[1], x["a"], x[1:2], x[1:].
    Where x may be: a list, dict, tuple, str

    Attributes:
        node: the subscript node to be inferred
    """

    indexed_type = infer(node.value, context, solver)

    if isinstance(node.slice, ast.Index):
        index_type = infer(node.slice.value, context, solver)
        result_type = solver.new_z3_const("index")
        solver.add(axioms.index(indexed_type, index_type, result_type, solver.z3_types),
                   fail_message="Indexing in line {}".format(node.lineno))
        return result_type
    else:  # Slicing
        # Some slicing may contain 'None' bounds, ex: a[1:], a[::]. Make Int the default type.
        lower_type = upper_type = step_type = solver.z3_types.int
        if node.slice.lower:
            lower_type = infer(node.slice.lower, context, solver)
        if node.slice.upper:
            upper_type = infer(node.slice.upper, context, solver)
        if node.slice.step:
            step_type = infer(node.slice.step, context, solver)

        result_type = solver.new_z3_const("slice")

        solver.add(axioms.slicing(lower_type, upper_type, step_type, indexed_type, result_type, solver.z3_types),
                   fail_message="Slicing in line {}".format(node.lineno))
        return result_type
项目:insights-core    作者:RedHatInsights    | 项目源码 | 文件源码
def _build_subscript(value1, value2):
    return Subscript(
        value=value1,
        slice=Index(value=value2),
        ctx=Load())
项目:metapensiero.pj    作者:azazel75    | 项目源码 | 文件源码
def Subscript_default(t, x):
    assert isinstance(x.slice, ast.Index)
    v = x.slice.value
    if isinstance(v, ast.UnaryOp) and isinstance(v.op, ast.USub):
        return JSSubscript(
            JSCall(JSAttribute(x.value, 'slice'), [v]),
            JSNum(0))
    return JSSubscript(x.value, v)
项目:peval    作者:fjarri    | 项目源码 | 文件源码
def handle_Subscript(state, node, ctx):
        state, value_result = _peval_expression(state, node.value, ctx)
        state, slice_result = _peval_expression(state, node.slice, ctx)
        if is_known_value(value_result) and is_known_value(slice_result):
            success, elem = try_call_method(
                value_result.value, '__getitem__', args=(slice_result.value,))
            if success:
                return state, KnownValue(value=elem)

        state, new_value = map_reify(state, value_result)
        state, new_slice = map_reify(state, slice_result)
        if type(new_slice) not in (ast.Index, ast.Slice, ast.ExtSlice):
            new_slice = ast.Index(value=new_slice)
        return state, replace_fields(node, value=new_value, slice=new_slice)
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def init_idx(cls, idx):
        if idx != ():
            raise typy.TypeFormationError("Index of float_ type must be ().")
        return idx
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def syn_Subscript(self, ctx, e):
        slice_ = e.slice
        if not isinstance(slice_, ast.Index):
            raise _errors.TyError("Must provide a single label.", slice_)
        value = slice_.value
        label = _read_label(value)
        try:
            ty = self.idx[label]
        except KeyError:
            raise _errors.TyError("Cannot project component labeled " + str(label), e)
        value.label = label
        return ty
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def _labeled_translation(idx_mapping, arg_translation):
    lambda_translation = ast.Lambda(
        args=ast.arguments(
            args=[ast.Name(id='x', ctx=ast.Param())],
            vararg=None,
            kwarg=None,
            defaults=[]),
        body=ast.Tuple(
            elts=list(
                ast.Subscript(
                    value=ast.Name(
                        id='x',
                        ctx=ast.Load()),
                    slice=ast.Index(
                        value=ast.Num(n=n)),
                    ctx=ast.Load())
                for n in idx_mapping
            ),
            ctx=ast.Load()
        )
    )
    return ast.Call(
        func=lambda_translation,
        args=[arg_translation],
        keywords=[],
        starargs=[],
        kwargs=None
    )
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def _is_unascribed_match_head(cls, e):
        if isinstance(e, ast.Subscript):
            value = e.value
            if isinstance(value, ast.Name) and value.id == 'match':
                slice = e.slice
                if isinstance(slice, ast.Index):
                    e.is_match_head = True
                    e.scrutinee = slice.value
                    e.asc_ast = None
                    return True
                else:
                    raise _errors.TyError("Invalid match scrutinee.", e)
        return False
项目:pseudo-python    作者:alehander42    | 项目源码 | 文件源码
def _translate_subscript(self, value, slice, ctx, location):
        value_node = self._translate_node(value)
        value_general_type = self._general_type(value_node['pseudo_type'])
        if value_general_type not in INDEXABLE_TYPES:
            raise type_check_error('pseudo-python can use [] only on String, List, Dictionary or Tuple',
                location, self.lines[location[0]],
                wrong_type=value_node['pseudo_type'])

        if isinstance(slice, ast.Index):
            z = self._translate_node(slice.value)
            if value_general_type in ['String', 'List', 'Tuple'] and z['pseudo_type'] != 'Int':
                raise PseudoPythonTypeCheckError('a non int index for %s %s' % (value_general_type, z['pseudo_type']))

            if value_general_type == 'Dictionary' and z['pseudo_type'] != value_node['pseudo_type'][1]:
                raise PseudoPythonTypeCheckError('a non %s index for %s %s' % (value_node['pseudo_type'][1], value_general_type, z['pseudo_type']))

            if value_general_type == 'String':
                pseudo_type = 'String'
            elif value_general_type == 'List' or value_general_type == 'Array':
                pseudo_type = value_node['pseudo_type'][1]
            elif value_general_type == 'Tuple':
                if z['type'] != 'int':
                    raise PseudoPythonTypeCheckError('pseudo-python can support only literal int indices of a heterogenous tuple ' +
                                                     'because otherwise the index type is not predictable %s %s ' % (serialize_type(value_node['pseudo_type']), z['type']))

                elif z['value'] > len(value_node['pseudo_type']) - 2:
                    raise PseudoPythonTypeCheckError('%s has only %d elements' % serialize_type(value_node['pseudo_type']), len(value_node['pseudo_type']))

                pseudo_type = value_node['pseudo_type'][z['value'] + 1]

            else:
                pseudo_type = value_node['pseudo_type'][2]

            if 'special' in value_node: # sys.argv[index]
                if z['pseudo_type'] != 'Int':
                    raise type_check_error('pseudo-python supports only int indices for sys.argv',
                        location, self.lines[location[0]],
                        wrong_type=z['pseudo_type'])
                return {
                    'type': 'standard_call',
                    'namespace': 'system',
                    'function': 'index',
                    'args': [z],
                    'pseudo_type': 'String'
                }
            result = {
                'type': 'index',
                'sequence': value_node,
                'index': z,
                'pseudo_type': pseudo_type
            }
            if result in self._tuple_assigned:
                j = z.get('value', z.get('name', z.get('attr', '_x')))
                k = value_node.get('value', value_node.get('name', value_node.get('attr', '_y')))
                # i kno c:
                if not any(a[0] == '_old_%s_%s' % (j, k) for a in self._tuple_used):
                    self._tuple_used.append(('_old_%s_%s' % (j, k), result))
                result = {'type': 'local', 'name': '_old_%s_%s' % (j, k), 'pseudo_type': pseudo_type}
            return result
        else:
            return self._translate_slice(receiver=value_node, upper=slice.upper, step=slice.step, lower=slice.lower, location=location)
项目:pseudo-python    作者:alehander42    | 项目源码 | 文件源码
def _hint(self, x):
        if isinstance(x, (ast.Name, ast.Str)):
            name = x.id if isinstance(x, ast.Name) else x.s
            if name in BUILTIN_SIMPLE_TYPES:
                return BUILTIN_SIMPLE_TYPES[name]
            elif name in self.type_env.top.values:
                return name
        elif isinstance(x, ast.Subscript) and isinstance(x.value, (ast.Name, ast.Str)):
            name = x.value.id if isinstance(x.value, ast.Name) else x.value.s
            if name in ['List', 'Set', 'Dict', 'Tuple', 'Callable']:
                if name not in self._typing_imports:
                    raise translation_error('please add\nfrom typing import %s on top to use it\n' % name, (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno])
                if not isinstance(x.slice, ast.Index):
                    raise translation_error('invalid index', (x.value.lineno, x.value.col_offset), self.lines[x.lineno])
                index = x.slice.value
                if name in ['List', 'Set']:
                    if not isinstance(index, (ast.Name, ast.Subscript)):
                        raise type_check_error('%s expects one valid generic arguments' % name, (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno])
                    return [name, self._hint(index)]
                elif name == 'Tuple':
                    if not isinstance(index, ast.Tuple) or any(not isinstance(y, (ast.Name, ast.Subscript)) for y in index.elts):
                        raise type_check_error('Tuple expected valid generic arguments', (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno])
                    return ['Tuple'] + [self._hint(y) for y in index.elts]
                elif name == 'Dict':
                    if not isinstance(index, ast.Tuple) or len(index.elts) != 2 or not isinstance(index.elts[1], (ast.Name, ast.Subscript)):
                        raise type_check_error('Dict expected 2 valid generic arguments', (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno])
                    if not isinstance(index.elts[0], ast.Name) or index.elts[0].id not in KEY_TYPES:
                        raise type_check_error('type not supported as a dictionary key type', (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno],
                            suggestions='only those types are supported:\n  %s  ' % '\n  '.join(PSEUDO_KEY_TYPES),
                            right='  Dict[str, List[int]]',
                            wrong='  Dict[List[int], Tuple[int]]')
                    return ['Dictionary', self._hint(index.elts[0]), self._hint(index.elts[1])]
                else:
                    if not isinstance(index, ast.Tuple) or len(index.elts) != 2 or not isinstance(index.elts[0], ast.List) or not isinstance(index.elts[1], (ast.Name, ast.Subscript)) or any(not isinstance(y, (ast.Name, ast.Subscript)) for y in index.elts[0].elts):
                        raise type_check_error('Callable expected valid generic arguments of the form Callable[[<arg_type>, <arg_type>*], <return>]', (x.value.lineno, x.value.col_offset), self.lines[x.value.lineno])
                    return ['Function'] + [self._hint(y) for y in index.elts[0].elts] + [self._hint(index.elts[1])]
        raise type_check_error('type not recognized',
            (x.lineno, x.col_offset), self.lines[x.lineno],
            suggestions='supported type hints are:\n  ' + '\n  '.join(
                ['int', 'float', 'str', 'bool',
                 'List[<element_hint>]', 'Dict[<key_hint>, <value_hint>]', 'Tuple[<element_hints>..]', 'Set[<element_hint>]', 'Callable[[<arg_hint>*], <return_hin>]'
                 'your class e.g. Human']))
项目:py2rb    作者:naitoh    | 项目源码 | 文件源码
def visit_Delete(self, node):
        """
        Delete(expr* targets)
        """
        id = ''
        num = ''
        key = ''
        slice = ''
        attr = ''
        for stmt in node.targets:
            if isinstance(stmt, (ast.Name)):
                id = self.visit(stmt)
            elif isinstance(stmt, (ast.Subscript)):
                if isinstance(stmt.value, (ast.Name)):
                    id = self.visit(stmt.value)
                if isinstance(stmt.slice, (ast.Index)):
                    if isinstance(stmt.slice.value, (ast.Str)):
                        key = self.visit(stmt.slice)
                    if isinstance(stmt.slice.value, (ast.Num)):
                        num = self.visit(stmt.slice)
                if isinstance(stmt.slice, (ast.Slice)):
                    slice = self.visit(stmt.slice)
            elif isinstance(stmt, (ast.Attribute)):
                if isinstance(stmt.value, (ast.Name)):
                    id = self.visit(stmt.value)
                attr = stmt.attr
        if num != '':
            """ <Python> del foo[0]
                <Ruby>   foo.delete_at[0] """
            self.write("%s.delete_at(%s)" % (id, num))
        elif key != '':
            """ <Python> del foo['hoge']
                <Ruby>   foo.delete['hoge'] """
            self.write("%s.delete(%s)" % (id, key))
        elif slice != '':
            """ <Python> del foo[1:3]
                <Ruby>   foo.slise!(1...3) """
            self.write("%s.slice!(%s)" % (id, slice))
        elif attr != '':
            """ <Python> del foo.bar
                <Ruby>   foo.instance_eval { remove_instance_variable(:@bar) } """
            self.write("%s.instance_eval { remove_instance_variable(:@%s) }" % (id, attr))
        else:
            """ <Python> del foo
                <Ruby>   foo = nil """
            self.write("%s = nil" % (id))
项目:py2rb    作者:naitoh    | 项目源码 | 文件源码
def visit_Assign(self, node):
        """
        Assign(expr* targets, expr value)
        """
        assert len(node.targets) == 1
        target = node.targets[0]
        #~ if self._class_name:
            #~ target = self._class_name + '.' + target
        # ast.Tuple, ast.List, ast.*
        value = self.visit(node.value)
        #if isinstance(node.value, (ast.Tuple, ast.List)):
        #    value = "[%s]" % self.visit(node.value)
        #else:
        #    value = self.visit(node.value)
        if isinstance(target, (ast.Tuple, ast.List)):
            # multiassign.py
            """ x, y, z = [1, 2, 3] """
            x = [self.visit(t) for t in target.elts]
            self.write("%s = %s" % (','.join(x), value))
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Index):
            # found index assignment # a[0] = xx
            #self.write("%s%s = %s" % (self.visit(target.value), # a[0] = xx
            name = self.visit(target.value)
            for arg in self._function_args:
                if arg == ("**%s" % name):
                    self._is_string_symbol = True
            self.write("%s[%s] = %s" % (name, self.visit(target.slice), value))
            self._is_string_symbol = False
        elif isinstance(target, ast.Subscript) and isinstance(target.slice, ast.Slice):
            # found slice assignmnet
            self.write("%s[%s...%s] = %s" % (self.visit(target.value),
                self.visit(target.slice.lower), self.visit(target.slice.upper),
                value))
        else:
            if isinstance(target, ast.Name):
                var = self.visit(target)
                if not (var in self._scope):
                    self._scope.append(var)
                if isinstance(node.value, ast.Call):
                    if isinstance(node.value.func, ast.Name):
                        if node.value.func.id in self._class_names:
                            self._classes_self_functions_args[var] = self._classes_self_functions_args[node.value.func.id]
                # set lambda functions
                if isinstance(node.value, ast.Lambda):
                    self._lambda_functions.append(var)
                self.write("%s = %s" % (var, value))
            elif isinstance(target, ast.Attribute):
                var = self.visit(target)
                """ [instance variable] : 
                <Python> self.foo = hoge
                <Ruby>   @foo     = hoge
                """
                if var == 'self':
                    self.write("@%s = %s" % (str(target.attr), value))
                    self._class_self_variables.append(str(target.attr))
                else:
                    self.write("%s = %s" % (var, value))
            else:
                raise RubyError("Unsupported assignment type")