In my current project, I am working with a lot of JSON objects that contain arrays, er.. lists so I setup a decorator for convienece when the list is one item or multiple. Even though this is convienent in the current project, is it reusable code?def collapse(function):    @functools.wraps(function)    def func(*args, **kwargs):        call = function(*args, **kwargs)        if isinstance(call, (list, tuple)) and (len(call) == 1):            return call[0]        return call    return func@collapsedef get_results(query, wrapper=None):    # get json object.    result = result.json() # using the requests library.    if wrapper:        return result[wrapper]    return resultSo, get_results() has the potential of returning either a list or a dict. In most cases, the code knows when what type is returned, so using the @collapse decorator changes [result_dict] to just result_dictIs this a good practice, or should I write a decorator that takes a limit parameter@limit(1) # def limit(items=None): ... return data[0:items] if items else datadef get_results(query, wrapper=None):Just wrote out the limit decorator...def limit(items=None, start=0, collapse=False):    if items and (start > 0):        items += start    def wrapper(function):        @functools.wraps(function)        def func(*args, **kwargs):            call = function(*args, **kwargs)            if isinstance(call, (list, tuple)):                results = call[start:items] if items else call                if collapse and (len(results) == 1):                    return results[0]                else:                    return results            return call        return func    return wrapper
Proper use or convenience, or both?
python