コード例 #1
0
 def visit_For(self, node: ast.For) -> Union[bool, ast.For]:
     enumerate_node = ast.Name(id="enumerate", ctx=ast.Load())
     node_iterable = node.iter.args[0].args[0]
     original_target = node.target
     deleter = AssignDeleter(target=original_target, seq=node_iterable)
     new_body = deleter.visit(node).body or [ast.Pass()]
     elm_target = (deleter.elem_target if deleter.uses_seq else ast.Name(
         id="_", ctx=ast.Store()))
     # for (original_target,elm_target) in enumerate(node_iterable):
     new_node = ast.For(
         target=ast.Tuple(elts=[original_target, elm_target],
                          ctx=ast.Store()),
         iter=ast.Call(func=enumerate_node,
                       args=[node_iterable],
                       keywords=[]),
         body=new_body,
         orelse=node.orelse,
     )
     new_node = ast.fix_missing_locations(ast.copy_location(new_node, node))
     new_node = self.generic_visit(new_node)
     return new_node
コード例 #2
0
def transform_generic(node):
    """Convert a `BinOp` `%` formatted str with a unknown name on the `node.right` to an f-string.

    When `node.right` is a Name since we don't know if it's a single var or a dict so we sniff the string.

    Sniffs the left string for Dict style usage
    e.g. `"val: %(key_name1)s val2: %(key_name2)s" % some_dict`

    else (e.g. `"val: %s" % some_var`):
    Borrow the core logic by injecting the name into a ast.Tuple

    Returns ast.JoinedStr (f-string), bool: str-in-str
    """

    has_dict_str_format = DICT_PATTERN.findall(node.left.s)
    if has_dict_str_format:
        return transform_dict(node), True

    # if it's just a name then pretend it's tuple to use that code
    node.right = ast.Tuple(elts=[node.right])
    return transform_tuple(node), False
コード例 #3
0
ファイル: ddl.py プロジェクト: zzl200012/railgun
def p_exprlist(p):
    '''exprlist : expr
			| exprlist "," expr'''

    if len(p) == 2:
        p[0] = p[1]

    elif len(p) == 4:
        if isinstance(p[1], ast.Tuple):
            p[0] = p[1]
        else:
            p[0] = ast.Tuple(elts=[
                p[1],
            ],
                             ctx=ast.Load(),
                             lineno=p[1].lineno,
                             col_offset=p[1].col_offset)

        p[0].elts.append(p[3])

    return
コード例 #4
0
ファイル: constant_folding.py プロジェクト: mac0ne/pythran
 def to_ast(self, value):
     if (type(value) in (int, long, float, complex)):
         return ast.Num(value)
     elif isinstance(value, bool):
         return ast.Attribute(ast.Name('__builtin__', ast.Load()),
                              'True' if value else 'False', ast.Load())
     elif isinstance(value, str):
         return ast.Str(value)
     elif isinstance(value, list) and len(value) < ConstantFolding.MAX_LEN:
         #SG: unsure whether it Load or something else
         return ast.List(map(self.to_ast, value), ast.Load())
     elif isinstance(value, tuple) and len(value) < ConstantFolding.MAX_LEN:
         return ast.Tuple(map(self.to_ast, value), ast.Load())
     elif isinstance(value, set) and len(value) < ConstantFolding.MAX_LEN:
         return ast.Set(map(self.to_ast, value))
     elif isinstance(value, dict) and len(value) < ConstantFolding.MAX_LEN:
         keys = map(self.to_ast, value.iterkeys())
         values = map(self.to_ast, value.itervalues())
         return ast.Dict(keys, values)
     else:
         raise ConstantFolding.ConversionError()
コード例 #5
0
    def translate(self, string, target):
        """
        >>> from chameleon.tales import test
        >>> test(PathExpr('None')) is None
        True
        """
        string = string.strip()

        if not string:
            return template("target = None", target=target)

        m = self.path_regex.match(string)
        if m is None:
            raise ExpressionError("Not a valid path-expression.", string)

        nocall, path = m.groups()

        # note that unicode paths are not allowed
        parts = str(path).split("/")

        components = self._find_translation_components(parts)

        base = parts[0]

        if not components:
            if len(parts) == 1 and (nocall or base == "None"):
                return template("target = base", base=base, target=target)
            else:
                components = ()

        call = template(
            "traverse(base, econtext, call, path_items)",
            traverse=self.traverser,
            base=load(base),
            call=load(str(not nocall)),
            path_items=ast.Tuple(elts=components),
            mode="eval",
        )

        return template("target = value", target=target, value=call)
