Skip to content

sweetbean.variable

CodeVariable

Bases: Variable

A variable to access JavaScript code.

Source code in sweetbean/variable/__init__.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class CodeVariable(Variable):
    """
    A variable to access JavaScript code.
    """

    def __init__(self, name, value):
        """
        Arguments:
            name: the name of the variable
            value: the initial value of the variable
        """
        super().__init__(name)
        self.value = value

    def set(self):
        return f"let {self.name}={self.value};"

    def to_js(self):
        return self.name

__init__(name, value)

Parameters:

Name Type Description Default
name

the name of the variable

required
value

the initial value of the variable

required
Source code in sweetbean/variable/__init__.py
83
84
85
86
87
88
89
90
def __init__(self, name, value):
    """
    Arguments:
        name: the name of the variable
        value: the initial value of the variable
    """
    super().__init__(name)
    self.value = value

DataVariable

Bases: Variable

A data variable for jsPsych to access data of previous trials.

Source code in sweetbean/variable/__init__.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class DataVariable(Variable):
    """
    A data variable for jsPsych to access data of previous trials.
    """

    def __init__(self, name, window):
        """
        Arguments:
            name: the name of the variable
            window: the window of the data
        """
        super().__init__(name)
        self.name = f'data["bean_{name}"]'
        self.raw_name = name
        self.window = window

    def to_js(self):
        if self.window == 0:
            return self.name
        return f"jsPsych.data.get().last({self.window})['trials'][0]['bean_{self.raw_name}']"

__init__(name, window)

Parameters:

Name Type Description Default
name

the name of the variable

required
window

the window of the data

required
Source code in sweetbean/variable/__init__.py
33
34
35
36
37
38
39
40
41
42
def __init__(self, name, window):
    """
    Arguments:
        name: the name of the variable
        window: the window of the data
    """
    super().__init__(name)
    self.name = f'data["bean_{name}"]'
    self.raw_name = name
    self.window = window

FunctionVariable

Bases: Variable

A variable that is the result of a function. Examples: >>> def add(a, b): ... return a + b >>> FunctionVariable('a', add, [1, 2]).to_js() ((a,b) => {return a+b})(1, 2)

Source code in sweetbean/variable/__init__.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class FunctionVariable(Variable):
    """
    A variable that is the result of a function.
    Examples:
        >>> def add(a, b):
        ...     return a + b
        >>> FunctionVariable('a', add, [1, 2]).to_js()
        ((a,b) => {return a+b})(1, 2)

    """

    def __init__(self, name, fct, args):
        """
        Arguments:
            name: the name of the variable
            fct: the function
            args: the arguments of the function
        """
        super().__init__(name)
        self.fct = fct
        self.args = args

    def to_js(self):
        fct_declaration = _fct_to_js(self.fct)
        fct_input = _fct_args_to_js(self.args)
        return f"({fct_declaration}){fct_input}"

__init__(name, fct, args)

Parameters:

Name Type Description Default
name

the name of the variable

required
fct

the function

required
args

the arguments of the function

required
Source code in sweetbean/variable/__init__.py
61
62
63
64
65
66
67
68
69
70
def __init__(self, name, fct, args):
    """
    Arguments:
        name: the name of the variable
        fct: the function
        args: the arguments of the function
    """
    super().__init__(name)
    self.fct = fct
    self.args = args

SharedVariable

A variable that can be shared between different stimuli.

Source code in sweetbean/variable/__init__.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class SharedVariable:
    """
    A variable that can be shared between different stimuli.
    """

    def __init__(self, name, value):
        """
        Arguments:
            name: the name of the variable
            value: the initial value of the variable
        """
        self.name = str(name)
        self.value = value

    def set(self):
        return f"let {self.name}={self.value};"

    def to_js(self):
        return self.name

__init__(name, value)

Parameters:

Name Type Description Default
name

the name of the variable

required
value

the initial value of the variable

required
Source code in sweetbean/variable/__init__.py
104
105
106
107
108
109
110
111
def __init__(self, name, value):
    """
    Arguments:
        name: the name of the variable
        value: the initial value of the variable
    """
    self.name = str(name)
    self.value = value

SideEffect

Source code in sweetbean/variable/__init__.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class SideEffect:
    def __init__(self, set_variable, get_variable):
        """
        A side effect that can set variables.

        Arguments:
            set_variable: the variable to set (often a SharedVariable)
            get_variable: the variable to get (e.g, the variable the set variable should be set to)
        """
        self.set_variable = set_variable
        self.get_variable = get_variable

    def to_js(self):
        return f"{self.set_variable.name}={_var_to_js(self.get_variable)};"

__init__(set_variable, get_variable)

A side effect that can set variables.

Parameters:

Name Type Description Default
set_variable

the variable to set (often a SharedVariable)

required
get_variable

the variable to get (e.g, the variable the set variable should be set to)

required
Source code in sweetbean/variable/__init__.py
121
122
123
124
125
126
127
128
129
130
def __init__(self, set_variable, get_variable):
    """
    A side effect that can set variables.

    Arguments:
        set_variable: the variable to set (often a SharedVariable)
        get_variable: the variable to get (e.g, the variable the set variable should be set to)
    """
    self.set_variable = set_variable
    self.get_variable = get_variable

TimelineVariable

Bases: Variable

A timeline variable for jsPsych.

Source code in sweetbean/variable/__init__.py
19
20
21
22
23
24
25
class TimelineVariable(Variable):
    """
    A timeline variable for jsPsych.
    """

    def to_js(self):
        return f"jsPsych.timelineVariable('{self.name}')"

Variable

Bases: ABC

A base class for variables.

Source code in sweetbean/variable/__init__.py
 6
 7
 8
 9
10
11
12
13
14
15
16
class Variable(ABC):
    """
    A base class for variables.
    """

    def __init__(self, name):
        self.name = name

    @abstractmethod
    def to_js(self):
        pass