本文整理匯總了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
示例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
示例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)
示例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
示例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()
示例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))
示例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)
}
)
)
示例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))
示例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)
示例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
)
示例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
示例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)
示例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
示例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"]
示例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