I want a function that usually takes an argument of type X, where X is either a scalar, or a list, or a dict, and returns a list X with the same key values based on other information.
def foo(info, k):
return [bar(item,k) for item in processInfo(info)]
def bar(item, keydata):
if keydata is a scalar:
return item[keydata]
elif keydata is a list:
return [item[k] for k in keydata]
elif keydata is a dict:
return dict((k,item[v]) for (k,v) in keydata.iteritems())
else:
raise ValueError('bar expects a scalar, list, or dict')
My question is: how can I send between the three types?
edit: The string should be interpreted as a scalar, not a list / iterable. Tuples must be interpreted as iterable.
edit 2: I want the duck to print, not strict typing.
source
share