Better Dict
Description
Python dictionary on steroids. The custom dictionary is inspired in
by the functionalities that pandas offers in
their DataFrame and Series classes.
Installation
To install Better Dict, execute the command:
$ pip install better-dict
Quickstart
Here's a quick example of how to use Better Dict:
import better_dict as bd
d = bd.BetterDict({"a": 1, "b": 2, "c": 3})
# == Accessing values ==========================================================
# Access multiple keys at once:
d[["a", "b"]] # returns {"a": 1, "b": 2}
# Access dictionary values using item indexes:
d.iloc[0] # returns 1
d.iloc[[0, 2]] # returns [1, 3]
d.iloc[1:] # returns [2, 3]
# Access dictionary keys using their values:
d.vloc[1] # returns "a"
d.vloc[[1, 3]] # returns ["a", "c"]
# == Key Translations ==========================================================
# Rename dictionary keys:
d.rename({"a": "A", "b": "B", "c": "C"}) # returns {"A": 1, "B": 2, "C": 3}
# == Apply Function ============================================================
# Apply a function to all dictionary values:
d.apply(lambda x: x + 1) # returns {"a": 2, "b": 3, "c": 4}
# Apply a function to all dictionary keys:
d.apply_keys(lambda x: x.upper(), axis=0) # returns {"A": 1, "B": 2, "C": 3}
# == I/O Operations ============================================================
# Save dictionary to a Pickle file:
d.to_pickle("d.pkl")
# Load dictionary from a Pickle file:
d = bd.BetterDict.from_pickle("d.pkl")
# Save dictionary to a joblib file:
d.to_joblib("d.joblib")
# Load dictionary from a joblib file:
d = bd.BetterDict.from_joblib("d.joblib")
Q&A
1. What is the BetterDict class and what additional functionality does it provide?
The BetterDict class is a custom subclass of Python's built-in dict class,
designed to provide additional functionality for easier and more flexible
manipulation of dictionaries. The main enhancements include:
- Accessing dictionary keys by value.
- Manipulating dictionary keys and values using index notation.
- Accessing and manipulating dictionary values using dot notation.
- Other features include saving/loading dictionaries to/from files, creating dictionaries from various data structures, applying functions to dictionary values and keys, fuzzy key matching, and renaming dictionary keys.
2. How can I access and set values in a BetterDict instance?
Accessing and setting values in a BetterDict instance is made easy through a
variety of methods:
Get/Setvalues by key: Use the standard dictionary syntax with square brackets (e.g.,d["key"]andd["key"] = value).- Get/Set multiple values at once: Supply an iterable of keys
(e.g.,
d["key1", "key2"]andd["key1", "key2"] = value1, value2). - Index notation: Use the iloc property to access and set values by index
(e.g.,
d.iloc[index]andd.iloc[index1, index2] = value1, value2). - Additionally, dot notation can be used to access and set values (e.g.,
d.keyandd.key = value).
3. What are the available I/O operations for BetterDict and how can I use them?
BetterDict supports I/O operations using the pickle and joblib libraries,
allowing you to easily save and load dictionaries to/from files. The main
methods for I/O operations are:
- Save with pickle: Use the
save_picklemethod, supplying the file path (e.g.,d.save_pickle("file_path.pkl")). - Load with pickle: Use the
load_picklemethod, supplying the file path (e.g.,d = BetterDict.load_pickle("file_path.pkl")). - Save with joblib: Use the
save_joblibmethod, supplying the file path (e.g.,d.save_joblib("file_path.joblib")). - Load with joblib: Use the
load_joblibmethod, supplying the file path (e.g.,d = BetterDict.load_joblib("file_path.joblib")).
4. How can I create a BetterDict from different data structures like pandas.DataFrame or numpy.ndarray?
BetterDict offers class methods to create instances from various data
structures, such as pandas DataFrames, pandas Series, numpy arrays, and lists:
- From
pandas.DataFrame: Use thefrom_framemethod (e.g.,d = BetterDict.from_frame(data_frame)). - From
pandas.Series: Use thefrom_seriesmethod (e.g.,d = BetterDict.from_series(data_series)). - From
numpy.ndarray: No direct method is available, but you can first convert the array to a pandas DataFrame and then usefrom_frame(e.g.,d = BetterDict.from_frame(pd.DataFrame(array))). - From list: Use the
from_listmethod (e.g.,d = BetterDict.from_list(list_obj)).
These methods facilitate easy conversion between different data structures and BetterDict.
Contributing
Contributions are welcome! If you have any suggestions or feature requests, please open an issue or submit a pull request.
For more information on how to contribute to Better Dict, please read the Contributor Guide.
License
Distributed under the terms of the MIT License, Better Dict is free and open source software.
Metadata
Release files for better-dict 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| better_dict-0.0.1.tar.gz | 14.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| better_dict-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.8 kB
Release files / better_dict-0.0.1.tar.gz
| Download URL | better_dict-0.0.1.tar.gz |
|---|---|
| Size | 14.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebb84949c3a1abe609db25cd7f6ab1ae1ecdd733ad12fc7268dcb71bb229cb80
|
|
BLAKE2b-256 checksum How to use checksums |
d2821e25dedd78444ccd26561a79886153bdcfef4c62eb01d249ad8e7562d559
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.12 Darwin/22.1.0
|
Release files / better_dict-0.0.1-py3-none-any.whl
| Download URL | better_dict-0.0.1-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aee58d0b3ed3a1e10ad701b6a8ef8ce6d9e7915adf15a7c1b381bf3a2e778c06
|
|
BLAKE2b-256 checksum How to use checksums |
7f1e71d237d2c49357611b0a0116cd6a241a6cb78c85d685a23f3bbb720f67e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.12 Darwin/22.1.0
|