Skip to content
Advertisement

Syntax for nested dictionaries in Python

I’m developing Python scripts for the automated grading of assignments using CanvasAPI, an API wrapper in Python for the Canvas learning management platform. In studying the documentation, I can successfully issue curl commands in Python for a few parameters. For example, this conversion below is for uploading rubric information for a single submission:

Curl command per the Canvas API docs:

PUT /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id

with:

rubric_assessment[criterion_id][points]

Turns into this via the CanvasAPI Python wrapper

edit(rubric_assessment={'criterion_#':{'points': 'point #'}})

However, I’m having difficulty with the syntax for adding additional parameters, which creates what appears to be nested dictionaries. For example, if I wanted to add a text comment along with grade points, the API documentation offers this:

rubric_assessment[criterion_id][comments]

For which I’ve attempted:

edit(rubric_assessment={'criterion_#':{'points': 'point #'}, {'criterion_#':{'comments': 'comment_text'}}})

which generates a syntax error:

SyntaxError: positional argument follows keyword argument

I’ve also attempted:

edit(rubric_assessment={'criterion_#':{'points': 'point #'},{'comments': 'comment_text'}})

which generates this error:

SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

What is the proper way to structure the syntax to pass both parameters? Many thanks for any insights and assistance.

Advertisement

Answer

from rubric_assessment[criterion_id][comments], it looks like you need, edit(rubric_assessment={'criterion_#':{'points': 'point #','comments':'comment_text'}})

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement