Skip to content

Instantly share code, notes, and snippets.

@smathot
Created November 12, 2011 18:02
Show Gist options
  • Save smathot/1360889 to your computer and use it in GitHub Desktop.
Save smathot/1360889 to your computer and use it in GitHub Desktop.
OpenSesame code snippets
# ***
# * Continuously poll the keyboard
# ***
from openexp.keyboard import keyboard
my_keyboard = keyboard(self.experiment, timeout=20)
start_time = self.time()
while True:
# Perform some task here
key, end_time = my_keyboard.get_key()
if key != None:
break
self.experiment.set("response", my_keyboard.to_chr(key))
self.experiment.set("response_time", end_time-start_time)
# ***
# * Copy the canvas from a sketchpad item
# ***
from openexp.canvas import canvas
my_canvas = canvas(self.experiment)
my_canvas.copy(self.experiment.items["name_of_sketchpad"].canvas)
# ***
# * Getting and setting variables
# ***
# Setting variables
self.experiment.set("my_variable", "my_value") # Preferred
self.experiment.my_variable = "my_value" # Not preferred
# Getting variables
print self.get("my_variable") # Preferred
print self.experiment.my_variable # Not preferred
# Checking whether a variable exists
if self.has("my_variable"):
print "My variable exists"
# ***
# * Overlay multi-character text input over a sketchpad
# ***
from openexp.canvas import canvas
from openexp.keyboard import keyboard
my_canvas = canvas(self.experiment)
my_keyboard = keyboard(self.experiment)
resp = "" # Here we store the response
while True:
# Get a keyboard response
key, time = my_keyboard.get_key()
# keyboard.to_chr() converts the keycode to a description,
# like 'space' or 'return'. If return is pressed the loop
# should be exited.
if my_keyboard.to_chr(key) == "return":
break
# Handle backspace by removing the last character
if my_keyboard.to_chr(key) == "backspace":
resp = resp[:-1]
else:
# The built-in chr() converts the keycode to a character,
# like ' ' and '/'.
resp += chr(key)
# Copy the canvas from a sketchpad called 'my_sketchpad' and
# overlay the response
my_canvas.copy(self.experiment.items["my_sketchpad"].canvas)
my_canvas.text(resp)
my_canvas.show()
# Save the response
self.experiment.set("response", resp)
# ***
# * Print a debugging message
# ***
print "This is a debug message"
# ***
# * Write a custom message to the log-file
# ***
self.log("This is a custom log message")
# ***
# * Reverse the text in all sketchpad, feedback, text_display, and text_input
# * items. Supports Unicode characters.
# ***
from PyQt4.QtCore import QString
for item in self.experiment.items.values():
if item.item_type in ['sketchpad', 'feedback']:
for element in item.items:
if 'text' in element:
s = element['text']
s = self.usanitize(QString(self.unsanitize(s)[::-1]))
element['text'] = s
elif item.item_type in ['text_input']:
s = item._question
s = self.usanitize(QString(self.unsanitize(s)[::-1]))
item._question = s
elif item.item_type in ['text_display']:
s = item.content
s = self.usanitize(QString(self.unsanitize(s)[::-1]))
item.content = s
# ***
# * Keep track of averages, for example to be used in a feedback item
# ***
my_response_time = 1000 # Indicates response time
is_correct = True # Indicates whether the response is correct
self.experiment.total_responses += 1
if is_correct:
self.experiment.total_correct += 1
self.experiment.total_response_time += my_response_time
self.experiment.acc = 100. * self.experiment.total_correct / self.experiment.total_responses
self.experiment.avg_rt = self.experiment.total_response_time / self.experiment.total_responses
self.experiment.accuracy = self.experiment.acc
self.experiment.average_response_time = self.experiment.avg_rt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment