示例#1
0
        def ast_from_dict(d):
            keys = []
            values = []

            for k, v in d.items():
                keys.append(ast.Str(s=k))
                values.append(ast.Num(n=v))

            return ast.Dict(keys=keys, values=values)
示例#2
0
    def get_klass_constructor(self, properties: PropertiesType,
                              requireds: RequiredType) -> ast.FunctionDef:
        # Prepare body
        fn_body = []

        # Default value for `data` argument
        if len(properties) > 0:
            fn_body.append(
                ast.Assign(
                    targets=[ast.Name(id="data")],
                    value=ast.BoolOp(op=ast.Or(),
                                     values=[
                                         ast.Name(id="data"),
                                         ast.Dict(keys=[], values=[])
                                     ]),
                ))

        for key in sorted(properties.keys()):
            # Get default value
            property_ = properties[key]
            is_required = key in requireds

            value = self.get_member_value(key,
                                          property_,
                                          is_required=is_required)

            # Build assign expression
            attribute = ast.Assign(
                targets=[ast.Attribute(value=ast.Name(id="self"), attr=key)],
                value=value)

            # Add to body
            fn_body.append(attribute)

        # Bundle function arguments and keywords
        fn_arguments = ast.arguments(
            args=[
                ast.arg(arg="self", annotation=None),
                ast.arg(arg="data", annotation=None)
            ],
            vararg=None,
            kwarg=None,
            kwonlyargs=[],
            kw_defaults=[],
            defaults=[ast.NameConstant(value=None)],
        )

        # Generate class constructor
        fn_init = ast.FunctionDef(name="__init__",
                                  args=fn_arguments,
                                  body=fn_body,
                                  decorator_list=[],
                                  returns=None)

        # Return constructor
        return fn_init
示例#3
0
        def _dict_default_helper(d):

            keys = []
            values = []

            for k, v in d.items():
                keys.append(ast.Str(s=k))

                if isinstance(v, bool):
                    values.append(ast.NameConstant(value=v))
                elif isinstance(v, int):
                    values.append(ast.Num(n=v))
                elif isinstance(v, list):
                    values.append(
                        ast.List(elts=[_default_helper(el) for el in v]))
                elif isinstance(v, str):
                    values.append(ast.Str(s=v))
                elif isinstance(v, dict):
                    values.append(_dict_default_helper(v))
                else:
                    raise NotImplementedError(
                        "Default type not handled (Dict)")

            return ast.Dict(keys=keys, values=values)