from datetime import datetime


def sanitize(output):

    """Sanitize a dictionary or a list of dictionaries as follows:

       * remove 'tel:+'/'alias:' prefixes from 'to'/'from'/'address' keys' values
       * add 'obfuscated' key when 'from' key is present
       * convert 'timestamp' keys' values to datetime object

    """

    if isinstance(output, dict):
        for (k, v) in output.items():
            if k in ('from', 'to', 'address'):
                obfuscated = v.startswith('alias:')
                output[k] = v[6:] if obfuscated else v[5:]
                if k == 'from':
                    output[u'obfuscated'] = obfuscated
            elif k == 'timestamp':
                output[k] = datetime.strptime(v, '%Y-%m-%dT%H:%M:%S.%f+0000')

        return output
    elif isinstance(output, list):
        return [sanitize(item) for item in output]
    else:
        return output