コード例 #6
0
 def _gen_test_case(func_name):
     return ast.FunctionDef(
         name=f'test_{func_name}',
         args=ast.arguments(args=[
             ast.arg(arg=argn, annotation=None) for argn in args_names
         ],
                            vararg=None,
                            kwonlyargs=[],
                            kw_defaults=[],
                            kwarg=None,
                            defaults=[]),
         body=[
             ast.Expr(
                 ast.Call(func=ast.Name(id=func_name, ctx=ast.Load()),
                          args=[
                              ast.arg(arg=argn, annotation=None)
                              for argn in args_names
                          ],
                          keywords=[]))
         ],
         decorator_list=[
             ast.Call(
                 func=ast.Attribute(value=ast.Attribute(value=ast.Name(
                     id='pytest', ctx=ast.Load()),
                                                        attr='mark',
                                                        ctx=ast.Load()),
                                    attr='parametrize',
                                    ctx=ast.Load()),
                 args=[
                     ast.Str(s=','.join(args_names)),
                     ast.List(elts=[
                         (_to_entry(args[0]) if len(list(args)) == 1 else
                          ast.Tuple(elts=[_to_entry(arg) for arg in args],
                                    ctx=ast.Load())) for args in args_list
                     ],
                              ctx=ast.Load())
                 ],
                 keywords=[])
         ],
         returns=None)
コード例 #7
0
ファイル: test_context.py プロジェクト: zorroroot/bandit
    def test__get_literal_value(self):
        new_context = context.Context()

        value = ast.Num(42)
        expected = value.n
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.Str('spam')
        expected = value.s
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.List([ast.Str('spam'), ast.Num(42)], ast.Load())
        expected = [ast.Str('spam').s, ast.Num(42).n]
        self.assertListEqual(expected, new_context._get_literal_value(value))

        value = ast.Tuple([ast.Str('spam'), ast.Num(42)], ast.Load())
        expected = (ast.Str('spam').s, ast.Num(42).n)
        self.assertTupleEqual(expected, new_context._get_literal_value(value))

        value = ast.Set([ast.Str('spam'), ast.Num(42)])
        expected = set([ast.Str('spam').s, ast.Num(42).n])
        self.assertSetEqual(expected, new_context._get_literal_value(value))

        value = ast.Dict(['spam', 'eggs'], [42, 'foo'])
        expected = dict(spam=42, eggs='foo')
        self.assertDictEqual(expected, new_context._get_literal_value(value))

        value = ast.Ellipsis()
        self.assertIsNone(new_context._get_literal_value(value))

        value = ast.Name('spam', ast.Load())
        expected = value.id
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.Bytes(b'spam')
        expected = value.s
        self.assertEqual(expected, new_context._get_literal_value(value))

        self.assertIsNone(new_context._get_literal_value(None))
コード例 #8
0
ファイル: nested_for.py プロジェクト: Tadaboody/good_smell
    def visit_For(self, node: ast.For) -> ast.For:
        inner_for: ast.For = node.body[0]
        new_target = ast.Tuple(elts=[node.target, inner_for.target])

        def create_comprehension(for_node: ast.For) -> ast.comprehension:
            return ast.comprehension(target=for_node.target,
                                     iter=for_node.iter,
                                     ifs=[])

        gen_exp = ast.GeneratorExp(
            elt=new_target,
            generators=[
                create_comprehension(node),
                create_comprehension(inner_for)
            ],
        )
        new_for = ast.For(target=new_target,
                          iter=gen_exp,
                          body=inner_for.body,
                          orelse=node.orelse)
        new_for = ast.fix_missing_locations(new_for)
        return new_for
コード例 #9
0
ファイル: list_comp_to_map.py プロジェクト: gouarin/pythran
    def visit_ListComp(self, node):

        if node in self.optimizable_comprehension:

            self.generic_visit(node)

            iterList = []
            varList = []

            for gen in node.generators:
                iterList.append(self.make_Iterator(gen))
                varList.append(ast.Name(gen.target.id, ast.Param()))

            # If dim = 1, product is useless
            if len(iterList) == 1:
                iterAST = iterList[0]
                varAST = ast.arguments([varList[0]], None, None, [])
            else:
                prodName = ast.Attribute(value=ast.Name(id='itertools',
                                                        ctx=ast.Load()),
                                         attr='product',
                                         ctx=ast.Load())

                iterAST = ast.Call(prodName, iterList, [], None, None)
                varAST = ast.arguments([ast.Tuple(varList, ast.Store())], None,
                                       None, [])

            mapName = ast.Attribute(value=ast.Name(id='__builtin__',
                                                   ctx=ast.Load()),
                                    attr='map',
                                    ctx=ast.Load())

            ldBodymap = node.elt
            ldmap = ast.Lambda(varAST, ldBodymap)

            return ast.Call(mapName, [ldmap, iterAST], [], None, None)

        else:
            return self.generic_visit(node)
