def dicttotext(d, sort=False, max=0):
    """Function for converting dictionary to plaintext output, optionally with some (reverse) sorting"""
    if sort:
        f = lambda x: sorted(x, key=lambda y: -1 * y[1])
    else:
        f = lambda x: x
    output = ''
    for i, (key, value) in enumerate(f(d.items())):
        output += key + '\t' + str(value) + '\n'
        if max != 0 and i >= max:
            break
    return output
