Access experimental variables
instance var
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)
function var.clear(preserve=[])
New in 3.1.2
Clears all experimentals variables.
Example:
var.clear()
Keywords:
preserve
-- A list of variable names that shouldn't be cleared.- Type: list
- Default: []
function var.get(var, default=None, _eval=True, valid=None)
Gets an experimental variable.
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')
Arguments:
var
-- The variable to retrieve.- Type: str, unicode
Keywords:
default
-- A default value in case the variable doesn't exist, orNone
for no default value.- Type: any
- Default: None
_eval
-- Determines whether the returned should be evaluated for variable references.- Type: bool
- Default: True
valid
-- A list of valid values, orNone
to allow all values.- Type: NoneType, list
- Default: None
function var.has(var)
Checks if an experimental variable exists.
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!')
Arguments:
var
-- The variable to check.- Type: str, unicode
function var.items()
Returns a list of (variable_name, value) tuples. See var.vars()
for a note about the non-exhaustiveness of this function.
Example:
for varname, value in var.items():
print(varname, value)
Returns:
A list of (variable_name, value) tuples.
- Type: list
function var.set(var, val)
Sets and experimental variable.
Example:
var.set(u'my_variable', u'my_value')
# Equivalent to
var.my_variable = u'my_value'
Arguments:
var
-- The variable to assign.- Type: str, unicode
val
-- The value to assign.- Type: any
function var.unset(var)
Deletes a variable.
Example:
var.unset(u'my_variable')
# Equivalent to:
del var.my_variable
Arguments:
var
-- The variable to delete.- Type: str, unicode
function var.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.
Example:
for varname in var.vars():
print(varname)
Returns:
A list of variable names.
- Type: list