コード例 #10
0
    def test__get_literal_value(self):
        new_context = context.Context()

        value = ast.Num(42)
        expected = value.n
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.Str("spam")
        expected = value.s
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.List([ast.Str("spam"), ast.Num(42)], ast.Load())
        expected = [ast.Str("spam").s, ast.Num(42).n]
        self.assertListEqual(expected, new_context._get_literal_value(value))

        value = ast.Tuple([ast.Str("spam"), ast.Num(42)], ast.Load())
        expected = (ast.Str("spam").s, ast.Num(42).n)
        self.assertTupleEqual(expected, new_context._get_literal_value(value))

        value = ast.Set([ast.Str("spam"), ast.Num(42)])
        expected = {ast.Str("spam").s, ast.Num(42).n}
        self.assertSetEqual(expected, new_context._get_literal_value(value))

        value = ast.Dict(["spam", "eggs"], [42, "foo"])
        expected = dict(spam=42, eggs="foo")
        self.assertDictEqual(expected, new_context._get_literal_value(value))

        value = ast.Ellipsis()
        self.assertIsNone(new_context._get_literal_value(value))

        value = ast.Name("spam", ast.Load())
        expected = value.id
        self.assertEqual(expected, new_context._get_literal_value(value))

        value = ast.Bytes(b"spam")
        expected = value.s
        self.assertEqual(expected, new_context._get_literal_value(value))

        self.assertIsNone(new_context._get_literal_value(None))
コード例 #11
0
    def add_call(self, external_function_name, args, target_ids):

        arg_ids = np.arange(len(args))
        start_idx = self.variable_names[args[0]]
        targets = []
        args = []
        for target_id in target_ids:
            targets.append(
                ast.Subscript(value=GLOBAL_ARRAY, slice=ast.Index(value=ast.Constant(value=target_id + start_idx)),
                              ctx=ast.Store()))
        for arg_id in arg_ids:
            args.append(
                ast.Subscript(value=GLOBAL_ARRAY, slice=ast.Index(value=ast.Constant(value=arg_id + start_idx)),
                              ctx=ast.Load))
        if len(targets) > 1:
            self.body.append(ast.Assign(targets=[ast.Tuple(elts=targets)],
                                        value=ast.Call(func=ast.Name(id=external_function_name, ctx=ast.Load()),
                                                       args=args, keywords=[]), lineno=0))
        else:
            self.body.append(ast.Assign(targets=[targets[0]],
                                        value=ast.Call(func=ast.Name(id=external_function_name, ctx=ast.Load()),
                                                       args=args, keywords=[]), lineno=0))
コード例 #12
0
    def test_rename_symbols(self):
        node = ast.Tuple([
            ast.Name('a', ast.Load()),
            ast.Name('b', ast.Load()),
            ast.Attribute(ast.Name('b', None), 'c', ast.Store()),
            ast.Attribute(ast.Attribute(ast.Name('b', None), 'c', ast.Load()),
                          'd', None)
        ], None)
        node = qual_names.resolve(node)
        node = ast_util.rename_symbols(
            node, {
                qual_names.QN('a'): qual_names.QN('renamed_a'),
                qual_names.QN('b.c'): qual_names.QN('renamed_b_c'),
            })

        self.assertEqual(node.elts[0].id, 'renamed_a')
        self.assertTrue(isinstance(node.elts[0].ctx, ast.Load))
        self.assertEqual(node.elts[1].id, 'b')
        self.assertEqual(node.elts[2].id, 'renamed_b_c')
        self.assertTrue(isinstance(node.elts[2].ctx, ast.Store))
        self.assertEqual(node.elts[3].value.id, 'renamed_b_c')
        self.assertTrue(isinstance(node.elts[3].value.ctx, ast.Load))
