Access experimental variables
instance var
New in 4.0.0: As of OpenSesame 4.0, all experimental variables are
also available in the Python workspace. This means that you therefore
don't need the var
object anymore.
The var
object provides access to experimental variables.
Experimental variables are the variables that live in the GUI, and are
commonly set as independent variables in the loop item, referred
to using
the square-bracket ([my_variable]
) notation, and logged by
the logger
item.
A var
object is created automatically when the experiment starts.
In addition to the functions listed below, the following semantics are
supported:
Example:
# Set an experimental variable
var.my_variable = u'my_value'
# Get an experimental variable
print(u'Subject nr = %d' % var.subject_nr)
# Delete (unset) an experimental
variable
del var.my_variable
# Check if an experimental variable exists
if
u'my_variable' in var:
print(u'my_variable exists!')
# Loop through all
experimental variables
for var_name in var:
print(u'variable found:
%s' % var_name)
clear(preserve=[])
New in 3.1.2
Clears all experimentals variables.
Parameters
- preserve: A list of variable names that shouldn't be cleared.
Example
var.clear()
get(var, default=None, _eval=True, valid=None)
Gets an experimental variable.
Parameters
- var: The variable to retrieve.
- default: A default value in case the variable doesn't exist, or
None
for no default value. - _eval: Determines whether the returned should be evaluated for variable references.
- valid: A list of valid values, or
None
to allow all values.
Example
print('my_variable = %s' % var.get(u'my_variable'))
# Equivalent to:
print('my_variable = %s' % var.my_variable)
# But if you want to pass keyword arguments you need to use `get()`:
var.get(u'my_variable', default=u'a_default_value')
has(var)
Checks if an experimental variable exists.
Parameters
- var: The variable to check.
Example
if var.has(u'my_variable'):
print(u'my_variable has been defined!')
# Equivalent to:
if u'my_variable' in var:
print(u'my_variable has been defined!')
inspect()
Generates a description of all experimental variables, both alive and hypothetical.
Returns
- A dict where variable names are keys, and values are dicts with source, value, and alive keys.
items()
Returns a list of (variable_name, value) tuples. See var.vars()
for a note about the non-exhaustiveness of this function.
Returns
- A list of (variable_name, value) tuples.
Example
for varname, value in var.items():
print(varname, value)
set(var, val)
Sets and experimental variable.
Parameters
- var: The variable to assign.
- val: The value to assign.
Example
var.set(u'my_variable', u'my_value')
# Equivalent to
var.my_variable = u'my_value'
unset(var)
Deletes a variable.
Parameters
- var: The variable to delete.
Example
var.unset(u'my_variable')
# Equivalent to:
del var.my_variable
vars()
Returns a list of experimental variables. Because experimental
variables can be stored in multiple places, this list may not be
exhaustive. That is, u'my_var' in var
may return True
, while
u'my_var' is not in the list of variables as returned by this function.
Returns
- A list of variable names.
Example
for varname in var.vars():
print(varname)