當前位置: 首頁>>代碼示例>>Python>>正文


Python attr.fields_dict方法代碼示例

本文整理匯總了Python中attr.fields_dict方法的典型用法代碼示例。如果您正苦於以下問題:Python attr.fields_dict方法的具體用法?Python attr.fields_dict怎麽用?Python attr.fields_dict使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在attr的用法示例。


在下文中一共展示了attr.fields_dict方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: column_map

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def column_map(cls):
        """Return a dictionary that maps fields to DF Names."""
        col_mapping = {}
        for name in attr.fields_dict(cls):
            out_name = "".join([part.capitalize() for part in name.split("_")])
            col_mapping[name] = out_name
        return col_mapping


# pylint: enable=too-many-instance-attributes


# pylint: disable=too-few-public-methods 
開發者ID:microsoft,項目名稱:msticpy,代碼行數:15,代碼來源:ti_provider_base.py

示例2: attrib_equality

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def attrib_equality(
    some: T, other: Union[T, Any], props_to_check: Union[str, tuple] = None
):
    # if props_to_check is None, inspect attributes based on attrs and only for
    # some. attributes in other and their equality check is discarded
    # TODO: deep check
    if props_to_check is None:
        for key, attrib in attr.fields_dict(some.__class__).items():
            if attrib.eq:
                if not hasattr(other, attrib.name):
                    return False

                some_attrib = getattr(some, attrib.name)
                other_attrib = getattr(other, attrib.name)
                # check if attributes have attrs themselves and use
                # attrib_equality recursively else use equality
                if have_attr(some_attrib, other_attrib):
                    if not attrib_equality(some_attrib, other_attrib):
                        return False
                else:
                    if some_attrib != other_attrib:
                        return False

        return True
    else:
        if isinstance(props_to_check, str):
            props_to_check = props_to_check.replace(" ", "").split(",")

        for prop in props_to_check:
            if getattr(some, prop) != getattr(other, prop):
                return False
        return True 
開發者ID:GoLP-IST,項目名稱:nata,代碼行數:34,代碼來源:attrs.py

示例3: check_and_structure

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def check_and_structure(key: str, value: Any, class_type: type) -> Any:
    attr_fields_dict = attr.fields_dict(class_type)
    if key not in attr_fields_dict:
        raise TrainerConfigError(
            f"The option {key} was specified in your YAML file for {class_type.__name__}, but is invalid."
        )
    # Apply cattr structure to the values
    return cattr.structure(value, attr_fields_dict[key].type) 
開發者ID:StepNeverStop,項目名稱:RLs,代碼行數:10,代碼來源:settings.py

示例4: get_attr_data

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def get_attr_data(obj: Any) -> Dict[str, Any]:
    from omegaconf.omegaconf import _maybe_wrap

    d = {}
    is_type = isinstance(obj, type)
    obj_type = obj if is_type else type(obj)
    for name, attrib in attr.fields_dict(obj_type).items():
        is_optional, type_ = _resolve_optional(attrib.type)
        is_nested = is_attr_class(type_)
        type_ = _resolve_forward(type_, obj.__module__)
        if not is_type:
            value = getattr(obj, name)
        else:
            value = attrib.default
            if value == attr.NOTHING:
                if is_nested:
                    value = type_
                else:
                    _raise_missing_error(obj, name)
                    assert False
        if _is_union(type_):
            e = ConfigValueError(
                f"Union types are not supported:\n{name}: {type_str(type_)}"
            )
            format_and_raise(node=None, key=None, value=value, cause=e, msg=str(e))

        d[name] = _maybe_wrap(
            ref_type=type_, is_optional=is_optional, key=name, value=value, parent=None,
        )
    return d 
開發者ID:omry,項目名稱:omegaconf,代碼行數:32,代碼來源:_utils.py

示例5: __init__

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def __init__(self):
        self.metadata_column_index = list(
            attr.fields_dict(self.row_class).keys()
        ).index("metadata")
        self._update_metadata_schema_cache_from_ll() 
開發者ID:tskit-dev,項目名稱:tskit,代碼行數:7,代碼來源:tables.py

示例6: from_dict

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def from_dict(cls, kwargs):
        fields = fields_dict(cls)
        return cls(**filter_keys(lambda key: key in fields, kwargs)) 
開發者ID:allegroai,項目名稱:trains-agent,代碼行數:5,代碼來源:base.py

示例7: _parse_action

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def _parse_action(self, action_name, action_version, action_config):
        data = self.default.copy()
        data.update(action_config)

        if not action_config.get("generate", True):
            return None

        definitions_keys = set()
        for schema_key in ("request", "response"):
            if schema_key in action_config:
                try:
                    schema = action_config[schema_key]
                    refs = self._expand_schema_references_with_definitions(schema)
                    self._resolve_schema_references(schema, refs=refs)
                    definitions_keys.update(refs)
                except ValueError as ex:
                    name = "%s.%s/%.1f/%s" % (
                        self.name,
                        action_name,
                        action_version,
                        schema_key,
                    )
                    raise ValueError("%s in %s" % (str(ex), name))

        return Action(
            name=action_name,
            version=action_version,
            definitions_keys=list(definitions_keys),
            service=self.name,
            **(
                {
                    key: value
                    for key, value in data.items()
                    if key in attr.fields_dict(Action)
                }
            )
        ) 
開發者ID:allegroai,項目名稱:trains-agent,代碼行數:39,代碼來源:service.py

示例8: bind_widget

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def bind_widget(self, model: PresentationModel, path: str, *args, **kwargs) -> None:
        BoundWidget.bind_widget(self, model, path, *args, **kwargs)
        try:
            parent, name = flatten_attr(model.cfg, path)
        except AttributeError:
            return

        fields = attr.fields_dict(type(parent))
        field = fields[name]
        self.setSuffix(get_units(field)) 
