Inline_script functions
When you are using the inline_script item, you are essentially writing the body of two functions (prepare
and run
) of an inline_script
object. The inline_script
object has many more functions which you can use, and these are listed below. To use these functions, you use the self.[function_name]
notation. For example:
subject_nr = self.get("subject_nr")
or
self.sleep(1000)
For OpenSesame 2.8.3
class inline_scriptFunction overview:
__init__
auto_type
color_check
copy_sketchpad
eval_text
flush_log
get
get_check
get_refs
has
log
offline_canvas
prepare
resolution
run
sanitize
set
set_response
sleep
time
unistr
unset
inline_script.__init__(name, experiment, string=None)
Constructor. You will generally not create an inline_script item yourself, but use OpenSesame to create a body for the prepare() and run() functions.
Arguments:
name → The name of the item.
experiment → The experiment.
Keyword arguments:
string → An item definition string (default=None).
Arguments:
name → The name of the item.
experiment → The experiment.
Keyword arguments:
string → An item definition string (default=None).
inline_script.auto_type(val)
Inherited from item
Converts a value into the 'best fitting' or 'simplest' type that is compatible with the value.
Arguments:
val → A value.
Returns:
The same value converted to the 'best fitting' type.
Example:
Converts a value into the 'best fitting' or 'simplest' type that is compatible with the value.
Arguments:
val → A value.
Returns:
The same value converted to the 'best fitting' type.
Example:
print(type(self.auto_type('1'))) # Prints 'int'
print(type(self.auto_type('1.1'))) # Prints 'float'
print(type(self.auto_type('some text'))) # Prints 'unicode'
# Note: Boolean values are converted to 'yes' / 'no' and are
# therefore also returned as unicode objects.
print(type(self.auto_type(True))) # Prints 'unicode'
inline_script.color_check(col)
Inherited from item
Checks whether a string is a valid color name.
Arguments:
col → The color to check.
Exceptions:
Raises a osexception if col is not a valid color.
Example:
Checks whether a string is a valid color name.
Arguments:
col → The color to check.
Exceptions:
Raises a osexception if col is not a valid color.
Example:
# Ok
print(self.color_check('red'))
# Ok
print(self.color_check('#FFFFFF'))
# Raises osexception
print(self.color_check('this is not a color'))
inline_script.copy_sketchpad(sketchpad_name)
Creates a canvas that is a copy from the canvas of a sketchpad item.
Arguments:
sketchpad_name → The name of the sketchpad.
Returns:
An openexp canvas.
Example:
Arguments:
sketchpad_name → The name of the sketchpad.
Returns:
An openexp canvas.
Example:
my_canvas = self.copy_sketchpad('my_sketchpad')
inline_script.eval_text(text, round_float=False, soft_ignore=False, quote_str=False)
Inherited from item
Evaluates a string of text, so that all variable references (e.g., '[var]') are replaced by values.
Arguments:
text → The text to be evaluated.
Keyword arguments:
round_float → A Boolean indicating whether float values should be rounded to a precision of [round_decimals]. round_decimals is an OpenSesame variable that has a default value of 2. (Default=False)
soft_ignore → A Boolean indicating whether missing variables should be ignored, rather than cause an exception (default=False).
quote_str → A Boolean indicating whether string variables should be surrounded by single quotes (default=False).
Returns:
The evaluated text.
Example:
Evaluates a string of text, so that all variable references (e.g., '[var]') are replaced by values.
Arguments:
text → The text to be evaluated.
Keyword arguments:
round_float → A Boolean indicating whether float values should be rounded to a precision of [round_decimals]. round_decimals is an OpenSesame variable that has a default value of 2. (Default=False)
soft_ignore → A Boolean indicating whether missing variables should be ignored, rather than cause an exception (default=False).
quote_str → A Boolean indicating whether string variables should be surrounded by single quotes (default=False).
Returns:
The evaluated text.
Example:
exp.set('var', 'evaluated')
# Prints 'This string has been evaluated
print(self.eval_text('This string has been [var]'))
inline_script.flush_log()
Inherited from item
Forces any pending write operations to the log file to be written to disk.
Example:
Forces any pending write operations to the log file to be written to disk.
Example:
self.log('TRIAL FINISHED')
self.flush_log()
inline_script.get(var, _eval=True)
Inherited from item
Returns the value of an OpenSesame variable. Checks first if the variable exists 'locally' in the item and, if not, checks if the variable exists 'globally' in the experiment.
The type of the returned value can be int, float, or unicode (string). The appropriate type is automatically selected, e.g. '10' is returned as int, '10.1' as float, and 'some text' as unicode.
The _eval parameter is used to specify whether the value of the variable should be evaluated, in case it contains references to other variables. This is best illustrated by example 2 below.
Arguments:
var → The name of an OpenSesame variable.
_eval → Indicates whether the variable should be evaluated, i.e. whether containing variables should be processed (default=True).
Exceptions:
A osexception is raised if the variable is not found.
Returns:
The value.
Example:
Example 2:
Returns the value of an OpenSesame variable. Checks first if the variable exists 'locally' in the item and, if not, checks if the variable exists 'globally' in the experiment.
The type of the returned value can be int, float, or unicode (string). The appropriate type is automatically selected, e.g. '10' is returned as int, '10.1' as float, and 'some text' as unicode.
The _eval parameter is used to specify whether the value of the variable should be evaluated, in case it contains references to other variables. This is best illustrated by example 2 below.
Arguments:
var → The name of an OpenSesame variable.
_eval → Indicates whether the variable should be evaluated, i.e. whether containing variables should be processed (default=True).
Exceptions:
A osexception is raised if the variable is not found.
Returns:
The value.
Example:
if self.get('cue') == 'valid':
print('This is a validly cued trial')
Example 2:
exp.set('var1', 'I like [var2]')
exp.set('var2', 'OpenSesame')
print(self.get('var1')) # prints 'I like OpenSesame'
print(self.get('var1', _eval=False)) # prints 'I like [var2]'
inline_script.get_check(var, default=None, valid=None, _eval=True)
Inherited from item
Similar to get(), but falls back to a default if the variable has not been set. It also raises an error if the value is not part of the valid list.
Arguments:
var → The name of an OpenSesame variable
default → A default 'fallback' value or None for no fallback, in which case an exception is rased if the value does not exist.
valid → A list of allowed values (or None for no restrictions). An exception is raised if the value is not an allowed value.
_eval → Indicates whether the variable should be evaluated, i.e. whether containing variables should be processed (default=True).
Exceptions:
Raises a osexception if the variable is not defined and there is no default value, or if the variable value is not part of the 'valid' list.
Returns:
The value
Example:
Similar to get(), but falls back to a default if the variable has not been set. It also raises an error if the value is not part of the valid list.
Arguments:
var → The name of an OpenSesame variable
default → A default 'fallback' value or None for no fallback, in which case an exception is rased if the value does not exist.
valid → A list of allowed values (or None for no restrictions). An exception is raised if the value is not an allowed value.
_eval → Indicates whether the variable should be evaluated, i.e. whether containing variables should be processed (default=True).
Exceptions:
Raises a osexception if the variable is not defined and there is no default value, or if the variable value is not part of the 'valid' list.
Returns:
The value
Example:
if self.get_check('cue', default='invalid') == 'valid':
print('This is a validly-cued trial')
inline_script.get_refs(text)
Inherited from item
Returns a list of variables that are referred to by a string of text.
Arguments:
text → A string of text.
Returns:
A list of variable names or an empty list if the string contains no references.
Example:
Returns a list of variables that are referred to by a string of text.
Arguments:
text → A string of text.
Returns:
A list of variable names or an empty list if the string contains no references.
Example:
print(self.get_refs('There are [two] [references] here'))
# Prints ['two', 'references']
inline_script.has(var)
Inherited from item
Checks if an OpenSesame variable exists, either in the item or in the experiment.
Arguments:
var → The name of an OpenSesame variable.
Returns:
True if the variable exists, False if not.
Example:
Checks if an OpenSesame variable exists, either in the item or in the experiment.
Arguments:
var → The name of an OpenSesame variable.
Returns:
True if the variable exists, False if not.
Example:
if not self.has('response'):
print('No response has been collected yet')
inline_script.log(msg)
Inherited from item
Writes a message to the log file. Note that using the log() function in combination with a logger item may result in messy log files.
msg → A message. This can be any type and will we be converted to a unicode string using the logic described in `unistr()`.
Example:
Writes a message to the log file. Note that using the log() function in combination with a logger item may result in messy log files.
msg → A message. This can be any type and will we be converted to a unicode string using the logic described in `unistr()`.
Example:
self.log('TIMESTAMP = %s' % self.time())
inline_script.offline_canvas(auto_prepare=True)
Creates an empty canvas.
Keyword arguments:
auto_prepare → See canvas documentation. (default=True)
Returns:
An openexp canvas.
Example:
Keyword arguments:
auto_prepare → See canvas documentation. (default=True)
Returns:
An openexp canvas.
Example:
my_canvas = self.offline_canvas()
inline_script.prepare()
Executes the prepare script. The code that you enter in the 'prepare' tab of an inline_script item in the GUI is used as a body for this function.
inline_script.resolution()
Inherited from item
Returns the display resolution and check whether the resolution is valid.
Note: The meaning of 'resolution' depends on the back-end. For example, the legacy and OpenGL back-ends change the actual resolution of the display, whereas the other back-ends do not alter the actual display resolution, but create a 'virtual display' with the requested resolution that is presented in the center of the display.
Returns:
A (width, height) tuple
Returns the display resolution and check whether the resolution is valid.
Note: The meaning of 'resolution' depends on the back-end. For example, the legacy and OpenGL back-ends change the actual resolution of the display, whereas the other back-ends do not alter the actual display resolution, but create a 'virtual display' with the requested resolution that is presented in the center of the display.
Returns:
A (width, height) tuple
inline_script.run()
Executes the run script. The code that you enter in the 'run' tab of an inline_script item in the GUI is used as a body for this function.
inline_script.sanitize(s, strict=False, allow_vars=True)
Inherited from item
Removes invalid characters (notably quotes) from the string.
Arguments:
s → The string (unicode or str) to be sanitized.
Keyword arguments:
strict → If True, all except underscores and alphanumeric characters are
stripped (default=False).
allow_vars → If True, square brackets are not sanitized, so you can use
variables (default=True).
Returns:
A sanitized unicode string
Example:
Removes invalid characters (notably quotes) from the string.
Arguments:
s → The string (unicode or str) to be sanitized.
Keyword arguments:
strict → If True, all except underscores and alphanumeric characters are
stripped (default=False).
allow_vars → If True, square brackets are not sanitized, so you can use
variables (default=True).
Returns:
A sanitized unicode string
Example:
# Prints 'Universit Aix-Marseille'
print(self.sanitize('\"Université Aix-Marseille\"'))
# Prints 'UniversitAixMarseille'
print(self.sanitize('\"Université Aix-Marseille\""', strict=True))
inline_script.set(var, val)
Inherited from item
Sets an OpenSesame variable.
If you want to set a variable so that it is available in other items as well (such as the logger item, so you can log the variable), you need to use the set() function from the experiment. So, in an inline_script item you would generally set a variable with exp.set(), rather than self.set().
Please note that you can only set simple variable types (str, unicode, float, and int). If you use the set function to save an object, it will be converted to a string representation. To make complex variables globally accessible in your experiment, please use the global keyword.
The type of the value can be anything. However, see get() for an explanation of how data-types are handled.
Arguments:
var → The name of an OpenSesame variable.
val → The value.
Example:
Sets an OpenSesame variable.
If you want to set a variable so that it is available in other items as well (such as the logger item, so you can log the variable), you need to use the set() function from the experiment. So, in an inline_script item you would generally set a variable with exp.set(), rather than self.set().
Please note that you can only set simple variable types (str, unicode, float, and int). If you use the set function to save an object, it will be converted to a string representation. To make complex variables globally accessible in your experiment, please use the global keyword.
The type of the value can be anything. However, see get() for an explanation of how data-types are handled.
Arguments:
var → The name of an OpenSesame variable.
val → The value.
Example:
exp.set('my_timestamp', self.time())
inline_script.set_response(response=None, response_time=None, correct=None)
Inherited from item
Processes a response in such a way that feedback variables are updated as well.
Keyword arguments:
response → The response value. (default=None)
response_time → The response time. (default=None)
correct → The correctness value. (default=None)
Example:
Processes a response in such a way that feedback variables are updated as well.
Keyword arguments:
response → The response value. (default=None)
response_time → The response time. (default=None)
correct → The correctness value. (default=None)
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp)
t1 = self.time()
button, timestamp = my_keyboard.get_key()
if button == 'left':
correct = 1
else:
correct = 0
rt = timestamp - t1
self.set_response(response=button, response_time=rt, \
correct=correct)
inline_script.sleep(ms)
Inherited from item
Sleeps for a specified duration.
Arguments:
ms → An integer value specifying the duration in milliseconds.
Example:
Sleeps for a specified duration.
Arguments:
ms → An integer value specifying the duration in milliseconds.
Example:
self.sleep(1000) # Sleeps one second
inline_script.time()
Inherited from item
Returns the current time.
Returns:
A timestamp of the current time.
Example:
Returns the current time.
Returns:
A timestamp of the current time.
Example:
print('The time is %s' % self.time())
inline_script.unistr(val)
Inherited from item
Converts a value to a unicode string. This function is mostly necessary to make sure that normal strings with special characters are correctly encoded into unicode, and don't result in TypeErrors.
The conversion logic is as follows:
- unicode values are returned unchanged.
- str values are decoded using utf-8.
- all other types are typecast to unicode, assuming utf-8 encoding where applicable.
Arguments:
val → A value of any type.
Returns:
A unicode string.
Converts a value to a unicode string. This function is mostly necessary to make sure that normal strings with special characters are correctly encoded into unicode, and don't result in TypeErrors.
The conversion logic is as follows:
- unicode values are returned unchanged.
- str values are decoded using utf-8.
- all other types are typecast to unicode, assuming utf-8 encoding where applicable.
Arguments:
val → A value of any type.
Returns:
A unicode string.
inline_script.unset(var)
Inherited from item
Unsets (forgets) an OpenSesame variable.
Arguments:
var → The name of an OpenSesame variable.
Example:
Unsets (forgets) an OpenSesame variable.
Arguments:
var → The name of an OpenSesame variable.
Example:
self.set('var', 'Hello world!')
print(self.get('var')) # Prints 'Hello world!'
self.unset('variable_to_forget')
print(self.get('var')) # Gives error!