pyart.lazydict.LazyLoadDict¶
- class pyart.lazydict.LazyLoadDict(dic)[source]¶
A dictionary-like class supporting lazy loading of specified keys.
Keys which are lazy loaded are specified using the set_lazy method. The callable object which produces the specified key is provided as the second argument to this method. This object gets called when the value of the key is loaded. After this initial call the results is cached in the traditional dictionary which is used for supplemental access to this key.
Testing for keys in this dictionary using the “key in d” syntax will result in the loading of a lazy key, use “key in d.keys()” to prevent this evaluation.
The comparison methods, __cmp__, __ge__, __gt__, __le__, __lt__, __ne__, nor the view methods, viewitems, viewkeys, viewvalues, are implemented. Neither is the the fromkeys method.
- Parameters
dic (dict) – Dictionary containing key, value pairs which will be stored and evaluated traditionally. This dictionary referenced not copied into the LazyLoadDictionary and hence changed to this dictionary may change the original. If this behavior is not desired copy dic in the initalization.
Examples
>>> d = LazyLoadDict({'key1': 'value1', 'key2': 'value2'}) >>> d.keys() ['key2', 'key1'] >>> lazy_func = lambda : 999 >>> d.set_lazy('lazykey1', lazy_func) >>> d.keys() ['key2', 'key1', 'lazykey1'] >>> d['lazykey1'] 999
initalize.
|
|
|
Return a copy of the dictionary. |
|
|
|
True if dictionary has key, else False. |
|
|
|
|
|
If key is not found, d is returned if given, otherwise KeyError is raised. |
|
as a 2-tuple; but raise KeyError if D is empty. |
|
Set a lazy key to load from a callable object. |
|
|
|
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v |
|