コード例 #13
0
    def _create_repr_method(self):
        """Create the __repr__ method.

            def __repr__(self) -> str:
                return ('Attribute(name=%r, value=%r)' % (self.name, self.value))

        """
        node = ast.FunctionDef(
            name="__repr__",
            args=ast.arguments(
                args=[ast.arg(arg="self", annotation=None)],
                vararg=None,
                kwonlyargs=[],
                kw_defaults=[],
                kwarg=None,
                defaults=[],
            ),
            body=[],
            decorator_list=[],
            returns=ast.Name(id="str"),
        )
        node.body.append(
            ast.Return(value=ast.BinOp(
                left=ast.Str(
                    s="%s(%s)" % (
                        self.resource.name,
                        ", ".join(f"{attr}=%r"
                                  for attr in self.attribute_names),
                    ),
                    kind=None,
                ),
                op=ast.Mod(),
                right=ast.Tuple(elts=[
                    ast.Name(id=f"self.{attr}")
                    for attr in self.attribute_names
                ]),
            )))
        return node
コード例 #14
0
 def visit_For(self, node):
     """
     Converts iteration expressionsinto RangeDim semantic nodes
     """
     index = node.target
     if isinstance(index, ast.Name):
         self.index_vars.add(index.id)
     _range = node.iter
     if isinstance(_range, ast.Call) and _range.func.id == "eachindex":
         loopvars = []
         for dim in self.connections[0].mapping.shape:
             loopvars.append(self._gen_unique_variable())
         nodes = []
         for index, var in enumerate(loopvars):
             nodes.append(
                 ast.For(
                     ast.Name(var, ast.Store()),
                     ast.Call(
                         ast.Name("range_dim", ast.Load()),
                         [_range.args[0], ast.Num(index)], []), [], []))
         index_expr = ast.Tuple(
             [ast.Name(var, ast.Load()) for var in loopvars], ast.Load())
         nodes[-1].body = [
             util.replace_name(node.target, index_expr, s)
             for s in node.body
         ]
         for i in reversed(range(1, len(nodes))):
             nodes[i - 1].body.append(nodes[i])
         return self.visit(nodes[0])
     elif isinstance(_range, ast.Call) and _range.func.id in [
             "enumerate_dim", "range_dim"
     ]:
         node.body = [self.visit(s) for s in node.body]
         node.body = util.flatten(node.body)
         return RangeDim(node, self.connections[0].mapping,
                         self.connections[0].source)
     else:
         raise NotImplementedError(ast.dump(node))
コード例 #15
0
ファイル: asthelpers.py プロジェクト: gitter-badger/CrossHair
def call_positional_expr(call_node, idx, *extra):
    params = call_node.args
    while idx > 0 and not _isstarred(params[0]):
        idx -= 1
        params = params[1:]
        if not params:  # return default
            return extra[0]
    if idx == 0 and not _isstarred(params[0]):  # the easy case
        return params[0]
    params = [
        p.value if _isstarred(p) else ast.Tuple(elts=[p]) for p in params
    ]
    concatenated = functools.reduce(lambda x, y: ast.BinOp(x, ast.Add, y),
                                    params)
    ret = ast.Subscript(value=concatenated,
                        slice=ast.Index(value=ast.Num(n=idx)))
    if extra:
        default = extra[0]
        ret = apply_ast_template(exprparse('R if I < len(R) else D'),
                                 R=ret,
                                 I=idx,
                                 D=default)
    return ret
コード例 #16
0
ファイル: matchsyntax.py プロジェクト: qoffee/metacontext
    def template(self, translator, body, args, var):
        try:
            match_msg_node = translator.stack[-2]['match_msg_Name']
        except (IndexError, KeyError):
            raise SyntaxError(
                "with case() should be nested in a with match() construct")

        if var:
            if isinstance(var, ast.Tuple):
                bound_vars = var
            else:
                bound_vars = ast.Tuple([var], ast.Store())
        else:
            bound_vars = ast.Name('__x__', ast.Store())

        with quote() as q:
            with unquote_bind(bound_vars) as __x:
                __is_match, __x = match(unquote([match_msg_node] + args.args))
                if __is_match:
                    unquote_stmts(body)
                    break

        return q
コード例 #17
0
ファイル: make_ipy.py プロジェクト: dataflownb/dfconvert
def transform_last_node(csource,cast,exec_count):
    if isinstance(exec_count,int):
        exec_count = ("{0:#0{1}x}".format(int(exec_count),8))[2:]
    if len(cast.tree.body) > 0 and isinstance(cast.tree.body[-1], ast.Expr):
        expr_val = cast.tree.body[-1].value
        if isinstance(expr_val, ast.Tuple):
            tuple_eles = []
            named_flag = False
            out_exists = False
            for idx, elt in enumerate(expr_val.elts):
                if isinstance(elt, ast.Name):
                    named_flag = True
                    tuple_eles.append(ast.Name(elt.id, ast.Store))
                else:
                    out_exists = True
                    tuple_eles.append(ast.Name('Out_' + str(exec_count) + '['+str(idx)+']', ast.Store))
            if (named_flag):
                nnode = ast.Assign([ast.Tuple(tuple_eles, ast.Store)], expr_val)
                out_assign = 'Out_'+str(exec_count)+' = []\n' if out_exists else ''
                ast.fix_missing_locations(nnode)
                start,end = cast.tree.body[-1].first_token.startpos, cast.tree.body[-1].last_token.endpos
                csource = csource[:start] + out_assign + astor.to_source(nnode) + csource[end:]
    return csource
コード例 #18
0
 def visit_BinOp(self, node: ast.BinOp) -> Union[ast.BinOp, ast.Subscript]:
     if isinstance(node.op, ast.BitOr):
         self.rewritten = True
         return ast.Subscript(
             value=ast.Name(id='_Union',
                            ctx=ast.Load(),
                            lineno=1,
                            col_offset=1),
             slice=ast.Index(
                 value=ast.Tuple(elts=list(get_elts(node)),
                                 ctx=ast.Load(),
                                 lineno=1,
                                 col_offset=1),
                 ctx=ast.Load(),
                 lineno=1,
                 col_offset=1,
             ),
             lineno=1,
             col_offset=1,
             ctx=ast.Load(),
         )
     else:
         return node
コード例 #19
0
    def _to_ast_object(self, node, node_value):
        if sys.version_info.major >= 3:
            if sys.version_info.minor >= 8:
                # check for unicode strings
                kind = "u" if isinstance(node_value, str) else None

                return ast.Constant(node_value, kind=kind)
            else:
                return ast.Constant(node_value)

        else:  # python 2
            if isinstance(node_value, bool):
                return ast.Name(bool, node.ctx)
            elif isinstance(node_value, (int, float, str)):
                return ast.Num(node_value)
            elif isinstance(node_value, list):
                return ast.List(
                    [self._to_ast_object(node, value) for value in node_value],
                    node.ctx)
            elif isinstance(node_value, tuple):
                return ast.Tuple(
                    [self._to_ast_object(node, value) for value in node_value],
                    node.ctx)
コード例 #20
0
ファイル: compiler.py プロジェクト: mrh1997/cymu
def astconv_struct_decl(struct_decl_astc, ctx, prefix_stmts):
    field_astpy_list = []
    for decl_astc in struct_decl_astc.get_children():
        if decl_astc.kind.name == 'FIELD_DECL':
            type_name = decl_astc.type.spelling
            if type_name.startswith('struct '):
                type_astpy = attr(type_name.replace(' ', '_'))
            else:
                type_astpy = attr('datamodel', 'CProgram',
                                  TYPE_MAP[decl_astc.type.kind])
            field_astpy_list.append(
                ast.Tuple(elts=[ast.Str(s=decl_astc.spelling), type_astpy],
                          ctx=ast.Load()))
        elif decl_astc.kind.name == 'STRUCT_DECL':
            substruct_astpy = astconv_struct_decl(decl_astc, ctx, prefix_stmts)
            prefix_stmts.append(substruct_astpy)

    struct_name = 'struct_' + struct_decl_astc.spelling
    return ast.Assign(targets=[ast.Name(id=struct_name, ctx=ast.Store())],
                      value=call(
                          attr('datamodel', 'StructCType'),
                          ast.Str(s=struct_decl_astc.spelling),
                          ast.List(elts=field_astpy_list, ctx=ast.Load())))
