Accessors#
This module defines the accessors, that enable dictionary manipulations using index notation, and finding keys based on their values.
Accessor classes for better_dict.core.BetterDict
dictionary.
Classes#
This module defines the following classes:
VLoc
: Class allows the key retrieval using the dictionary values.ILoc
: Class allows access and setting values using index notation.
Notes
The VLoc
and ILoc
classes can be used by any dictionary-like
class. To create a new class that inherits from VLoc
or ILoc
you must define the following properties:
iloc
: property needed to implement theILoc
class.vloc
: property needed to implement theVLoc
class.
The following example shows how to implement them:
class MyDict(dict, VLoc, ILoc):
@property
def iloc(self):
return ILoc(self)
@property
def vloc(self):
return VLoc(self)
In the example above, the MyDict
class inherits from the dict
,
ILoc
and VLoc
classes. By defining the properties
iloc
and vloc
and passing self
to the ILoc
and
VLoc
, the dictionary keys and values can be accessed by the
ILoc
and VLoc
classes.
- class ILoc(data)[source]
Class for manipulating
BetterDict
using index notation.- Parameters:
data (
BetterDict
) – Dictionary to access as aBetterDict
object.
- __getitem__(index: int | Iterable[int]) Any [source]
Get the value of the dictionary at the given index(es).
- __setitem__(index: int | Iterable[int], value: Any | Iterable[Any]) None [source]
Set the value(s) of the dictionary at the given index(es).
- Variables:
data (
BetterDict
) – Dictionary to access as aBetterDict
object.
Examples
The following example shows how to use the
ILoc
class to access the values of aBetterDict
dictionary using index notation: >>> from better_dict import BetterDict >>> d = BetterDict({‘a’: 1, ‘b’: 2, ‘c’: 3}) >>> d.iloc[0] 1You can also use the
ILoc
class get multiple values at once: >>> d.iloc[0, 1] [1, 2] >>> d.iloc[[0, 1]] [1, 2] >>> d.iloc[1:] [2, 3]Class allows setting multiple values at once using index notation: >>> d.iloc[[0, 1]] = [10, 20] >>> d {‘a’: 10, ‘b’: 20, ‘c’: 3}
- __getitem__(index)[source]
Get the value of the dictionary at the given index(es).
- Parameters:
index (
int | Iterable[int]
) – Index(es) of the value(s) to get.- Returns:
Value(s) of the dictionary at the given index(es).
- Return type:
Any
- __setitem__(index, value)[source]
Set the value(s) of the dictionary at the given index(es).
To set multiple values at once, you need to specify the same number of indices and values, for example:
d.iloc[[0, 1]] = [10, 20]
- Parameters:
index (
int | Iterable[int]
) – Index(es) of the value(s) to set.value (
Any | Iterable[Any]
) – Value(s) to set.
- Return type:
None
- class VLoc(data)[source]
Class for accessing the keys of a dictionary by value.
- Parameters:
data (
BetterDict
) – Dictionary to access.
Examples
The following example shows how to use the
VLoc
class to access the keys of a dictionary by value. >>> from better_dict import BetterDict >>> d = BetterDict({‘a’: 1, ‘b’: 2, ‘c’: 3}) >>> d.vloc[1] [‘a’]Notes
The
VLoc
class only allows access to the keys of the dictionary by value. It does not allow manipulations of the dictionary keys or values.- __getitem__(value)[source]
Get the keys of the dictionary that match the value(s).
- Parameters:
value (
object | Iterable[object]
) – Value(s) to match.- Returns:
keys(s) of the dictionary that match the value(s).
- Return type:
List[Hashable]
- inverse()[source]
Get the inverse of the dictionary.
- Returns:
Inverse of the dictionary.
- Return type:
BetterDict