<commit_before>#PyJ2D - Copyright (C) 2011 <NAME> <https://gatc.ca/>
#Released under the MIT License <https://opensource.org/licenses/MIT>

from java.lang import Thread, System, InterruptedException
from javax.swing import Timer
from java.awt.event import ActionListener
from java.util.concurrent.atomic import AtomicBoolean
from pyj2d import env

__docformat__ = 'restructuredtext'


class Clock(object):
    """
    **pyj2d.time.Clock**
    
    * Clock.get_time
    * Clock.tick
    * Clock.tick_busy_loop
    * Clock.get_fps
    """

    _repaint_sync = None

    def __init__(self):
        self._time = System.nanoTime() // 1000000
        self._time_init = self._time
        self._time_diff = [33 for i in range(10)]
        self._pos = 0
        self._thread = Thread()

    def get_time(self):
        """
        Return time (in ms) between last two calls to tick().
        """
        return self._time_diff[self._pos]

    def tick(self, framerate=0):
        """
        Call once per program cycle, returns ms since last call.
        An optional framerate will add pause to limit rate.
        """
        while self._repaint_sync.get():
            try:
                self._thread.sleep(1)
            except InterruptedException:
                Thread.currentThread().interrupt()
                break
        self._time = System.nanoTime() // 1000000
        if framerate:
            time_pause = ( (1000 // framerate)
                           - (self._time - self._time_init) )
            if time_pause > 0:
                try:
                    self._thread.sleep(time_pause)
                except InterruptedException:
                    Thread.currentThread().interrupt()
                self._time = System.nanoTime() // 1000000
        if self._pos:
            self._pos -= 1
        else:
            self._pos = 9
        self._time_diff[self._pos] = self._time - self._time_init
        self._time_init = self._time
        return self._time_diff[self._pos]

    def tick_busy_loop(self, framerate=0):
        """
        Call once per program cycle, returns ms since last call.
        An optional framerate will add pause to limit rate.
        """
        return self.tick(framerate)

    def get_fps(self):
        """
        Return fps.
        """
        return 1000.0 / (sum(self._time_diff) / 10)

<commit_msg>revise time tick framerate handling<commit_after>#PyJ2D - Copyright (C) 2011 <NAME> <https://gatc.ca/>
#Released under the MIT License <https://opensource.org/licenses/MIT>

from java.lang import Thread, System, InterruptedException
from javax.swing import Timer
from java.awt.event import ActionListener
from java.util.concurrent.atomic import AtomicBoolean
from pyj2d import env

__docformat__ = 'restructuredtext'


class Clock(object):
    """
    **pyj2d.time.Clock**
    
    * Clock.get_time
    * Clock.tick
    * Clock.tick_busy_loop
    * Clock.get_fps
    """

    _repaint_sync = None

    def __init__(self):
        self._time = System.nanoTime() // 1000000
        self._time_init = self._time
        self._time_diff = [33 for i in range(10)]
        self._pos = 0
        self._framerate = 0
        self._frametime = 0
        self._thread = Thread()

    def get_time(self):
        """
        Return time (in ms) between last two calls to tick().
        """
        return self._time_diff[self._pos]

    def tick(self, framerate=0):
        """
        Call once per program cycle, returns ms since last call.
        An optional framerate will add pause to limit rate.
        """
        while self._repaint_sync.get():
            try:
                self._thread.sleep(1)
            except InterruptedException:
                Thread.currentThread().interrupt()
                break
        self._time = System.nanoTime() // 1000000
        if framerate:
            if framerate != self._framerate:
                self._framerate = framerate
                self._frametime = int(1000 / framerate)
            time_pause = self._frametime - (self._time-self._time_init)
            if time_pause > 0:
                try:
                    self._thread.sleep(time_pause)
                except InterruptedException:
                    Thread.currentThread().interrupt()
                self._time = System.nanoTime() // 1000000
        if self._pos:
            self._pos -= 1
        else:
            self._pos = 9
        self._time_diff[self._pos] = self._time - self._time_init
        self._time_init = self._time
        return self._time_diff[self._pos]

    def tick_busy_loop(self, framerate=0):
        """
        Call once per program cycle, returns ms since last call.
        An optional framerate will add pause to limit rate.
        """
        return self.tick(framerate)

    def get_fps(self):
        """
        Return fps.
        """
        return 1000.0 / (sum(self._time_diff) / 10)


{'fraction_non_alphanumeric': 0.09775967413441955, 'fraction_numerical': 0.020773930753564155, 'mean_word_length': 1.8338141950375073, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 4, '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': '565247', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1378, 'n_words': 360}