bdict¶
-
class
bdict
(seq=None, **kwargs)[source]¶ Bases:
collections.UserDict
Returns a new dictionary initialized from an optional positional argument, and a possibly empty set of keyword arguments.
Each
key: value
pair is entered into the dictionary in both directions, so you can perform lookups with either the key or the value.If no positional argument is given, an empty dictionary is created.
If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.
If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.
If an attempt is made to add a key or value that already exists in the dictionary a
ValueError
will be raised.Keys or values of
None
,True
andFalse
will be stored internally as"_None"
,"_True"
and"_False"
respectivelyBased on https://stackoverflow.com/a/1063393 by https://stackoverflow.com/users/9493/brian
Improved May 2020 with suggestions from https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/
Methods:
__contains__
(key)Return
key in self
.__delitem__
(key)Delete
self[key]
.__eq__
(other)Return
self == other
.__getitem__
(key)Return
self[key]
.__repr__
()Return repr(self).
__setitem__
(key, val)Set
self[key]
tovalue
.clear
()Removes all items from the
bdict
.get
()Return the value for
k
ifk
is in the dictionary, elsedefault
.items
()Returns a set-like object providing a view on the
bdict
’s items.keys
()Returns a set-like object providing a view on the
bdict
’s keys.pop
(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem
()as a 2-tuple; but raise KeyError if D is empty.
setdefault
(k[,d])update
([E, ]**F)If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values
()Returns an object providing a view on the
bdict
’s values.-
__repr__
()¶ Return repr(self).
-
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
bdict
’s items.- Return type
AbstractSet
[Tuple
[~KT
,~VT
]]
-
keys
()[source]¶ Returns a set-like object providing a view on the
bdict
’s keys.- Return type
AbstractSet
[~KT
]
-
pop
(k[, d]) → v, remove specified key and return the corresponding value.¶ If key is not found, d is returned if given, otherwise KeyError is raised.
-
popitem
() → (k, v), remove and return some (key, value) pair¶ as a 2-tuple; but raise KeyError if D is empty.
-
setdefault
(k[, d]) → D.get(k,d), also set D[k]=d if k not in D¶
-
update
([E, ]**F) → None. Update D from mapping/iterable E and F.¶ If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
-
values
()[source]¶ Returns an object providing a view on the
bdict
’s values.- Return type
ValuesView
[~VT
]
-