コード例 #21
0
ファイル: code_gen.py プロジェクト: stjordanis/flor
def make_proc_side_effects(left_arg_list, right_arg_list):
    """
    left_arg_list: list of ASTs, already under_scored
    right_arg_list: list of ASTs

    *arg_list = flor.skip_stack.pop().proc_side_effects(*arg_list)
    """
    assert len(left_arg_list) == len(right_arg_list)
    if left_arg_list:
        names = [astunparse.unparse(e).strip() for e in right_arg_list]
        mask = mask_lattice(names)

        right_arg_list = [
            arg for i, arg in enumerate(right_arg_list) if i in mask
        ]
        left_arg_list = [
            arg for i, arg in enumerate(left_arg_list) if i in mask
        ]

        load_list = deepcopy(right_arg_list)
        store_list = deepcopy(left_arg_list)
        for each in load_list:
            each.ctx = ast.Load()
        for each in store_list:
            each.ctx = ast.Store()
        if len(store_list) > 1:
            store_list = [ast.Tuple(elts=store_list)]
        return ast.Assign(targets=store_list,
                          value=ast.Call(func=ast.Attribute(
                              value=make_attr_call('skip_stack', 'pop'),
                              attr='proc_side_effects',
                              ctx=ast.Load()),
                                         args=load_list,
                                         keywords=[]))
    else:
        # Case when there is nothing to proc_side_effect.
        return ast.Expr(value=make_attr_call('skip_stack', 'pop'))
コード例 #22
0
def mk_full_signature_from_json(abi):
    funcs = [func for func in abi if func['type'] == 'function']
    sigs = []

    for func in funcs:
        args = []
        returns = None
        for a in func['inputs']:
            arg = ast.arg(arg=a['name'],
                          annotation=abi_type_to_ast(a['type']),
                          lineno=0,
                          col_offset=0)
            args.append(arg)

        if len(func['outputs']) == 1:
            returns = abi_type_to_ast(func['outputs'][0]['type'])
        elif len(func['outputs']) > 1:
            returns = ast.Tuple(
                elts=[abi_type_to_ast(a['type']) for a in func['outputs']])

        decorator_list = [ast.Name('public', None)]
        if func['constant']:
            decorator_list.append(ast.Name('constant', None))
        if func['payable']:
            decorator_list.append(ast.Name('payable', None))

        sig = FunctionSignature.from_definition(code=ast.FunctionDef(
            name=func['name'],
            args=ast.arguments(args=args),
            decorator_list=decorator_list,
            returns=returns,
        ),
                                                custom_units=set(),
                                                custom_structs=dict(),
                                                constants=Constants())
        sigs.append(sig)
    return sigs
コード例 #23
0
    def visit_GeneratorExp(self, node):

        if node in self.optimizable_comprehension:
            self.update = True
            self.generic_visit(node)

            iters = [self.make_Iterator(gen) for gen in node.generators]
            variables = [
                ast.Name(gen.target.id, ast.Param()) for gen in node.generators
            ]

            # If dim = 1, product is useless
            if len(iters) == 1:
                iterAST = iters[0]
                varAST = ast.arguments([variables[0]], None, None, [])
            else:
                prodName = ast.Attribute(value=ast.Name(id='itertools',
                                                        ctx=ast.Load()),
                                         attr='product',
                                         ctx=ast.Load())

                iterAST = ast.Call(prodName, iters, [], None, None)
                varAST = ast.arguments([ast.Tuple(variables, ast.Store())],
                                       None, None, [])

            imapName = ast.Attribute(value=ast.Name(id='itertools',
                                                    ctx=ast.Load()),
                                     attr='imap',
                                     ctx=ast.Load())

            ldBodyimap = node.elt
            ldimap = ast.Lambda(varAST, ldBodyimap)

            return ast.Call(imapName, [ldimap, iterAST], [], None, None)

        else:
            return self.generic_visit(node)
コード例 #24
0
    def _create_discriminator_field(self, type_obj):
        items = {}
        for child in type_obj.get_all_children():
            items[
                child.
                discriminator_value] = f"commercetools._schemas.{child.package_name}.{child.name}Schema"

        field = type_obj.get_discriminator_field()
        self.generator.add_import_statement(self.resource.package_name,
                                            "commercetools", "helpers")

        return ast.Call(
            func=ast.Name(id="helpers.Discriminator"),
            args=[],
            keywords=[
                ast.keyword(
                    arg="discriminator_field",
                    value=ast.Tuple(elts=[
                        ast.Str(s=field.name, kind=None),
                        ast.Str(s=field.attribute_name, kind=None),
                    ]),
                ),
                ast.keyword(
                    arg="discriminator_schemas",
                    value=ast.Dict(
                        keys=[ast.Str(s=v, kind=None) for v in items.keys()],
                        values=[
                            ast.Str(s=v, kind=None) for v in items.values()
                        ],
                    ),
                ),
                ast.keyword(arg="unknown",
                            value=ast.Name(id="marshmallow.EXCLUDE")),
                ast.keyword(arg="allow_none",
                            value=ast.NameConstant(True, kind=None)),
            ],
        )
