Core#
Module defines the BetterDict
class.
The BetterDict
class is a subclass of the dict
class that
provides additional functionality to the dictionary. The additional
functionality includes:
Accessing the dictionary keys by value.
Manipulating the dictionary keys and values using index notation.
Accessing and manipulating the dictionary values using dot notation.
Accessing the dictionary values by their data types.
Saving and loading the dictionary to and from a file.
Creating the dictionary from a
pandas.DataFrame
object.Creating the dictionary from a
numpy.ndarray
object.Creating the dictionary from a
numpy.matrix
object.Creating the dictionary from a
pandas.Series
object.Applying a function to the dictionary values and keys.
Finding keys using fuzzy matching.
Renaming the dictionary keys.
- class ApplyMixin[source]#
Mixin class that adds the apply method to the
BetterDict
class.The
apply
method enables operations on the values or keys of the dictionary.- apply(func, *args, axis=1, **kwargs)[source]#
Apply a function to the
BetterDict
keys or values.- Parameters:
func (
callable
) – Function to apply.*args – Positional arguments to pass to the function.
**kwargs – Keyword arguments to pass to the function.
axis (
int
, default1
) – Axis along which to apply the function. Ifaxis=0
, the function is applied to the keys of the dictionary. Ifaxis=1
, the function is applied to the values of the dictionary.
- Returns:
Result of applying the function to the object.
- Return type:
Notes
The class mimics the
pandas.DataFrame.apply
method.
- keys_apply(func, *args, **kwargs)[source]#
Apply the function to the keys of the dictionary.
- Parameters:
func (
callable
) – Function to apply.*args – Positional arguments to pass to the function.
**kwargs – Keyword arguments to pass to the function.
- Returns:
Result of applying the function to the keys of the object.
- Return type:
- abstract rename(keys)[source]#
Rename the keys of the dictionary.
- Parameters:
keys (dict) –
- Return type:
None
- value_apply(func, *args, **kwargs)[source]#
Apply the function to each value of a dictionary.
- Parameters:
func (
callable
) – Function to apply.*args – Positional arguments to pass to the function.
**kwargs – Keyword arguments to pass to the function.
- Returns:
Result of applying the function to each row of the object.
- Return type:
- class BetterDict[source]#
Custom dictionary class that allows multiple get/set operations at once.
Class also supports the following operations:
Access and set values using index notation.
Access keys referrencing the dictionary values.
Get the keys and values as lists.
Get the closest match to a given key.
Select a subset of the dictionary based on the values data types.
Apply functions row- and column-wise.
Perform I/O operations using
pickle
andjoblib
.
Examples
Create a
BetterDict
instance from a normal dictionary: >>> d = BetterDict({“a”: 1, “b”: 2}) >>> d[“a”] 1Get multiple values at once >>> d[“a”, “b”] {‘a’: 1, ‘b’: 2}
Set multiple values at once >>> d[“a”, “b”] = 3, 4 >>> d[“a”, “b”] {‘a’: 3, ‘b’: 4}
Access values using index notation >>> d.iloc[0] 3
Set values using index notation >>> d.iloc[0, 1] = [5, 6] >>> d.iloc[:] [5, 6]
Get the keys and values as lists >>> d.keys() # Dictionary keys [‘a’, ‘b’] >>> d.values() # Dictionary values [5, 6]
Get the closest match to a given key >>> d.get_closest_match(“A”) ‘a’
- __getitem__(key)[source]#
Get the value(s) associated with the key(s).
- Parameters:
key (
Hashable | Iterable[Hashable]
) – Key(s) to get the value(s) for.- Returns:
Value(s) associated with the key(s).
- Return type:
object | BetterDict
- __setattr__(name, value)[source]#
Set the value associated with the key.
- Parameters:
name (
str
) – Key to set the value for.value (
Any
) – Value to set.
- __setitem__(key, value)[source]#
Set the value(s) associated with the key(s).
- Parameters:
key (
Hashable | Iterable[Hashable]
) – Key(s) to set the value(s) for.value (
Any | Iterable[Any]
) – Value(s) to set.
- close_match(key, cutoff=0.6)[source]#
Return the key that is the closest match to the name from
key
.Before applying the fuzzy match, the
key
and the dictionary keys are converted to lower case. This maximizes the chances of finding a match.- Parameters:
key (
Hashable
) – Key to find the closest match for.cutoff (
float
, default0.6
) – Minimum similarity ratio for a match to be returned. The parameter must be between 0 and 1. A value of 1 means that the strings must be identical. A value close to 0 means that the strings don’t have to be identical, for a match to be returned.
- Returns:
Closest match to the given key.
- Return type:
Hashable
- Raises:
KeyError – If no match is found.
- dtypes()[source]#
Get the data types of the dictionary values.
- Returns:
Data types of the dictionary values.
- Return type:
- classmethod from_frame(pandas_df)[source]#
Create a
BetterDict
from a pandas DataFrame.- Parameters:
pandas_df (
pd.DataFrame
) – Pandas DataFrame to create theBetterDict
from.- Returns:
BetterDict
created from the pandas DataFrame.- Return type:
- classmethod from_list(list_obj)[source]#
Create a
BetterDict
from a list.- Parameters:
list_obj (
list
) – List to create theBetterDict
from.- Returns:
BetterDict
created from the list.- Return type:
- classmethod from_series(pandas_series)[source]#
Create a
BetterDict
from a pandas Series.- Parameters:
pandas_series (
pd.Series
) – Pandas Series to create theBetterDict
from.- Returns:
BetterDict
created from the pandas Series.- Return type:
- property iloc: ILoc#
Access the dictionary values by index.
- Returns:
Dictionary values accessed by index.
- Return type:
ILoc
- property index: List[int]#
Get the indexes of the dictionary.
- Returns:
Indexes of the dictionary.
- Return type:
List[int]
- keys()[source]#
Return a list of the dictionary keys.
- Returns:
List of the dictionary keys.
- Return type:
List[Hashable]
- rename(keys)[source]#
Rename the keys of the dictionary.
- Parameters:
keys (
dict
) – New keys for the dictionary.
- values()[source]#
Return a list of the dictionary values.
- Returns:
List of the dictionary values.
- Return type:
List[Any]
- property vloc: VLoc#
Access the dictionary values by value.
- Returns:
Dictionary values accessed by value.
- Return type:
VLoc
- translate_dtype(dtype)[source]#
Translate the
dtype
string to the corresponding Python type.- Parameters:
dtype (
str
) – Data type represented as string to translate.- Returns:
Type or tuple of types corresponding to the text data types.
- Return type:
type | Tuple[type
,]
- Raises:
ValueError – If the data type string is not recognized.