ultk.util.frozendict
1from typing import Generic, TypeVar 2from yaml import YAMLObject 3 4K = TypeVar("K") 5V = TypeVar("V") 6 7 8class FrozenDict(dict[K, V], Generic[K, V], YAMLObject): 9 yaml_tag = "!frozendict" 10 11 def __hash__(self): 12 return hash(frozenset(self.items())) 13 14 def __setitem__(self, key, value): 15 raise TypeError("FrozenDict is immutable") 16 17 def __delitem__(self, key): 18 raise TypeError("FrozenDict is immutable") 19 20 def clear(self): 21 raise TypeError("FrozenDict is immutable") 22 23 def pop(self, key, default=None): 24 raise TypeError("FrozenDict is immutable") 25 26 def popitem(self): 27 raise TypeError("FrozenDict is immutable") 28 29 def setdefault(self, key, default=None): 30 raise TypeError("FrozenDict is immutable") 31 32 @classmethod 33 def to_yaml(cls, dumper, data): 34 return dumper.represent_mapping(cls.yaml_tag, dict(data)) 35 36 @classmethod 37 def from_yaml(cls, loader, node): 38 return FrozenDict(loader.construct_mapping(node, deep=True)) 39 40 def update(self, *args, **kwargs): 41 raise TypeError("FrozenDict is immutable") 42 43 def __repr__(self): 44 return f"FrozenDict({super().__repr__()})"
class
FrozenDict(dict[~K, ~V], typing.Generic[~K, ~V], yaml.YAMLObject):
9class FrozenDict(dict[K, V], Generic[K, V], YAMLObject): 10 yaml_tag = "!frozendict" 11 12 def __hash__(self): 13 return hash(frozenset(self.items())) 14 15 def __setitem__(self, key, value): 16 raise TypeError("FrozenDict is immutable") 17 18 def __delitem__(self, key): 19 raise TypeError("FrozenDict is immutable") 20 21 def clear(self): 22 raise TypeError("FrozenDict is immutable") 23 24 def pop(self, key, default=None): 25 raise TypeError("FrozenDict is immutable") 26 27 def popitem(self): 28 raise TypeError("FrozenDict is immutable") 29 30 def setdefault(self, key, default=None): 31 raise TypeError("FrozenDict is immutable") 32 33 @classmethod 34 def to_yaml(cls, dumper, data): 35 return dumper.represent_mapping(cls.yaml_tag, dict(data)) 36 37 @classmethod 38 def from_yaml(cls, loader, node): 39 return FrozenDict(loader.construct_mapping(node, deep=True)) 40 41 def update(self, *args, **kwargs): 42 raise TypeError("FrozenDict is immutable") 43 44 def __repr__(self): 45 return f"FrozenDict({super().__repr__()})"
def
pop(self, key, default=None):
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If the key is not found, return the default if given; otherwise, raise a KeyError.
def
popitem(self):
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.
def
setdefault(self, 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.
def
update(self, *args, **kwargs):
D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]