コード例 #25
0
def myescape(obj):
    """myescape(obj) Translate the given AST into a Python AST
    that can be evaluated to construct the given AST."""
    if isinstance(obj, _pyast.AST):
        ast_type = type(obj)
        esc_args = [
            myescape(getattr(obj, ctor_arg)) for ctor_arg in ast_type._fields
            # FIXME: Investigate possible stink; Optional fields
            # at the end of the field list may be elided by CPython.
            # For example, constants' kind field goes missing if not
            # explicitly given to the ctor.
            if hasattr(obj, ctor_arg)
        ]
        ret_val = _pyast.Call(_pyast.Name(ast_type.__name__, _pyast.Load()),
                              esc_args, [])
    elif isinstance(obj, dict):
        keyobjs = obj.keys()
        ret_val = _pyast.Dict([myescape(keyobj) for keyobj in keyobjs],
                              [myescape(obj[keyobj]) for keyobj in keyobjs])
    elif isinstance(obj, list):
        ret_val = _pyast.List([myescape(subobj) for subobj in obj],
                              _pyast.Load())
    elif isinstance(obj, tuple):
        ret_val = _pyast.Tuple([myescape(subobj) for subobj in obj],
                               _pyast.Load())
    elif isinstance(obj, int):
        ret_val = _pyast.Num(obj)
    elif isinstance(obj, float):
        ret_val = _pyast.Num(obj)
    elif isinstance(obj, str):
        ret_val = _pyast.Str(obj)
    elif obj is None:
        ret_val = _pyast.Name("None", _pyast.Load())
    else:
        raise NotImplementedError("Don't know how to escape '%r'!" % (obj))
    return ret_val
コード例 #26
0
ファイル: ast.py プロジェクト: fagan2888/Bigfish
    def visit_Expr(self, node):

        def get_arg_name(node):
            assert isinstance(node, ast.Name)
            assert isinstance(node.ctx, ast.Load)
            return node.id

        node = self.generic_visit(node)
        value = node.value
        if isinstance(value, ast.Call):
            if isinstance(value.func, ast.Name) and (value.func.id == 'Export') and isinstance(value.func.ctx,
                                                                                               ast.Load):
                if value.keywords:
                    # TODO 自定义错误
                    raise ValueError
                arg_names = [get_arg_name(arg_node) for arg_node in value.args]
                if not arg_names:
                    return node
                value.args[:] = [ast.copy_location(ast.Str(s=name), arg_node) for name, arg_node in
                                 zip(arg_names, value.args)]
                self.__export_id += 1
                patcher = LocationPatcher(value.args[-1])
                value.keywords.append(
                    patcher.visit(ast.keyword(arg='series_id', value=ast.Name(id='series_id', ctx=ast.Load()))))
                value.keywords.append(patcher.visit(ast.keyword(arg='export_id', value=ast.Num(n=self.__export_id))))
                value.keywords.append(
                    patcher.visit(ast.keyword(arg='barnum', value=ast.Name(id='BarNum', ctx=ast.Load()))))
                # TODO行号问题
                new_node = ast.copy_location(ast.Assign(targets=[], value=value), node)
                new_node.targets.append(ast.copy_location(
                    ast.Tuple(
                        elts=[ast.copy_location(ast.Name(id=name, ctx=ast.Store()), node) for name in
                              arg_names],
                        ctx=ast.Store()), node))
                return new_node
        return node
