Skip to content

Question utils

question.utils:#

copy_shuffle(questions) #

Makes a shuffled copy of a sequence of questions.

Args: questions (list[Question]): A list of questions

Returns: A shuffled copy of questions

Source code in question/utils.py
def copy_shuffle(questions):
    """ Makes a shuffled copy of a sequence of questions.

    Args:
    questions (list[Question]): A list of questions

    Returns:
    A shuffled copy of questions
    """
    qcopy = deepcopy(questions)
    random.shuffle(qcopy)
    return qcopy

question_by_key(key, questions, is_skippable=None, drop_choices=[]) #

Return a copy of question with given key

Parameters:

Name Type Description Default
key str

Key of question

required
questions list[Question]

List of questions

required
is_skippable bool

True will make the returned questions skippable

None
drop_choices list[str]

Choices in the question to be removed (if applicable)

[]

Returns:

Type Description

A copy of question

Source code in question/utils.py
def question_by_key(key, questions, is_skippable=None, drop_choices=[]):
    """Return a copy of question with given key

    Args:
        key (str): Key of question
        questions (list[Question]): List of questions
        is_skippable (bool): True will make the returned questions skippable
        drop_choices (list[str]): Choices in the question to be removed (if applicable)

    Returns:
        A copy of question
    """
    for question in questions:
        if question.key == key:
            q = deepcopy(question)
            # Question is_skippable
            if is_skippable is not None:
                q.is_skippable = is_skippable
            if hasattr(question, 'choices') and len(drop_choices):
                for choice in drop_choices:
                    q.choices.pop(choice, None)
            return q
    return None

total_unanswered_questions(participant, questions) #

Return how many questions have not been answered yet by the participant

Parameters:

Name Type Description Default
participant Participant

Participant who answer questions

required
questions list[Question]

List of questions

required

Returns:

Type Description

Number of unanswered questions

Source code in question/utils.py
def total_unanswered_questions(participant, questions):
    """ Return how many questions have not been answered yet by the participant

    Args:
        participant (Participant): Participant who answer questions
        questions (list[Question]): List of questions

    Returns:
        Number of unanswered questions
    """
    profile_questions = participant.profile().values_list('question_key', flat=True)
    return len([question for question in questions if question.key not in profile_questions])

unanswered_questions(participant, questions, randomize=False, cutoff_index=None) #

Generator to give next unasked profile question and prepare its result

Parameters:

Name Type Description Default
participant Participant

participant who will be checked for unanswered questions

required
questions list[Questions]

list of questions from which to select an unanswered question

required
randomize bool

optionally, randomize order of questions

False
cutoff_index int

Maximal index in a questions sequence to consider questions

None

Yields:

Type Description

Next unasked profile question

Source code in question/utils.py
def unanswered_questions(participant, questions, randomize=False, cutoff_index=None):
    """Generator to give next unasked profile question and prepare its result

    Args:
        participant (Participant): participant who will be checked for unanswered questions
        questions (list[Questions]): list of questions from which to select an unanswered question
        randomize (bool): optionally, randomize order of questions
        cutoff_index (int): Maximal index in a questions sequence to consider questions

    Yields:
        Next unasked profile question

    """
    if randomize:
        random.shuffle(questions)
    for question in questions[:cutoff_index]:
        profile_result = prepare_profile_result(question.key, participant)
        if profile_result.given_response is None:
            q = deepcopy(question)
            q.result_id = profile_result.pk
            yield q