開發者ID:corrscope,項目名稱:corrscope,代碼行數:12,代碼來源:model_bind.py

示例9: new_from_state

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def new_from_state(cls: Type[T], state: Dict[str, Any]) -> T:
        """ Redirect `Alias(key)=value` to `key=value`.
        Then call the dataclass constructor (to validate parameters). """

        cls_name = cls.__name__
        fields = attr.fields_dict(cls)

        # All names which can be passed into __init__()
        field_names = {name.lstrip("_") for name, field in fields.items() if field.init}

        new_state = {}
        for key, value in dict(state).items():
            class_var = getattr(cls, key, None)

            if class_var is Ignored:
                pass

            elif isinstance(class_var, Alias):
                target = class_var.key
                if target in state:
                    raise CorrError(
                        f"{cls_name} received both Alias {key} and "
                        f"equivalent {target}"
                    )
                new_state[target] = value

            elif key not in field_names:
                warnings.warn(
                    f'Unrecognized field "{key}" in !{cls_name}, ignoring', CorrWarning
                )

            else:
                new_state[key] = value

        del state
        return cls(**new_state) 
開發者ID:corrscope,項目名稱:corrscope,代碼行數:38,代碼來源:config.py

示例10: variables_dict

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def variables_dict(process_cls):
    """Get all xsimlab variables declared in a process.

    Exclude attr.Attribute objects that are not xsimlab-specific.
    """
    return OrderedDict(
        (k, v) for k, v in fields_dict(process_cls).items() if "var_type" in v.metadata
    ) 
開發者ID:benbovy,項目名稱:xarray-simlab,代碼行數:10,代碼來源:utils.py

示例11: _reset_attributes

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def _reset_attributes(self):
        new_attributes = OrderedDict()

        for k, attrib in attr.fields_dict(self._base_cls).items():
            new_attributes[k] = attr.attrib(
                metadata=attrib.metadata,
                validator=attrib.validator,
                converter=attrib.converter,
                default=attrib.default,
                init=False,
                repr=False,
            )

        return new_attributes 
開發者ID:benbovy,項目名稱:xarray-simlab,代碼行數:16,代碼來源:process.py

示例12: add_properties

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def add_properties(self):
        for var_name, var in attr.fields_dict(self._base_cls).items():
            var_type = var.metadata.get("var_type")

            if var_type is not None:
                make_prop_func = self._make_prop_funcs[var_type]

                self._p_cls_dict[var_name] = make_prop_func(var) 
開發者ID:benbovy,項目名稱:xarray-simlab,代碼行數:10,代碼來源:process.py

示例13: test_create_variable_cache

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def test_create_variable_cache(self, model):
        actual = model._var_cache[("init_profile", "n_points")]

        assert actual["name"] == "init_profile__n_points"
        assert (
            actual["attrib"]
            is attr.fields_dict(model["init_profile"].__class__)["n_points"]
        )
        assert actual["metadata"] == attr.fields_dict(InitProfile)["n_points"].metadata
        assert actual["value"] is None 
開發者ID:benbovy,項目名稱:xarray-simlab,代碼行數:12,代碼來源:test_model.py

示例14: simple_attr

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def simple_attr():
    """
    Return an attribute with a name just for testing purpose.
    """

    @attr.attrs
    class C:
        test = attr.attrib()

    return attr.fields_dict(C)["test"] 
開發者ID:benbovy,項目名稱:xarray-simlab,代碼行數:12,代碼來源:test_validators.py

示例15: convert_behaviors

# 需要導入模塊: import attr [as 別名]
# 或者: from attr import fields_dict [as 別名]
def convert_behaviors(old_trainer_config: Dict[str, Any]) -> Dict[str, Any]:
    all_behavior_config_dict = {}
    default_config = old_trainer_config.get("default", {})
    for behavior_name, config in old_trainer_config.items():
        if behavior_name != "default":
            config = default_config.copy()
            config.update(old_trainer_config[behavior_name])

            # Convert to split TrainerSettings, Hyperparameters, NetworkSettings
            # Set trainer_type and get appropriate hyperparameter settings
            try:
                trainer_type = config["trainer"]
            except KeyError:
                raise TrainerConfigError(
                    "Config doesn't specify a trainer type. "
                    "Please specify trainer: in your config."
                )
            new_config = {}
            new_config["trainer_type"] = trainer_type
            hyperparam_cls = TrainerType(trainer_type).to_settings()
            # Try to absorb as much as possible into the hyperparam_cls
            new_config["hyperparameters"] = cattr.structure(config, hyperparam_cls)

            # Try to absorb as much as possible into the network settings
            new_config["network_settings"] = cattr.structure(config, NetworkSettings)
            # Deal with recurrent
            try:
                if config["use_recurrent"]:
                    new_config[
                        "network_settings"
                    ].memory = NetworkSettings.MemorySettings(
                        sequence_length=config["sequence_length"],
                        memory_size=config["memory_size"],
                    )
            except KeyError:
                raise TrainerConfigError(
                    "Config doesn't specify use_recurrent. "
                    "Please specify true or false for use_recurrent in your config."
                )
            # Absorb the rest into the base TrainerSettings
            for key, val in config.items():
                if key in attr.fields_dict(TrainerSettings):
                    new_config[key] = val

            # Structure the whole thing
            all_behavior_config_dict[behavior_name] = cattr.structure(
                new_config, TrainerSettings
            )
    return all_behavior_config_dict 
開發者ID:StepNeverStop,項目名稱:RLs,代碼行數:51,代碼來源:upgrade_config.py


注:本文中的attr.fields_dict方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。