Package: deep-collections
Deep Collectionsdeep_collections is a Python library that provides tooling for easy access to deep collections (dicts, lists, deques, etc), while maintaining a great portion of the collection's original API. The class DeepCollection class will automatically subclass the original collection that is provided, and add several quality of life extensions to make using deep collections much more enjoyable.Got a bundle of JSON from an API? A large Python object from some data science problem? Some very lengthy set of instructions from some infrastructure as code like Ansible or SaltStack? Explore and modify it with ease.DeepCollection can take virtually any kind of object including all built-in container types (dict,list,set, andtuple), everything in thecollections module, anddotty-dicts, and all of these nested in any fashion.FeaturesPath traversal by supplying a list of path components as a key. This works for getting, setting, and deleting.Accessing nested components by supply only path fragments.Setting paths when parent parts do not exist.Path traversal through dict-like collections by dot chaining for gettingFinding all paths to keys or subpathsFinding all values for keys or subpaths, and deduping them.Provide all of the above through a class that is:easily instantiablea native subclass of the type it was instantiated witheasily subclassablePath conceptDeepCollections has a concept of a "path" for nested collections, where a path is a sequence of keys or indices that if followed in order, traverse the deep collection. As a quick example,{'a': ['b', {'c': 'd'}]}could be traversed with the path['a', 1, 'c']to find the value'd'.DeepCollections natively use paths as well as simple keys and indices. Fordc = DeepCollection(foo), items can be retrieved through the familiardc[path]as normal ifpathis a simple key or index, or if it is a non-stringlike iterable path (strings are assumed to be literal keys). This is done with a custom__getitem__method. Similarly,__delitem__and__setitem__also support using a path. The same flexibility exists for the familiar methods like.get, which behaves the same asdict.get, but can accept a path as well as a key.MatchingPath elements are interpretted as patterns to match against keys and indices. By default this feature is on and uses globbing.Recursion"**"recurses any depth to find the match for the next pattern given. For example:dc=DeepCollection({"a":{"b":{"c":{"d":5}}},"d":4})dc["a","**","d"]==5Coupled with another matching style like globbing allows you to do some powerful filtering:dc=DeepCollection({"a":{"b":{"c":{"xd":{"e":0},"yd":{"e":1},"zf":{"e":2}}}},"e":3})dc["a","**","?d","e"]==[0,1]This feature is independent of other matching patterns. In other words, you could swap globbing out for another matchin style, but"**"will remain usable unless disabled on it's own. You might want to use regex through your path but pair that with recursion.Matching numeric keys and indiciesTo enable pattern matching (like globbing) to make sense when attempting to match indices and numeric keys, if a path element is a string and appears to use globbing, it will be matched against the stringified index/key. In other wordsdc=DeepCollection(["a","b","c"])dc["[0-1]"]==DeepCollection(["a","b"])dc["[5]"]==DeepCollection([])# Matching pattern detected (globbing), so no results yields an empty list.dc["5"]# Raises TypeError. No matching pattern detected, so direct use of `"5"` was attempted and not cast to an int.dc=DeepCollection({1:'i','1':'j','a':'k'})dc['*[!1]']=="k"This is a compromise to afford pattern matching indices and numeric keys. As with deeper path traversal, since we're matching a pattern, 0 hits is not treated as a KeyError or IndexError, but simply returns an empty list.The often relied upon KeyError and IndexError are both saved when pattern matching is not detected.dc=DeepCollection(["a","b","c"])dc[5]...IndexError:listindexoutofrangeDeepCollection({})["a"]...KeyError:'a'Matching StylesDeep Collections supports the following matching styles:globregexequalityhashglob+regexcustom (built in soon)This can be set with many functions by passing e.g.match_with="regex".As said above, the special use of"**"is independant, and currently always on. Future versions will allow toggling this off as well.To abandon all matching styles and traverse paths as quickly as possible, usegetitem_by_path_strict.Matching Style: GlobbingAny given path element is matched withfnmatchcasefromthe Python stdlib. This style is used in the above examples.Matching Style: RegexAny given path element is matched withre.compile().match()fromthe Python stdlib.DeepCollection object APIDeepCollections are instantiated as a normal class, optionally with a given initial collection as an arguement.fromdeep_collectionsimportDeepCollectiondc=DeepCollection()# ordc=DeepCollection({"a":{"b":{"c":"d"}}})# ordc=DeepCollection(["a",["b",["c","d"]]])These are the noteworthy methods available on all DCs:__getitem____delitem____setitem__getpaths_to_valuepaths_to_keyvalues_for_keydeduped_values_for_keyThere are also corresponding functions availble that can use any native object that could be deep, but is not aDeepCollection, like a normal nesteddictorlist. This may be a convenient alternative to ad hoc traverse an object you already have, but it is also faster to use because it doesn't come with the initialization cost of a DeepCollection object. So if speed matters, use a function.deep_collections function APIAll of the useful methods for DeepCollection objects are available as functions that can take a collection as an argument, as well as several other supporting functions, which are made plainly availble.The core functions are focused on using the same path concept. The available functions and their related DC methods are:getitem_by_path-DeepCollection().__getitem__get_by_path-DeepCollection().getset_by_path-DeepCollection().set_by_pathdel_by_path-DeepCollection().del_by_pathpaths_to_value-DeepCollection().paths_to_valuepaths_to_key-DeepCollection().paths_to_keyvalues_for_key-DeepCollection().values_for_keydeduped_values_for_key-DeepCollection().deduped_values_for_keydedupe_itemsresolve_pathmatched_keys