kutils: Kyle’s Utilities¶
These are various data structures and tools that I think are useful and want to re-use across projects.
Introduction¶
Most programmers will have their own collection of useful code they’d like to reuse across projects. This is mine. It follows in spirit with my Lisp kutils package and the Go analogue. Note that these three libraries don’t really have much overlap, and reflect the things I tend to use each programming language for.
Dictionaries¶
There are a few utility dictionaries available. All of them inherit from the
base dict
class, and can be used as drop-ins for the standard dict
type. There are a few “primitive” base types that are then composed to
more complex types. To use these types, import kutils.dicts
.
AttrDict
: this dictionary provides access to the keys as attributes.NoneDict
: this dictionary returnsNone
for non-existent keys. This means it will not throw aKeyError
for top-level keys; note that nested dictionaries may not be `NoneDict`s.StrDict
: this dictionary returns an empty string for non-existent keys.DictDict
: this dictionary returns an emptyStrDict
for non-existent keys. This is mostly useful for values that are nested dictionaries of keys.AttrNoneDict
: this is a composition of theAttrDict
and theNoneDict
: it provides access to its keys as attributes, and returnsNone
for missing keys or attributes; this means missing top-level keys and attributes will not throwKeyError
orAttributeError
.AttrDictDict
: this is a composition of theAttrDict
andDictDict
: it provides access to its keys as attributes, and returns an emptyStrDict
for missing keys or attributes; this means missing top-level keys and attributes will not throwKeyError
orAttributeError
.
API reference: Dictionaries¶
The kutils.dicts
module provides several utility dictionaries.
dicts contains a number of special-case dictionaries.
-
class
kutils.dicts.
AttrDict
(*args, **kwargs)[source]¶ AttrDict represents a dictionary where the keys are represented as attributes.
>>> d = kd.AttrDict(foo='bar') >>> d.foo 'bar' >>> d['foo'] 'bar'
-
class
kutils.dicts.
AttrDictDict
(*args, **kwargs)[source]¶ AttrDictDict is a dictionary that returns an empty StrDict if a key is unavailable. This is meant for a dictionary of dictionaries of string values.
>>> d = kd.AttrDictDict() >>> d.foo {} >>> d['foo'] {} >>> d.foo.bar '' >>> d['foo']['bar'] ''
-
class
kutils.dicts.
AttrNoneDict
(*args, **kwargs)[source]¶ AttrNoneDict returns an AttrDict that returns None if a key isn’t present.
>>> d = kd.AttrNoneDict(foo='bar') >>> d.foo 'bar' >>> d['foo'] 'bar' >>> d.bar is None True >>> d['bar'] is None True
-
class
kutils.dicts.
AttrStrDict
(*args, **kwargs)[source]¶ AttrStrDict returns an AttrDict that returns an empty string if a key isn’t present.
>>> d = kd.AttrStrDict(foo='bar') >>> d.foo 'bar' >>> d['foo'] 'bar' >>> d.bar '' >>> d['bar'] ''
-
class
kutils.dicts.
DictDict
(*args, **kwargs)[source]¶ DictDict is a dictionary that returns an empty AttrStrDict if a key is unavailable. This is meant for a dictionary of dictionaries of string values.
>>> d = kd.DictDict() >>> d['foo'] {} >>> type(d['foo']) <class 'kutils.dicts.AttrStrDict'> >>> d.foo.bar ''