<commit_before>import contextlib
import datetime
import functools
import heapq
import time
from numbers import Number


class Timeline(object):

    def __init__(self, start_time=None):
        super(Timeline, self).__init__()
        current_time = self._real_time()
        if start_time is None:
            start_time = current_time
        self._forced_time = None
        self._scheduled = []
        self._time_factor = 1
        self._time_correction = TimeCorrection(start_time, current_time)

    def _real_sleep(self, seconds):
        time.sleep(seconds)

    def _real_time(self):
        return time.time()

    def set_time_factor(self, factor):
        """
        Sets the time factor -- the factor by which the virtual time advances compared to the real
        time. If set to 0, this means the virtual time does not progress at all until
        sleeps are performed
        """
        if factor < 0:
            raise ValueError("Cannot set negative time factor")
        self._correct_time()
        self._time_factor = factor

    def get_time_factor(self):
        """
        Retrieves the current time factor
        """
        return self._time_factor

    def freeze(self):
        """
        Shortcut for :func:`.set_time_factor`(0)
        """
        self.set_time_factor(0)

    def _correct_time(self):
        self._time_correction.virtual_time = self.time()
        self._time_correction.real_time = self._real_time()
        # shift stems from the previous correction...
        self._time_correction.shift = 0

    def sleep(self, seconds):
        """
        Sleeps a given number of seconds in the virtual timeline
        """
        if not isinstance(seconds, Number):
            raise ValueError(
                "Invalid number of seconds specified: {0!r}".format(seconds))
        if seconds < 0:
            raise ValueError("Cannot sleep negative number of seconds")
        if self._time_factor == 0:
            self.set_time(self.time() + seconds)
        else:
            self._real_sleep(seconds / self._time_factor)
        self.trigger_past_callbacks()

    def sleep_wait_all_scheduled(self):
        """
        Sleeps enough time for all scheduled callbacks to occur
        """
        while self._scheduled:
            self.sleep(max(0, self._scheduled[0].time - self.time()))<commit_msg>Fix sleep behavior for short time factors<commit_after>import contextlib
import datetime
import functools
import heapq
import time
from numbers import Number


class Timeline(object):

    def __init__(self, start_time=None):
        super(Timeline, self).__init__()
        current_time = self._real_time()
        if start_time is None:
            start_time = current_time
        self._forced_time = None
        self._scheduled = []
        self._time_factor = 1
        self._time_correction = TimeCorrection(start_time, current_time)

    def _real_sleep(self, seconds):
        time.sleep(seconds)

    def _real_time(self):
        return time.time()

    def set_time_factor(self, factor):
        """
        Sets the time factor -- the factor by which the virtual time advances compared to the real
        time. If set to 0, this means the virtual time does not progress at all until
        sleeps are performed
        """
        if factor < 0:
            raise ValueError("Cannot set negative time factor")
        self._correct_time()
        self._time_factor = factor

    def get_time_factor(self):
        """
        Retrieves the current time factor
        """
        return self._time_factor

    def freeze(self):
        """
        Shortcut for :func:`.set_time_factor`(0)
        """
        self.set_time_factor(0)

    def _correct_time(self):
        self._time_correction.virtual_time = self.time()
        self._time_correction.real_time = self._real_time()
        # shift stems from the previous correction...
        self._time_correction.shift = 0

    def sleep(self, seconds):
        """
        Sleeps a given number of seconds in the virtual timeline
        """
        if not isinstance(seconds, Number):
            raise ValueError(
                "Invalid number of seconds specified: {0!r}".format(seconds))
        if seconds < 0:
            raise ValueError("Cannot sleep negative number of seconds")
        if self._time_factor == 0:
            self.set_time(self.time() + seconds)
        else:
            end_time = self.time() + seconds
            while self.time() < end_time:
                self._real_sleep(max(0, (end_time - self.time()) / self._time_factor))
        self.trigger_past_callbacks()

    def sleep_wait_all_scheduled(self):
        """
        Sleeps enough time for all scheduled callbacks to occur
        """
        while self._scheduled:
            self.sleep(max(0, self._scheduled[0].time - self.time()))
{'fraction_non_alphanumeric': 0.0868041237113402, 'fraction_numerical': 0.0047422680412371136, 'mean_word_length': 2.218978102189781, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, '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': '4577848', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1306, 'n_words': 381}