Ssaw.assignments module

I’m working on an application to perform assignments. The code below creates the assignment but without any identifying data. How can I use this library to make assignments with pre-filled identifying data.

import ssaw
from ssaw.models import Assignment
from ssaw import AssignmentsApi

client = ssaw.Client(‘server_url’, ‘user’, ‘password’)

data = [
{“Variable”: “Ed_no”, “Answer”: “19030”}
]

newobj = Assignment(
responsible=“Dre”,
questionnaire_id=“a189a582c01a41129058b44e799815af$6”,
quantity=1,
identifying_data=data)

res = AssignmentsApi(client).create(newobj)
print(res.id)

When you run your code, what response does server gives? Also is there a way for you to show content of generated JSON body for request?

I can confirm that this is a bug with the ssaw package. I don’t expect the fix to take too long, I just created an issue (https://github.com/vavalomi/ssaw/issues/2) you can follow.

Update: New version containing the fix was uploaded to pypi, please update your copy.

1 Like

Thank you for the fast reply. Updating the saw package solved my problem.

Can I use this package for assignments with identifying and collected data (archive)? At our office we do quarterly labour force surveys and we would repeat some of the households from the previous quarter hence we pre-fill those assignments with data that was collected before.

When you create the assignment via ‘new assignment’ button, by uploading csv file or calling api (ssaw package) you just specify what values should each field have. System at that moment doesn’t know (and doesn’t need to) whether this values come from a previous round of a panel survey.

So your workflow would be:

  1. via export, or by reading individual assignment get the value of field(s) from the previous round
  2. use those values to create new assignment(s)

This works but not for list and multi select questions. For example I have a list question with variable “name” and when I export the data I get “name__0”, “name__1”, “name_2”, etc. The same happens for multi select questions.

How do you deal with these questions as well as roster questions?

Sample Code;

import csv
import ssaw
from ssaw.models import Assignment
from ssaw import AssignmentsApi

data = [
{“Variable”: “refwk_start”, “Answer”: ‘09/21/20’},
{“Variable”: “parish”, “Answer”: 50},
{“Variable”: “psu”, “Answer”: “15”},
{“Variable”: “Ed_no”, “Answer”: “19030”},
{“Variable”: “household_number”, “Answer”: 999},
{“Variable”: “address”, “Answer”: “Maran”},
{“Variable”: “head_of_houshold”, “Answer”: “John”},
{“Variable”: “name”, “Answer”: “John, Tomm”} # This is a list question
]

newobj = Assignment(
responsible=“Dre”,
questionnaire_id=“a189a582c01a41129058b44e799815af$6”,
quantity=1,
identifying_data=data)

res = AssignmentsApi(client).create(newobj)
print(res.id)

Hi andre, sorry for the late response, completely missed your question.

For the multi-select, and list question you would pass a list (although in the wrong format as a text) like this:
{“Variable”: “name”, “Answer”: “[‘John’, ‘Tomm’]”}
For the roster items the syntax is little more verbose …
instead of variable name u have to use question identity (guid):
{“Identity”: “d10488a95cd94beb8ee7a37af79b41c8”, “answerr”, 5} would be equivalent of using variable names. but for the roster instances you add indices:
{“Identity”: “[guid]_[parent1]-[parent2]-[parent3]-[parent4]”, “answerr”, val} or
{“Identity”: “d10488a95cd94beb8ee7a37af79b41c8_0-1”, “answerr”, 5} for the second-level roster.

This is how currently API expects data. I’ll try to work on the python client to make it little bit user-friendly, will update my answer here as soon as I have something better.

[UPDATE]
New version 0.3 was uploaded to pypi.

The above example still works of course but maybe little simpler way would be to define data to be uploaded like this:

from ssaw.models import Assignment, InterviewAnswers
from ssaw import AssignmentsApi, QuestionnairesApi

q_doc = QuestionnairesApi(client).document(id="00000000-0000-0000-0000-000000000000", version=1)

d = InterviewAnswers(q_doc)
d.set_answer(variable="address", answer="123 Main Street")
d.set_answer(variable="name", answer="Jane Doe")
# question in the first-level roster
d.set_answer(variable="member_name", answer="Jane", roster_vector=0)
# second-level roster
d.set_answer(variable="pet", answer="Cat", roster_vector=[0, 0])

newobj = Assignment(
    responsible="inter1",
    questionnaire_id="",
    quantity=5,
    identifying_data=d.dict())

res = AssignmentsApi(client).create(newobj)

Thank you, I will try it and let you know if it works for me.

Good Day Zurab,

Using ssaw version 0.1.1 I tried loading data using the list format as you suggested and it works for the list question but doesn’t work for the multi-select question, a 500 server error was returned. Also, can you please clarify working with rosters, there seems to be a typographic error with your code?

After updating ssaw package I compile my code but got the following error;

Traceback (most recent call last):
File “assign.py”, line 3, in
from ssaw.models import Assignment, InterviewAnswers
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ssaw/init.py”, line 2, in
from .assignments import AssignmentsApi
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ssaw/assignments.py”, line 4, in
from .models import Assignment
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ssaw/models.py”, line 6, in
from pydantic import BaseModel, Extra, Field
ModuleNotFoundError: No module named ‘pydantic’

Unfortunately this is again silly mistake on my side, somehow I failed to update the requirements file for the package after adding pydantic to it.

Please do go ahead and install it by calling pip install pydantic. I will update the ssaw package so that it is fixed for the future use.