Measuring the delay of background changing and sound playing using Timeit (python) - python

I need to figure out the delay between sending a command to change the background color or playing a sound and these events actually occurring using timeit.(I'm on windows, Python 2.73)
I'm doing a reaction time test where I'll record the time(using time.clock()) before either changing the background color or playing a sound. Then, when the subject presses a key I record the time again and take difference to find the reaction time.
For the sound playing, here's what I did:
import timeit
t = timeit.Timer(stmt = "winsound.PlaySound('C:\WINDOWS\media\Windows XP Error.wav', winsound.SND_FILENAME)",setup = "import winsound")
n = t.timeit(number = 100)
print n/100 -0.999
The 0.999 is the duration of the Windows XP Error.wav in seconds.
This gave me something like 56ms. I'm not sure if its reasonable and if its the right way to do it as well as should I be enabling the garbage collection or not?
For the background changing I'm having more problems. Since I'm doing the test in fullscreen mode I tried to put all of these into the setup parameter:
from Tkinter import Tk
root=Tk()
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),root.winfo_screenheight()))
root.mainloop()
Even though I separate them all with ; I still get syntax errors. When I try it not in full screen
setup = 'from Tkinter import Tk; root=Tk(); root.mainloop()' the window actually opens, yet nothing happens and if I close it I see other errors.Invalid command name "."
The statement that I'm actually measuring is root.configure(background='red').

Here's an example of a way to create a multi-line setup string for use with timeit:
setup = """
import random
l1 = [random.randrange(100) for _ in xrange(100)]
l2 = [random.randrange(100) for _ in xrange(10)]
"""
Here's another tip. To get an accurate measurement, it's important to time things following this basic pattern:
time = min(timeit.repeat(statements, setup=setup, repeat=R, number=N))
With an R of at least 3 (R = 3). This takes the fastest value obtained by doing everything 3 times, which will eliminate differences due to the many other things running on your system in the background.
This doesn't answer your whole question, but may be helpful in your quest.

Related

Python Prevent the screen saver

I need to run a code for hours, and the computer I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?
This worked for me. I'll just leave it here so people can use it.
import ctypes
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep.
## your code and operations
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
You can prevent screen saver by moving mouse cursor on a fixed period of time, below function can handle cursor moving.
import win32api
import random
def wakeup():
win32api.SetCursorPos((random.choice(range(100)),random.choice(range(100))))
This is a coded solution that you can place in your program (also works for Mac users):
pip3 install pyautogui
https://pypi.org/project/PyAutoGUI/ (Reference)
import pyautogui
import time
def mouse_move():
while True:
pyautogui.moveTo(100, 100, duration = 1) # move the mouse
time.sleep(60) # Every 1 min
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
Or, without the while loop, run it when required if your program is already within a while loop:
def mouse_move():
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()

how do I print a title for the top of my running program to continuously be visible for a code that prints and runs an endless amount of data?

Example: (to stay visible on the running program, able to view it at anytime if needed to scroll to the top)
print("this is my title")
print("here is my sub title")
count = 0
while count < 5000:
print("hello")
count = count + 1 # or count += 1
My code runs for as long as I set it too, that's not the problem. But when the program runs, it never shows the top printed title, or if I were to stop the program for a moment and physically scroll to the top, that printed title and other various text isn't visible.
How do I fix this to where, even if I wanted to print a million items, I could still see the printed title and printed information at the top?
First a useful non python way:
If you run a script (say my_long_print_script.py) from the terminal you can use less (in linux and osx for sure):
python my_long_print_script.py | less
then use enter to scroll down and q to quit.
Writing to stdout in python
In python you can directly write to stdout and 'overwrite' your previous output. This can lead to some sort of progress bar behaviour, I'm not sure this is what you want, here is an example:
# content of my_long_print_script.py:
import sys
from time import sleep
sys.stdout.write('This title should stay\n')
sys.stdout.write('this subtitle too\n')
for count in xrange(100):
sleep(0.1)
sys.stdout.write('{0}\r'.format(count))
sys.stdout.flush()
When you run this as a script (so you type python my_very_long_print_script.py in the terminal) the two title lines will persist and below a single line will continuously be updated.
FYI: I added the sleep because the count changes too quickly otherwise.
Hope this was somehow useful.
You'll probably want to use python's curses library. This is how you create "windows" that can be redrawn in-place.
I wrote a CLI version of 2048 that would scroll the terminal every time I output the game's board after a move. Using curses, I can now just overwrite the previous board without any scrolling.
Basically you'll want to initialize a new curses window object, set your output string to "My Title Here" and issue a window.redraw() command at (0,0) every time your program iterates.

Timer in a traffic light simulation [duplicate]

