Python attr.fields_dict() Examples

The following are 17 code examples of attr.fields_dict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module attr , or try the search function .
Example #1
Source File: service.py    From trains-agent with Apache License 2.0 5 votes vote down vote up
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)
                }
            )
        ) 
Example #2
Source File: test_validators.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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"] 
Example #3
Source File: test_model.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #4
Source File: process.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #5
Source File: process.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #6
Source File: utils.py    From xarray-simlab with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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
    ) 
Example #7
Source File: config.py    From corrscope with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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) 
Example #8
Source File: model_bind.py    From corrscope with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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)) 
Example #9
Source File: service.py    From trains with Apache License 2.0 5 votes vote down vote up
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)
                }
            )
        ) 
Example #10
Source File: ti_provider_base.py    From msticpy with MIT License 5 votes vote down vote up
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 
Example #11
Source File: base.py    From trains-agent with Apache License 2.0 5 votes vote down vote up
def from_dict(cls, kwargs):
        fields = fields_dict(cls)
        return cls(**filter_keys(lambda key: key in fields, kwargs)) 
Example #12
Source File: tables.py    From tskit with MIT License 5 votes vote down vote up
def __init__(self):
        self.metadata_column_index = list(
            attr.fields_dict(self.row_class).keys()
        ).index("metadata")
        self._update_metadata_schema_cache_from_ll() 
Example #13
Source File: _utils.py    From omegaconf with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #14
Source File: settings.py    From RLs with Apache License 2.0 5 votes vote down vote up
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) 
Example #15
Source File: attrs.py    From nata with MIT License 5 votes vote down vote up
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 
Example #16
Source File: settings.py    From RLs with Apache License 2.0 4 votes vote down vote up
def from_argparse(args: argparse.Namespace) -> "RunOptions":
        """
        Takes an argparse.Namespace as specified in `parse_command_line`, loads input configuration files
        from file paths, and converts to a RunOptions instance.
        :param args: collection of command-line parameters passed to mlagents-learn
        :return: RunOptions representing the passed in arguments, with trainer config, curriculum and sampler
          configs loaded from files.
        """
        argparse_args = vars(args)
        config_path = StoreConfigFile.trainer_config_path

        # Load YAML
        configured_dict: Dict[str, Any] = {
            "checkpoint_settings": {},
            "env_settings": {},
            "engine_settings": {},
        }
        if config_path is not None:
            configured_dict.update(load_config(config_path))

        # Use the YAML file values for all values not specified in the CLI.
        for key in configured_dict.keys():
            # Detect bad config options
            if key not in attr.fields_dict(RunOptions):
                raise TrainerConfigError(
                    "The option {} was specified in your YAML file, but is invalid.".format(
                        key
                    )
                )
        # Override with CLI args
        # Keep deprecated --load working, TODO: remove
        argparse_args["resume"] = argparse_args["resume"] or argparse_args["load_model"]
        for key, val in argparse_args.items():
            if key in DetectDefault.non_default_args:
                if key in attr.fields_dict(CheckpointSettings):
                    configured_dict["checkpoint_settings"][key] = val
                elif key in attr.fields_dict(EnvironmentSettings):
                    configured_dict["env_settings"][key] = val
                elif key in attr.fields_dict(EngineSettings):
                    configured_dict["engine_settings"][key] = val
                else:  # Base options
                    configured_dict[key] = val
        return RunOptions.from_dict(configured_dict) 
Example #17
Source File: upgrade_config.py    From RLs with Apache License 2.0 4 votes vote down vote up
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