<commit_before>#!/usr/bin/env python

import chronos_tools
import sys

"""
    Ensure that the set of deployed chronos jobs
    matches the set expected.
"""

def cleanup_jobs(client, jobs, kill_existing_tasks=False):
    """ Maps a list of jobs to cleanup to a list of responses from the api (or exception objects)
    """
    def catch_exception(client, job):
        try:
            return client.delete(job)
        except Exception as e:
            """ We *have* to catch an Exception, because the client catches
                the more specific exception thrown by the http clients
                and rethrows an Exception -_-.

                The chronos api returns a 204 No Content when the delete is
                successful, and chronos-python only returns the body of the
                response from all http calls. So, if this is successful,
                then None will be returned.
                https://github.com/asher/chronos-python/pull/9

                we catch it here, so that the other deletes are completed.
            """
            return e

    return map(lambda job: (job, catch_exception(client, job)), jobs)

def jobs_to_delete(expected_jobs, actual_jobs):
    return list(set(actual_jobs).difference(set(expected_jobs)))

def format_list_output(title, job_names):
    return '%s\n  %s' % (title, '\n  '.join(job_names))

def running_job_names(client):
    return [job['name'] for job in client.list()]

def expected_job_names(service_job_pairs):
    """ Expects a list of pairs in the form (service_name, job_name)
    and returns the list of pairs mapped to the job name of each pair.
    """
    return [job[-1] for job in service_job_pairs]

def main():
    config = chronos_tools.load_chronos_config()
    client = chronos_tools.get_chronos_client(config)

    # get_chronos_jobs_for_cluster returns (service_name, job)
    expected_jobs = expected_job_names(chronos_tools.get_chronos_jobs_for_cluster())<commit_msg>extract try/catch of api call into function<commit_after>#!/usr/bin/env python

import chronos_tools
import sys

"""
    Ensure that the set of deployed chronos jobs
    matches the set expected.
"""
def execute_chronos_api_call_for_resource(api_call, job):
    try:
        return api_call(job)
    except Exception as e:
        """ We *have* to catch an Exception, because the client catches
            the more specific exception thrown by the http clients
            and rethrows an Exception -_-.

            The chronos api returns a 204 No Content when the delete is
            successful, and chronos-python only returns the body of the
            response from all http calls. So, if this is successful,
            then None will be returned.
            https://github.com/asher/chronos-python/pull/9

            we catch it here, so that the other deletes are completed.
        """
        return e

def cleanup_jobs(client, jobs):
    """ Maps a list of jobs to cleanup to a list of responses from the api (or exception objects) """
    return [(job, execute_chronos_api_call_for_resource(client.delete, job)) for job in jobs]


def jobs_to_delete(expected_jobs, actual_jobs):
    return list(set(actual_jobs).difference(set(expected_jobs)))

def format_list_output(title, job_names):
    return '%s\n  %s' % (title, '\n  '.join(job_names))

def running_job_names(client):
    return [job['name'] for job in client.list()]

def expected_job_names(service_job_pairs):
    """ Expects a list of pairs in the form (service_name, job_name)
    and returns the list of pairs mapped to the job name of each pair.
    """
    return [job[-1] for job in service_job_pairs]

def main():
    config = chronos_tools.load_chronos_config()
    client = chronos_tools.get_chronos_client(config)

    # get_chronos_jobs_for_cluster returns (service_name, job)
    expected_jobs = expected_job_names(chronos_tools.get_chronos_jobs_for_cluster())
{'fraction_non_alphanumeric': 0.0809086268504339, 'fraction_numerical': 0.002552322613578356, 'mean_word_length': 3.04020618556701, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1992648', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1111, 'n_words': 397}