def obj_to_python_expr()

in core/maxframe/codegen.py [0:0]


    def obj_to_python_expr(cls, obj: Any = None) -> str:
        """
        Parameters
        ----------
        obj
            The object to convert to python expr.
        Returns
        -------
        str :
            The str type content equals to the object when use in the python code directly.
        """
        if obj is None:
            return "None"

        if isinstance(obj, (int, float)):
            return repr(obj)

        if isinstance(obj, bool):
            return "True" if obj else "False"

        if isinstance(obj, bytes):
            base64_bytes = base64.b64encode(obj)
            return f"base64.b64decode({base64_bytes})"

        if isinstance(obj, str):
            return repr(obj)

        if isinstance(obj, list):
            return (
                f"[{', '.join([cls.obj_to_python_expr(element) for element in obj])}]"
            )

        if isinstance(obj, dict):
            items = (
                f"{repr(key)}: {cls.obj_to_python_expr(value)}"
                for key, value in obj.items()
            )
            return f"{{{', '.join(items)}}}"

        if isinstance(obj, tuple):
            return f"({', '.join([cls.obj_to_python_expr(sub_obj) for sub_obj in obj])}{',' if len(obj) == 1 else ''})"

        if isinstance(obj, set):
            return (
                f"{{{', '.join([cls.obj_to_python_expr(sub_obj) for sub_obj in obj])}}}"
                if obj
                else "set()"
            )

        if isinstance(obj, PickleContainer):
            return UserCodeMixin.generate_pickled_codes(obj, None)

        raise ValueError(f"not support arg type {type(obj)}")