当前位置: 首页>>代码示例>>Python>>正文


Python dataclasses.make_dataclass方法代码示例

本文整理汇总了Python中dataclasses.make_dataclass方法的典型用法代码示例。如果您正苦于以下问题:Python dataclasses.make_dataclass方法的具体用法?Python dataclasses.make_dataclass怎么用?Python dataclasses.make_dataclass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dataclasses的用法示例。


在下文中一共展示了dataclasses.make_dataclass方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_error_type

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def create_error_type(base_name, graphql_fields):
    fields = [
        (name, List[str], dataclasses.field(default_factory=list))
        for name, _ in graphql_fields.items()
    ]
    fields.append(
        ("nonFieldErrors", List[str], dataclasses.field(default_factory=list))
    )

    error_type = dataclasses.make_dataclass(f"{base_name}Errors", fields)
    return strawberry.type(error_type) 
开发者ID:pythonitalia,项目名称:pycon,代码行数:13,代码来源:utils.py

示例2: create_input_type

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def create_input_type(base_name, graphql_fields):
    input_class = dataclasses.make_dataclass(
        f"{base_name}Input",
        [(name, type_, field) for name, [type_, field] in graphql_fields.items()],
    )
    return strawberry.input(input_class) 
开发者ID:pythonitalia,项目名称:pycon,代码行数:8,代码来源:utils.py

示例3: __new__

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def __new__(cls, name_, bases, namespace, **options):
        # Note: it's safe to pass options as keyword arguments as they are still type-checked by ObjectTypeOptions.

        # We create this type, to then overload it with the dataclass attrs
        class InterObjectType:
            pass

        base_cls = super().__new__(
            cls, name_, (InterObjectType,) + bases, namespace, **options,
        )
        if base_cls._meta:
            fields = [
                (
                    key,
                    "typing.Any",
                    field(
                        default=field_value.default_value
                        if isinstance(field_value, Field)
                        else None
                    ),
                )
                for key, field_value in base_cls._meta.fields.items()
            ]
            dataclass = make_dataclass(name_, fields, bases=())
            InterObjectType.__init__ = dataclass.__init__
            InterObjectType.__eq__ = dataclass.__eq__
            InterObjectType.__repr__ = dataclass.__repr__
        return base_cls 
开发者ID:graphql-python,项目名称:graphene,代码行数:30,代码来源:objecttype.py

示例4: make_config

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def make_config(cls_name: str, fields, *args, namespace=None, **kwargs):
    """
    Function to create a dataclass
    """
    if namespace is None:
        namespace = {}
    namespace.setdefault("_fromdict", classmethod(_fromdict))
    namespace.setdefault(
        "_replace",
        lambda self, *args, **kwargs: dataclasses.replace(
            self, *args, **kwargs
        ),
    )
    namespace.setdefault("_asdict", config_asdict)
    kwargs["eq"] = True
    kwargs["init"] = True
    # Ensure non-default arguments always come before default arguments
    fields_non_default = []
    fields_default = []
    for name, cls, field in fields:
        if (
            field.default is not dataclasses.MISSING
            or field.default_factory is not dataclasses.MISSING
        ):
            fields_default.append((name, cls, field))
        else:
            fields_non_default.append((name, cls, field))
    fields = fields_non_default + fields_default
    # Create dataclass
    return dataclasses.make_dataclass(
        cls_name, fields, *args, namespace=namespace, **kwargs
    ) 
开发者ID:intel,项目名称:dffml,代码行数:34,代码来源:base.py

示例5: make_msg_type

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def make_msg_type(cls_name, fields):
    """Makes dataclass-based message types for IPC."""
    if isinstance(fields, str):
        fields = fields.split()
    return make_dataclass(cls_name, fields, namespace={'__module__': __name__}) 
开发者ID:crowsonkb,项目名称:style_transfer,代码行数:7,代码来源:style_transfer.py

示例6: __new__

# 需要导入模块: import dataclasses [as 别名]
# 或者: from dataclasses import make_dataclass [as 别名]
def __new__(meta, name, bases, class_dict):
        cls = type.__new__(meta, name, bases, class_dict)
        cls = dataclasses.dataclass(cls, eq=False)

        # Get the fields for inner class autogeneration.
        cls_fields = dataclasses.fields(cls)
        # Keep track of all the fields that can vary.
        nonconst_fields = []
        for field in cls_fields:
            if ("constant_field" in field.metadata) and (
                    field.metadata["constant_field"]):
                continue
            nonconst_fields.append(field)

        # Create constant flag class.
        if "ConstFlags" not in cls.__dict__:
            const_flag_fields = [(field.name, bool,
                                  dataclasses.field(default=False))
                                 for field in nonconst_fields]
            cls.ConstFlags = dataclasses.make_dataclass(name + ".ConstFlags",
                                                        const_flag_fields)

            def __bool__(self) -> bool:
                return all(
                    getattr(self, field.name) for field in nonconst_fields)

            def set_all(self, value: bool) -> None:
                for field in nonconst_fields:
                    setattr(self, field.name, value)

            cls.ConstFlags.__bool__ = __bool__
            cls.ConstFlags.set_all = set_all

        # Create the gradient class.
        if "Grad" not in cls.__dict__:
            grad_fields = [(field.name + "_grad", field.type, np_zero_field(1))
                           for field in nonconst_fields]

            grad_bases = tuple(cls_base.Grad for cls_base in bases)
            cls.Grad = dataclasses.make_dataclass(name + ".Grad",
                                                  grad_fields,
                                                  bases=grad_bases)

            def __iadd__(self, value):
                for field in grad_fields:
                    field_val = getattr(self, field[0])
                    value_val = getattr(value, field[0])
                    try:
                        field_val.__iadd__(value_val)
                    except AttributeError:
                        setattr(self, field[0], value_val + field_val)
                return super(cls.Grad, self).__iadd__(value)

            cls.Grad.__iadd__ = __iadd__

        return cls 
开发者ID:stanfordnqp,项目名称:spins-b,代码行数:58,代码来源:flows.py


注:本文中的dataclasses.make_dataclass方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。