import hashlib


def compute_checksums(file_object,
                      compute_md5=True,
                      compute_sha512=True,
                      close_file=True):
    """Computes checksums for a python file object

    :param file_object: Python File object
    :param compute_md5: whether to compute md5 default=True
    :type compute_md5: bool
    :param compute_sha512: whether to compute sha512, default=True
    :type compute_sha512: bool
    :param close_file: whether to close the file_object, default=True
    :type leave_open: bool

    :return: the checksums as {'md5sum': result, 'sha512sum': result}
    :rtype : dict
    """
    blocksize = 0
    results = {}
    if compute_md5:
        md5_hasher = hashlib.new('md5')
        blocksize = max(md5_hasher.block_size, blocksize)
        results['md5sum'] = md5_hasher
    if compute_sha512:
        sha512_hasher = hashlib.new('sha512')
        blocksize = max(sha512_hasher.block_size, blocksize)
        results['sha512sum'] = sha512_hasher
    update_fns = {'md5sum': lambda x, y: x.update(y),
                  'sha512sum': lambda x, y: x.update(y)}
    file_object.seek(0)
    for chunk in iter(lambda: file_object.read(32 * blocksize), ''):
        for key, val in results.items():
            update_fns[key](val, chunk)
    if close_file:
        file_object.close()
    else:
        file_object.seek(0)
    final_fns = {'md5sum': lambda x: x.hexdigest(),
                 'sha512sum': lambda x: x.hexdigest()}
    return {key: final_fns[key](val) for key, val in results.items()}