FrozenOrderedDict¶
About¶
FrozenOrderedDict
is a immutable wrapper around an OrderedDict.
FrozenOrderedDict
is similar to frozendict
, and with regards to immutability it
solves the same problems:
Because dictionaries are mutable, they are not hashable and cannot be used in sets or as dictionary keys.
Nasty bugs can and do occur when mutable data structures are passed around.
It can be initialized just like a dict
or OrderedDict
.
Once instantiated, an instance of FrozenOrderedDict
cannot be altered,
since it does not implement the MutableMapping
interface.
It does implement the Mapping
interface, so can be used just like a
normal dictionary in most cases.
In order to modify the contents of a FrozenOrderedDict
, a new
instance must be created. The easiest way to do that is by
calling the .copy() method. It will return a new instance of
FrozenOrderedDict
initialized using the following steps:
A copy of the wrapped
OrderedDict
instance will be created.If any arguments or keyword arguments are passed to the .copy() method, they will be used to create another
OrderedDict
instance, which will then be used to update the copy made in step #1.Finally, self.__class__() will be called, passing the copy as the only argument.
API Reference¶
-
class
FrozenOrderedDict
(*args, **kwds)[source]¶ Bases:
cawdrey.base.FrozenBase
An immutable OrderedDict. It can be used as a drop-in replacement for dictionaries where immutability is desired.
Methods:
__contains__
(key)Return
key in self
.__eq__
(other)Return
self == other
.__getitem__
(key)Return
self[key]
.__iter__
()Iterates over the dictionary’s keys
__len__
()Returns the number of keys in the dictionary.
__repr__
()Return a string representation of the
DictWrapper
.copy
(*args, **kwargs)Return a copy of the
FrozenOrderedDict
.fromkeys
(iterable[, value])Create a new dictionary with keys from iterable and values set to value.
get
()Return the value for
k
ifk
is in the dictionary, elsedefault
.items
()Returns a set-like object providing a view on the
FrozenOrderedDict
’s items.keys
()Returns a set-like object providing a view on the
FrozenOrderedDict
’s keys.values
()Returns an object providing a view on the
FrozenOrderedDict
’s values.-
__repr__
()¶ Return a string representation of the
DictWrapper
.- Return type
-
copy
(*args, **kwargs)[source]¶ Return a copy of the
FrozenOrderedDict
.- Parameters
args
kwargs
-
classmethod
fromkeys
(iterable, value=None)¶ Create a new dictionary with keys from iterable and values set to value.
-
get
(k: KT) → Optional[VT][source]¶ -
get
(k: KT, default: Union[VT, T]) → Union[VT, T] Return the value for
k
ifk
is in the dictionary, elsedefault
.- Parameters
k – The key to return the value for.
default – The value to return if
key
is not in the dictionary. DefaultNone
.
-
items
()[source]¶ Returns a set-like object providing a view on the
FrozenOrderedDict
’s items.- Return type
AbstractSet
[Tuple
[~KT
,~VT
]]
-
keys
()[source]¶ Returns a set-like object providing a view on the
FrozenOrderedDict
’s keys.- Return type
AbstractSet
[~KT
]
-
values
()[source]¶ Returns an object providing a view on the
FrozenOrderedDict
’s values.- Return type
ValuesView
[~VT
]
-
Copyright¶
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.