Maybe it’s just Friday afternoon brain… but I can’t figure this out.
I have a function that calls itself recursively to mimic the recursive behaviour of the old browseTags function (most of the script i stole from the forum with some changes), however the argument that is returned, ret, isn’t getting its value cleared each time the function is called… How can i ensure it’s cleared, without having to specify the default/cleared value when calling it. I want to just specify path, filter, and recursive
def browseTags(path, filter={}, recursive=False, ret=[]):
# First, browse for anything that can have children (Folders and UDTs, generally)
if str(path).find("[") == -1:
path = '[default]' + str(path)
if recursive:
results = system.tag.browse(path)
if results.getResults() is not None:
for branch in results.getResults():
if branch['hasChildren']:
# If something has a child node, then call this function again so we can search deeper.
# Include the filter, so newer instances of this call will have the same filter.
ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
# Call this function again at the current path, but apply the filter.
results = system.tag.browse(path, filter)
if results.getResults() is not None:
for result in results.getResults():
ret.append(result)
return ret
maybe You can collect the result… maybe this way (using for or something else): result.append(browseTags(path,filter,recursive))
or something like that
and clear it after the whole script was done…
def browseTags(path, filter={}, recursive=False, ret=[]):
# First, browse for anything that can have children (Folders and UDTs, generally)
if str(path).find("[") == -1:
path = '[default]' + str(path)
if recursive:
results = system.tag.browse(path)
if results.getResults() is not None:
for branch in results.getResults():
if branch['hasChildren']:
# If something has a child node, then call this function again so we can search deeper.
# Include the filter, so newer instances of this call will have the same filter.
ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
# Call this function again at the current path, but apply the filter.
results = system.tag.browse(path, filter)
if results.getResults() is not None:
for result in results.getResults():
ret.append(result)
ret2 = ret
ret = []
return ret2
Very strange… Maybe I don’t understand Python as well as I thought (which wasn’t really all that well let’s be honest)
Thanks for the ideas!
def browseTags(path, filter={}, recursive=False, ret=None):
if ret==None: ret=[]
# First, browse for anything that can have children (Folders and UDTs, generally)
if str(path).find("[") == -1:
path = '[default]' + str(path)
if recursive:
results = system.tag.browse(path)
if results.getResults() is not None:
for branch in results.getResults():
if branch['hasChildren']:
# If something has a child node, then call this function again so we can search deeper.
# Include the filter, so newer instances of this call will have the same filter.
ret = browseTags(path=branch['fullPath'], filter=filter, recursive=recursive,ret=ret)
# Call this function again at the current path, but apply the filter.
results = system.tag.browse(path, filter)
if results.getResults() is not None:
for result in results.getResults():
ret.append(result)
return ret