Package: kdict
Kdict: dict with multi-dimensional, sliceable keyskdictis likedictfor multi-dimensional keys. Withkdict, you can easily filter and slice your dictionary by key dimensions.Example: machine learning model evaluation.Suppose you're evaluating several models on three cross validation folds, each with a training set and a test set.Beforekdict, you might store evaluation scores in a nested dictionary. But that's cumbersome and error-prone. Here's what it would take to get the mean accuracy for a particular model across all folds:# To access inner nested data without kdict, you'd need to write iterators like this:importnumpyasnpnp.mean([data[fold_id][fold_label]["lasso"]forfold_idindata.keys()forfold_labelindata[fold_id].keys()])kdictmakes storing and accessing this type of data a breeze. No more nesting:# Store data in a three-dimensional kdict.# Dimensions: fold ID, fold label, model namedata=kdict(...)# Slice the kdict to get lasso model's mean accuracy across all folds:# data[:, :, 'lasso'] is a subset of the full dictionarynp.mean(list(data[:,:,'lasso'].values()))In this example,datais a three-dimensionalkdictthat you can slice along any dimension. So how did we make this kdict?fromkdictimportkdictdata=kdict()# make a blank kdictforfold_idinrange(3):forfold_labelin['train','test']:formodel_namein['lasso','randomforest']:# add an entry for each fold ID, fold label, and model namedata[fold_id,fold_label,model_name]=get_model_score(fold_id,fold_label,model_name)The syntax, in a nutshell:Read or write a single element by accessing[key_dimension_1, key_dimension_2]and so on.Or get a subset of the dictionary by slicing, e.g.[:, key_dimension_2].InstallationpipinstallkdictUsageCreate akdictImport:from kdict import kdictCreate a blankkdict:data = kdict(). Or initialize from an existing dict:data = kdict(existing_dict). You can also use a dict comprehension there, such as:data=kdict({(fold_id,fold_label,model_name):get_model_score(fold_id,fold_label,model_name)formodel_namein['lasso','randomforest']forfold_labelin['train','test']forfold_idinrange(3)})Slice akdictAccess an individual item withdata[0, 'train', 'lasso'].Or get a subset of the dictionary with slices:data[0, :, :]will have all items where the first dimension of the key is 0. This slice is also akdict, so you can keep slicing and filtering further.You can also iterate over specific key dimensions:# get final dimension of the keysavailable_models=data.keys(dimensions=2)# or get all pairs of first two dimensionsforfold_id,fold_labelindata.keys(dimensions=[0,1]):...# now do something with data[fold_id, fold_label, :]EjectAkdictbehaves just like adict, except all keys must have the same number of dimensions.To get a rawdictback, calldata.eject().DevelopmentSubmit PRs againstdevelopbranch, then make a release pull request tomaster.# Install requirementspipinstall--upgradepipwheel
pipinstall-rrequirements_dev.txt# Install local packagepipinstall-e.# Install pre-commitpre-commitinstall# Run testsmaketest# Run lintmakelint# bump version before submitting a PR against master (all master commits are deployed)bump2versionpatch# possible: major / minor / patch# also ensure CHANGELOG.md updatedChangelog0.0.1First release on PyPI.