<commit_before>from datetime import datetime

import sys

__author__ = '<NAME> <<EMAIL>>'


# TODO: add docs

class Chronograph():

    def __init__(self, name="Default", start_timing=False, verbosity=0, m_logger=None, log_lvl=None):
        self.name = name
        self.timing_data = []
        self.verbosity = verbosity
        if m_logger:
            self.print_fnc = getattr(m_logger, log_lvl.lower())
        else:
            self.print_fnc = sys.stdout.write

        if start_timing:
            self.start()

    def start(self, label=None):
        if not self.timing_data or "stop" in self.timing_data[-1]:
            self.timing_data.append({"start": datetime.now(), "label": label})
            return True
        else:
            self.print_fnc("Warning: Cannot start Chronograph in current state\n")
            return False

    def stop(self):
        if self.timing_data and "stop" not in self.timing_data[-1]:
            self.timing_data[-1]["stop"] = datetime.now()
            return True
        else:
            self.print_fnc("Warning: Cannot stop Chronograph in current state\n")
            return False

    def reset(self):
        self.timing_data = []
        return True

    def split(self, label=None):
        success = self.stop()
        if success:
            success = self.start(label=label)
        return success

    def to_json(self):
        return self.timing_data


if __name__ == "__main__":

    x = Chronograph()
    x.start("my label")
    x.stop()
    x.split("code part 2")
    x.split()
    print(x.to_json())



        # TODO: allow with Chronograph() mode

        # TODO: allow getting a Chronograph by name

        # TODO: allow decorating a function with a Chronograph
<commit_msg>add function to get elapsed time and split time<commit_after>from datetime import datetime, timedelta

import sys

__author__ = '<NAME> <<EMAIL>>'


# TODO: add docs

class Chronograph():

    def __init__(self, name=None, verbosity=0, m_logger=None, log_lvl=None, start_timing=False):
        self.name = name
        self.header = "Chronograph {}".format(name) if name else "Chronograph"
        self.timing_data = []
        self.verbosity = verbosity
        if m_logger:
            self.print_fnc = getattr(m_logger, log_lvl.lower())
        else:
            self.print_fnc = sys.stdout.write

        if start_timing:
            self.start()

    def start(self, label=None):
        if not self.timing_data or "stop" in self.timing_data[-1]:
            self.timing_data.append({"start": datetime.now(), "label": label})
            if self.verbosity >= 2:
                self.print_fnc("{}: started at: {}\n".format(self.header, self.timing_data[-1]["start"]))
            return True
        else:
            self.print_fnc("{}: Warning: Cannot start Chronograph while in current state! Stop or reset chronograph before starting.\n".format(self.header))
            return False

    def stop(self):
        if self.timing_data and "stop" not in self.timing_data[-1]:
            self.timing_data[-1]["stop"] = datetime.now()
            if self.verbosity >= 2:
                self.print_fnc("{}: stopped at: {}\n".format(self.header, self.timing_data[-1]["start"]))
            if self.verbosity >= 1:
                self.print_fnc("{}: Total elapsed time: {} s. Last split time: {} s.\n".format(self.header, self.total_elapsed_time, self.last_split_time))
            return True
        else:
            self.print_fnc("{}: Warning: Cannot stop Chronograph while in current state! Start chronograph first.\n".format(self.header))
            return False

    def reset(self):
        self.timing_data = []
        if self.verbosity >= 2:
            self.print_fnc("{}: Reset at: {}\n".format(self.header, self.timing_data[-1]["start"]))
            return True
        return False

    def split(self, label=None):
        success = self.stop()
        if success:
            success = self.start(label=label)
        return success

    @property
    def total_elapsed_time(self):
        elapsed_time = 0.0
        for t in self.timing_data:
            start_time = t['start']
            stop_time = t['stop'] if 'stop' in t else datetime.now()
            elapsed_time += (stop_time - start_time).total_seconds()

        return elapsed_time

    @property
    def last_split_time(self):
        for t in reversed(self.timing_data):
            if 'stop' in t:
                return (t['stop'] - t['start']).total_seconds()
        return 0


    def to_json(self):
        return self.timing_data


if __name__ == "__main__":

    x = Chronograph(name="test", verbosity=1)
    x.start("my label")
    x.split("code part 2")
    x.split()
    print(x.to_json())



        # TODO: allow with Chronograph() mode

        # TODO: allow getting a Chronograph by name

        # TODO: allow decorating a function with a Chronograph

{'fraction_non_alphanumeric': 0.11817440912795436, 'fraction_numerical': 0.004278728606356968, 'mean_word_length': 2.2253613666228644, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1175736', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1467, 'n_words': 391}