コード例 #27
0
ファイル: utils.py プロジェクト: chenyfsysu/PythonNote
def new_constant(value, node=None):
    if isinstance(value, bool):
        new_node = ast.Name(id='True' if value else 'False', ctx=ast.Load())
    elif isinstance(value, (int, float, long, complex)):
        new_node = ast.Num(n=value)
    elif isinstance(value, (unicode, str)):
        new_node = ast.Str(s=value)
    elif isinstance(value, (tuple, list)):
        cls = ast.Tuple if isinstance(value, tuple) else ast.List
        elts = [new_constant(elt, node) for elt in value]
        new_node = cls(elts=elts, ctx=ast.Load)
    elif isinstance(value, set):
        elts = [new_constant(elt, node) for elt in value]
        new_node = ast.Set(elts=elts)
    elif isinstance(value, dict):
        keys = [new_constant(k, node) for k in value.keys()]
        values = [new_constant(v, node) for v in value.values()]
        new_node = ast.Dict(keys=keys, values=values)
    elif isinstance(value, frozenset):
        elts = [new_constant(elt, node) for elt in value]
        arg = ast.Tuple(elts=elts, ctx=ast.Load())
        func = ast.Name(id='frozenset', ctx=ast.Load())
        new_node = ast.Call(func=func,
                            args=[arg],
                            keywords=[],
                            starargs=None,
                            kwargs=None)
    elif value is None:
        new_node = ast.Name(id='None', ctx=ast.Load())
    else:
        raise TypeError("unknown type: %s" % type(value).__name__)

    node and copy_lineno(node, new_node)
    new_node.__preinit__(parent=node.parent if node else node.parent)
    new_node.__postinit__()
    return new_node
コード例 #28
0
 def _get_replacement_args(self, args, should_record, keywords):
     replacement_args = []
     for arg in args:
         if keywords:
             maybe_kwarg = getattr(arg, 'value')
         else:
             maybe_kwarg = arg
         chain = GetAttrSubSymbols()(maybe_kwarg)
         statically_resolvable = []
         for sym in chain.symbols:
             # TODO: only handles attributes properly; subscripts will break
             if not isinstance(sym, str):
                 break
             statically_resolvable.append(ast.Str(sym))
         statically_resolvable = ast.Tuple(elts=statically_resolvable,
                                           ctx=ast.Load())
         with self.attrsub_load_context(False):
             visited_maybe_kwarg = self.visit(maybe_kwarg)
         ast.copy_location(visited_maybe_kwarg, maybe_kwarg)
         argrecord_args = [visited_maybe_kwarg, statically_resolvable]
         if should_record:
             with self.attrsub_load_context(False):
                 new_arg_value = cast(
                     ast.expr,
                     ast.Call(func=ast.Name(self.arg_recorder, ast.Load()),
                              args=argrecord_args,
                              keywords=[]))
         else:
             new_arg_value = visited_maybe_kwarg
         # ast.copy_location(new_arg_value, maybe_kwarg)
         if keywords:
             setattr(arg, 'value', new_arg_value)
         else:
             arg = new_arg_value
         replacement_args.append(arg)
     return replacement_args
コード例 #29
0
def handle_from_mod_generic_name(node):
    """Convert a `BinOp` `%` formatted str with a unknown name on the `node.right` to an f-string.

    When `node.right` is a Name since we don't know if it's a single var or a dict so we sniff the string.

    `"val: %(key_name1)s val2: %(key_name2)s" % some_dict`
    Sniffs the left string for Dict style usage and calls: `handle_from_mod_dict_name`

    `"val: %s" % some_var`
    Borrow the core logic by injecting the name into a ast.Tuple

    Args:
       node (ast.BinOp): The node to convert to a f-string

    Returns ast.JoinedStr (f-string)
    """

    has_dict_str_format = MOD_KEY_PATTERN.findall(node.left.s)
    if has_dict_str_format:
        return handle_from_mod_dict_name(node)

    # if it's just a name then pretend it's tuple to use that code
    node.right = ast.Tuple(elts=[node.right])
    return handle_from_mod_tuple(node)
コード例 #30
0
def atom_rewrite(loc, name, token, value, number, strs, namedc, ellipsis, dict,
                 is_dict, is_gen, is_list, comp, yield_expr):
    if name:
        if not token:
            return ast.Name(name.value, ast.Load(), **loc @ name)
        return ex_ast.AssignExpr(ast.Name(name.value, ast.Store(),
                                          **loc @ name),
                                 value=value,
                                 **loc @ token)

    if number:
        return ast.Num(eval(number.value), **loc @ number)

    if strs:
        return str_maker(*strs)

    if ellipsis:
        return ast.Ellipsis()

    if namedc:
        return ast.NameConstant(eval(namedc.value), **loc @ namedc)

    if is_dict:
        return dict or ex_ast.ExDict([], [], ast.Load(), **loc @ is_dict)

    if is_gen:
        if yield_expr:
            return yield_expr
        return comp(is_tuple=True) if comp else ast.Tuple([], ast.Load(), **
                                                          loc @ is_gen)

    if is_list:
        return comp(is_list=True) if comp else ast.List([], ast.Load(), **
                                                        loc @ is_list)

    raise TypeError