1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
"""The expyriment clock.
This module contains an experimental clock.
"""
__author__ = 'Florian Krause <florian@expyriment.org>, \
Oliver Lindemann <oliver@expyriment.org>'
__version__ = '0.7.0'
__revision__ = '55a4e7e'
__date__ = 'Wed Mar 26 14:33:37 2014 +0100'
import sys
import time
import types
from _timer import get_time
import expyriment
class Clock(object) :
"""Basic timing class.
Unit of time is milliseconds.
"""
if sys.platform == 'win32':
_cpu_time = time.clock
else:
_cpu_time = time.time
def __init__(self, sync_clock=None):
"""Create a clock.
Parameters
----------
sync_clock : misc.Clock, optional
synchronise clock with existing one
"""
if (sync_clock.__class__.__name__ == "Clock"):
self.__init_time = sync_clock.init_time / 1000
else:
self.__init_time = get_time()
self._init_localtime = time.localtime()
self.__start = get_time()
@staticmethod
def monotonic_time():
"""Returns the time of the high-resolution monitonoic timer that is
used by Expyriment interally.
"""
return get_time()
@property
def init_time(self):
"""Getter for init time in milliseconds."""
return self.__init_time * 1000
@property
def time(self):
"""Getter for current time in milliseconds since clock init."""
return int((get_time() - self.__init_time) * 1000)
@property
def cpu_time(self):
"""Getter for CPU time."""
return self._cpu_time()
@property
def stopwatch_time(self):
"""Getter for time in milliseconds since last reset_stopwatch.
The use of the stopwatch does not affect the clock time.
"""
return int((get_time() - self.__start) * 1000)
@property
def init_localtime(self):
"""Getter for init time in local time"""
return self._init_localtime
def reset_stopwatch(self):
""""Reset the stopwatch.
The use of the stopwatch does not affect the clock time.
"""
self.__start = get_time()
def wait(self, waiting_time, function=None):
"""Wait for a certain amout of milliseconds.
Parameters
----------
waiting_time : int
time to wait in milliseconds
function : function, optional
function to repeatedly execute during waiting loop
"""
start = self.time
if type(function) == types.FunctionType or\
expyriment._active_exp._execute_wait_callback():
while (self.time < start + waiting_time):
if type(function) == types.FunctionType:
function()
expyriment._active_exp._execute_wait_callback()
else:
looptime = 200
if (waiting_time > looptime):
time.sleep((waiting_time - looptime) / 1000)
while (self.time < start + waiting_time):
pass
def wait_seconds(self, time_sec, function=None):
"""Wait for a certain amout of seconds (see also wait() ).
Parameters
----------
time_sec : int
time to wait in seconds
function : function, optional
function to repeatedly execute during waiting loop
"""
self.wait(time_sec * 1000, function)
def wait_minutes(self, time_minutes, function=None):
"""Wait for a certain amount of minutes (see also wait() ).
Parameters
----------
time_minutes : int
time to wait in minutes
function : function, optional
function to repeatedly execute during waiting loop
"""
self.wait_seconds(time_minutes * 60, function)
|