Skip to content

Experiment utils

experiment.utils

consent_upload_path(instance, filename)

Generate path to save consent file based on experiment.slug and language

Parameters:

Name Type Description Default
instance Experiment

Experiment instance to determine folder name

required
filename str

Name of the consent file to be uploaded

required

Returns:

Name Type Description
upload_to str

Path for uploading the consent file

Note

Used by the Block model for uploading consent file

Source code in experiment/utils.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def consent_upload_path(instance: Experiment, filename: str) -> str:
    """Generate path to save consent file based on experiment.slug and language

    Args:
        instance: Experiment instance to determine folder name
        filename: Name of the consent file to be uploaded

    Returns:
        upload_to: Path for uploading the consent file

    Note:
        Used by the Block model for uploading consent file
    """
    experiment = instance.experiment
    folder_name = experiment.slug
    language = instance.language

    join("consent", folder_name, f"{language}-{filename}")

create_player_labels(num_labels, label_style='number')

Create player labels

Parameters:

Name Type Description Default
num_labels int

Number of labels

required
label_style str

‘number’, ‘alphabetic’, ‘roman’

'number'

Returns:

Type Description
list[str]

Player label

Source code in experiment/utils.py
77
78
79
80
81
82
83
84
85
86
87
88
def create_player_labels(num_labels: int, label_style: str = "number") -> list[str]:
    """Create player labels

    Args:
        num_labels: Number of labels
        label_style: 'number', 'alphabetic', 'roman'

    Returns:
        Player label
    """

    return [format_label(i, label_style) for i in range(num_labels)]

external_url(text, url)

Create a HTML element for an external url

Parameters:

Name Type Description Default
text str

Text

required
url

Url

required

Returns:

Type Description
str

HTML element

Source code in experiment/utils.py
63
64
65
66
67
68
69
70
71
72
73
74
def external_url(text: str, url) -> str:
    """Create a HTML element for an external url

    Args:
        text: Text
        url: Url

    Returns:
        HTML element
    """

    return '<a href="{}" target="_blank" rel="noopener noreferrer" >{}</a>'.format(url, text)

format_label(number, label_style)

Generate player_label for create_player_label()

Parameters:

Name Type Description Default
number int

index

required
label_style str

‘number’, ‘alphabetic’, ‘roman’

required

Returns:

Type Description
str

Player label

Source code in experiment/utils.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def format_label(number: int, label_style: str) -> str:
    """Generate player_label for create_player_label()

    Args:
        number: index
        label_style: 'number', 'alphabetic', 'roman'

    Returns:
        Player label
    """

    if label_style == "alphabetic":
        return chr(number + 65)
    elif label_style == "roman":
        return roman.toRoman(number + 1)
    else:
        return str(number + 1)

non_breaking_spaces(input_string)

Convert regular spaces to non breaking spacing on the given string Args: input_string: Input string

Returns:

Type Description
str

String with non breaking spaces

Source code in experiment/utils.py
50
51
52
53
54
55
56
57
58
59
60
def non_breaking_spaces(input_string: str) -> str:
    """Convert regular spaces to non breaking spacing on the given string
    Args:
        input_string: Input string

    Returns:
        String with non breaking spaces
    """

    non_breaking_space = chr(160)
    return input_string.replace(" ", non_breaking_space)

slugify(text)

Create a slug from given string

Parameters:

Name Type Description Default
text str

Input text (str)

required

Returns:

Type Description
str

slug

Source code in experiment/utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def slugify(text: str) -> str:
    """Create a slug from given string

    Args:
        text: Input text (str)

    Returns:
        slug
    """

    non_url_safe = [
        '"',
        "#",
        "$",
        "%",
        "&",
        "+",
        ",",
        "/",
        ":",
        ";",
        "=",
        "?",
        "@",
        "[",
        "\\",
        "]",
        "^",
        "`",
        "{",
        "|",
        "}",
        "~",
        "'",
    ]
    translate_table = {ord(char): "" for char in non_url_safe}
    text = text.translate(translate_table)
    text = "_".join(text.split())
    return text.lower()