Ejemplo n.º 1
0
def test_ext_slice():
    result = match_ast(pattern, parse('x[0, 1, 2]'))
    actual = deslicify(result['slice'])

    check = match_ast(
        Tuple(
            elts=[Num(n=set(['n1'])),
                  Num(n=set(['n2'])),
                  Num(n=set(['n3']))]), actual)
    assert check == {'n1': 0, 'n2': 1, 'n3': 2}
Ejemplo n.º 2
0
def test_slice():
    result = match_ast(pattern, parse('x[:44:3]'))
    actual = deslicify(result['slice'])

    check = match_ast(
        Call(func=Attribute(value=Name(id='__builtins__', ctx=Load()),
                            attr='slice',
                            ctx=Load()),
             args=[
                 name_constant(value=set(['start'])),
                 Num(n=set(['stop'])),
                 Num(n=set(['step']))
             ]), actual)
    assert check == {
        'start': None,
        'stop': 44,
        'step': 3
    } or check == {
        'start': 'None',
        'stop': 44,
        'step': 3
    }
Ejemplo n.º 3
0
def test_skips_existent():
    module = check_mod()
    with Names(module, prefix='') as subj:
        assert subj.dotted('test', 'mod', 'func_name') == 'func_name0'
        assert subj.dotted('test', 'mod2', 'func_name') == 'func_name1'
        assert subj.dotted('test', 'mod', 'func_name') == 'func_name0'
        assert subj.dotted('test', 'mod2', 'func_name') == 'func_name1'

    expect = ImportFrom(
        module=set(['mod']),
        names=[alias(name=set(['name']), asname=set(['asname']))])
    print(dump(module))
    assert match_ast(expect, module.body[0]) == {
        'asname': 'func_name0',
        'mod': 'test.mod',
        'name': 'func_name'
    }
    assert match_ast(expect, module.body[1]) == {
        'asname': 'func_name1',
        'mod': 'test.mod2',
        'name': 'func_name'
    }
Ejemplo n.º 4
0
    def visit_Expr(self, node):
        '''Rewrite foo.append(...) as foo = invoke(foo, 'append', ...).

        This depends on the `invoke` method simply knowing which method invocations ought to be saved, thus this is
        tightly coupled to the pyrsistent API.
        '''
        match = match_ast(self._method_pattern, self.visit(node.value))
        if match is None:
            return cl(Expr(value=self.visit(node.value)), node)
        subject = match['subject']
        args = [subject, Str(s=match['method'])] + match['arguments']
        assign = Assign(targets=[Context.set(Store, subject)],
                        value=self.names.call_global(globals.invoke, args, match['keywords']))
        return self.visit_Assign(cl(assign, node))
Ejemplo n.º 5
0
def test_simple_index():
    result = match_ast(pattern, parse('x[42]'))
    actual = deslicify(result['slice'])

    check = match_ast(Num(n=set(['num'])), actual)
    assert check == {'num': 42}