DictWithAliases

class PlasmaCalcs.tools.iterables.DictWithAliases(*args_dict, aliases=UNSET, **kwargs_dict)

Bases: dict

dict but some keys are aliases for other keys.

E.g. d = AliasDict({‘x’: 7}); d.alias(‘x’, ‘y’);
d[‘y’] == d[‘x’] == 7
d[‘y’] = 8
d[‘y’] == d[‘x’] == 8
Checking “key in self” also checks aliases.
(use “key in self.keys()” to exclude aliases.)
Looping through self.keys() or self.items() does NOT include aliases.
add alias via self.alias(key0, key1),
and/or provide dict of aliases during __init__.

Methods

__delitem__(key)

deletes value associated with key (or alias) from self.

__init__(*args_dict[, aliases])

initialize self with key-value pairs, and optionally aliases.

add_aliases(aliases)

add multiple aliases at once.

alias(key0, key1)

tell self to treat these keys as the same key.

clear()

copy()

return a shallow copy of self.

fromkeys([value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

items()

keys()

pop(key[, default])

pop key from self, returning self[key].

popitem()

Remove and return a (key, value) pair as a 2-tuple.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

un_alias(alias)

remove alias from self.

update(*args_dict, **kwargs_dict)

update self with new key-value pairs.

values()

_default_aliases()

returns default aliases for this dict.

Attributes

DEFAULT_ALIASES

add_alias

alias to alias

aliases

aliases known by this dict.

__delitem__(key)

deletes value associated with key (or alias) from self.

does NOT alter anything about known aliases.
__init__(*args_dict, aliases=UNSET, **kwargs_dict)

initialize self with key-value pairs, and optionally aliases.

aliases: UNSET, or dict of {key1: key0} pairs to be aliased.
UNSET –> self will use self.DEFAULT_ALIASES.
dict –> self.alias(key0, key1) for each (key0, key1) pair.
(key1 is more likely to become the “alias”, internally.)
_default_aliases()

returns default aliases for this dict.

the implementation here just returns self.DEFAULT_ALIASES.copy().
property add_alias

alias to alias

add_aliases(aliases)

add multiple aliases at once.

aliases: dict of {alias: key} pairs.
Equivalent to calling self.alias(key, alias) for each (alias, key) pair.
alias(key0, key1)

tell self to treat these keys as the same key.

key0 is more likely to become the official key, while key1 will be the alias.
(however, if either is already known to self, decides “official vs alias” appropriately.)
property aliases

aliases known by this dict. aliases is a dict of {alias: key}

clear() None.  Remove all items from D.
copy()

return a shallow copy of self. Result aliases are the same as self.aliases.

fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(key, default=UNSET)

pop key from self, returning self[key].

does NOT alter anything about known aliases.
default: UNSET or any value
if key not in self, return default if provided (i.e., not UNSET)
popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order.

Raises KeyError if the dict is empty.
setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

un_alias(alias)

remove alias from self.

If alias is an “official” key with a known alias,
swap the alias for the official key, then remove the previously-official key.
update(*args_dict, **kwargs_dict)

update self with new key-value pairs.

does NOT alter anything about known aliases.
handles appropriately any known aliases in key implied by *args and **kwargs.
values() an object providing a view on D's values