This question already has answers here:
Measuring elapsed time with the Time module
(10 answers)
Closed 6 years ago.
I'm currently working on a Python 3.x project and I'm supposed to make a simple crossroad simulation with traffic lights etc. My only problem right now is that I am not aware of any way to measure time so as to use it to change the traffic lights accordingly. Is there any way to create a virtual timer which will begin as soon as the simulation starts and goes on infinitely until the window is closed? I'm considering using the time or timeit modules, but I'm a bit lost here.
Thanks in advance.
Since you included the tag tkinter, there is a tkinter-specific solution that works quite well. You can use the after method available on every widget to schedule an event to happen in the future.
In the simplest case, you can have a function that will run some other function on a given interval. For example:
import tkinter as tk
def run_periodically(func, ms):
func()
root.after(ms, run_periodically, func, ms)
def tick():
global count
count += 1
tick_label.configure(text="count: %d" % count)
count = 0
root = tk.Tk()
tick_label = tk.Label(root, text="")
tick_label.pack(padx=20, pady=20)
run_periodically(tick, 1000)
root.mainloop()
Of course, instead of having something called on a schedule, you can also just schedule the jobs as needed. For example, when the light turns red you can use after to both turn it off and turn the green on. You can then use after to turn the green off and the yellow on, and so on. Since this sounds like a homework assignment I won't provide an example.

Tkinter Scrolling Canvas Widget speed issues

I am currently remaking flappy bird in Tkinter. (I understand this is bad, I explain why at the bottom.) My issue is with the pipes, and the speeds they scroll at and the distance they are from each other. Unless something is wrong with my logic, if a start the two pipes separated from each other then move them when they get to a certain point, and place them at the same point, they should retain the gap between them. This may be better explained in code.
from tkinter import *
import random
root = Tk()
root.geometry('430x640')
root.configure(background='turquoise')
canvas = Canvas(root,width=int(435),height=int(645))
canvas.configure(background='turquoise')
canvas.pack()
x, x2 = 400, 700
y = random.randint(0,300)
y2 = random.randint(0,300)
def drawPipe():
global x,x2,y,y2
canvas.coords(pipeTop,(x,0,(x+50),y))
canvas.coords(pipeBottom,(x,640,(x+50),(y+150)))
canvas.coords(pipeTop2,(x2,0,(x2+50),y2))
canvas.coords(pipeBottom2,(x2,640,(x2+50),(y2+150)))
x -= 3
x2 -= 3
if x < -46:
x = 435
y = random.randint(5,540)
if x2 <-46:
x2 = 435
y2 = random.randint(5,540)
root.after(1,drawPipe)
pipeTop = canvas.create_rectangle(x,0,(x+50),y,fill='green')
pipeBottom = canvas.create_rectangle(x,640,x+50,y+150,fill='green')
pipeTop2 = canvas.create_rectangle(x2,0,(x2+50),y,fill='green')
pipeBottom2 = canvas.create_rectangle(x2,640,(x2+50),(y2+150),fill='green')
drawPipe()
root.mainloop()
This is not my full code, but it is the bit concerned with drawing and updating the pipes. When run, this code will show you how the pipes scrolling speed up and down. I do not understand how this is possible. All the values for the pipes are the same apart from the starting positions. Is this due to the inefficient way Tkinter uses the after method? I attempted to use threading but this produced problems when using root.bind (see my previous question). Or is it due to a logic error? Thank you in advance to anyone who can help me.
Side note: I realise I should not be making a game in tkinter, especially one that requires multiple things to be happening at once. However, I am doing this at school and the modules I would like to use (Pygame or Pyglet) cannot be downloaded just for me to make a game that has no real purpose. If I could use something other than tkinter I probably would. Thank you for your help.
Using after(1,..) you get 1000FPS (Frames Per Second) but you don't need it - use after(20, ...) to get 50 FPS.
Beside using after(1,..) your program have no time to do other things - it have no time to execute all after() so you can get different speed.
With after(1,..) I couldn't even move window.
And my CPU became hotter so fan started working faster and louder.

Manual pyglet loop freezes after a few iterations

I am testing out pyglet for usage in a larger project, and apparently pyglet recommends/wants you to use it's own loop (with pyglet.app.run())
This is a something I don't want, for reasons of compatibility of other packages and also to not have to rewrite the entire program structure.
Here I have prototype code stuck together from different parts and tutorials and docs.
It runs for 5-15 iterations and then just freezes, not printing anything and also not doing any draw updates.
from __future__ import division, print_function
import sys
import pyglet
window = pyglet.window.Window(800, 800, resizable=True)
window.set_caption('Pyglet Testing')
window.flip()
image = pyglet.resource.image('Sprites/scout.png')
def draw(dt):
image.blit(700-dt, 400)
while not window.has_exit:
dt = pyglet.clock.tick()
window.dispatch_events()
window.clear()
draw(dt)
window.flip()
print(dt)
My suspicion is that I have done nothing to catch events and handle them, so at a certain point it just overflows with events and blocks the whole thing. I couldn't understand how to do this however, and getting overflowed with events in under 1 second seems a bit much.
Any help?
Basically what you are doing is sending as many image.blit(...) commands to the window, until the pc probably can't handle it anymore.
For instance, if you change your code like this:
add this code:
import time
from time import sleep
change code:
def draw(dt):
image.blit(700-dt, 400)
sleep(0.1) #insert this line
When executing the modified code, you will notice that it does not freeze and that the output dt is around 0.11 seconds, which is the number of seconds since the last "tick" = the time slept (0.1 second) + the remainder time (clear window, display new frame ...)

Categories