Proposing fetch() to work with nested dictionaries

Proposed improvement to fetch()

  • Allow key=[lst] to target nested dictionaries
    fetch(key=[‘testStored’, ‘TestStored’, ‘key1’])

fetch() is particularly useful in that if the item is not found you can set default to return a default value.

fetch(key=‘item’, default='not found')

You can even set default as a function which makes it particularly useful for error handling. For example:

i = me.parent()
def do_something(myArg):
	somethingElse = "custom error handling"
	return somethingElse

i.store('hello', 'world')

lookFor = 'hello'
o = i.fetch(lookFor, do_something(lookFor)) 

print(o) # returns world

lookFor = 'yo'
o = i.fetch(key=lookFor, default=do_something(lookFor))

print(o) # returns 'custom error handling'

Target Nested Dictionaries

If my understanding is correct, fetch() only works on top level storage. It would be nice if you could fetch nested dictionary items via a list.
For example:
fetch(key=[‘nested’, ‘dictionary’, ‘key’])

This would be particularly useful for extensions where extensions dictionaries are always nested in a dictionary named <extension_name>Storage.

from TDStoreTools import StorageManager
import TDFunctions as TDF

class test:
	def __init__(self, ownerComp):
		self.ownerComp = ownerComp

        # Initiate testStorage
		storedItems = [
			{
				'name': 'TestStorage',
				'default': {
							'key1': 'val1',
							'key2': 'val2',
							'key3': 'val3',
							},
				'readOnly': False,
				'property': True,
				'dependable': True
			},
		]
		self.stored = StorageManager(self, ownerComp, storedItems)

`fetch` can only access top level storage.

i = me.parent()
o = i.fetch('testStored')
print(o) # {'TestStorage': {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}}

Would be great if this was possible!

o = i.fetch([‘testStored’, ‘TestStored’, ‘key1’])
print(o) # Print 'val1'

Thoughts on this idea?