{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/48025404","id":48025404,"diff_hunk":"@@ -2197,7 +2197,7 @@ def setup_response(self):\n \n         # if <customresponse> has an \"expect\" (or \"answer\") attribute then save\n         # that\n-        self.expect = xml.get('expect') or xml.get('answer')","path":"common/lib/capa/capa/responsetypes.py","position":4,"original_position":4,"commit_id":"f502a5ee098b5fdf0171f84d62bfbc235401cd59","original_commit_id":"f502a5ee098b5fdf0171f84d62bfbc235401cd59","user":{"login":"amir-qayyum-khan","id":10431250,"avatar_url":"https://avatars.githubusercontent.com/u/10431250?v=3","gravatar_id":"","url":"https://api.github.com/users/amir-qayyum-khan","html_url":"https://github.com/amir-qayyum-khan","followers_url":"https://api.github.com/users/amir-qayyum-khan/followers","following_url":"https://api.github.com/users/amir-qayyum-khan/following{/other_user}","gists_url":"https://api.github.com/users/amir-qayyum-khan/gists{/gist_id}","starred_url":"https://api.github.com/users/amir-qayyum-khan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amir-qayyum-khan/subscriptions","organizations_url":"https://api.github.com/users/amir-qayyum-khan/orgs","repos_url":"https://api.github.com/users/amir-qayyum-khan/repos","events_url":"https://api.github.com/users/amir-qayyum-khan/events{/privacy}","received_events_url":"https://api.github.com/users/amir-qayyum-khan/received_events","type":"User","site_admin":false},"body":"@msegado expect is little confusing. can we have expected or something more meaningful ?","created_at":"2015-12-18T13:41:51Z","updated_at":"2015-12-18T13:41:51Z","html_url":"https://github.com/edx/edx-platform/pull/10971#discussion_r48025404","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/10971","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/48025404"},"html":{"href":"https://github.com/edx/edx-platform/pull/10971#discussion_r48025404"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/10971"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/10971","id":53509289,"html_url":"https://github.com/edx/edx-platform/pull/10971","diff_url":"https://github.com/edx/edx-platform/pull/10971.diff","patch_url":"https://github.com/edx/edx-platform/pull/10971.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/10971","number":10971,"state":"open","locked":false,"title":"Allow python variable interpolation in CustomResponse \"expect\" field","user":{"login":"msegado","id":1887854,"avatar_url":"https://avatars.githubusercontent.com/u/1887854?v=3","gravatar_id":"","url":"https://api.github.com/users/msegado","html_url":"https://github.com/msegado","followers_url":"https://api.github.com/users/msegado/followers","following_url":"https://api.github.com/users/msegado/following{/other_user}","gists_url":"https://api.github.com/users/msegado/gists{/gist_id}","starred_url":"https://api.github.com/users/msegado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msegado/subscriptions","organizations_url":"https://api.github.com/users/msegado/orgs","repos_url":"https://api.github.com/users/msegado/repos","events_url":"https://api.github.com/users/msegado/events{/privacy}","received_events_url":"https://api.github.com/users/msegado/received_events","type":"User","site_admin":false},"body":"**Summary**:  This PR adds python variable interpolation to custom response problems (a behavior present in most other response types which appears to have been accidentally omitted here). The behavior is implemented by wrapping the `expect`/`answer` value in the same `contextualize_text` function used in the other response types, with a unit test to make sure it works.\r\n\r\n**Background**: The edX platform allows python variable interpolation in various places within advanced problems. This behavior is particularly useful for randomized problems (see the example in \"[Create a Randomized Custom Python-Evaluated Input Problem](http://edx.readthedocs.org/projects/edx-partner-course-staff/en/latest/exercises_tools/custom_python.html#create-a-randomized-custom-python-evaluated-input-problem)\") and also mentioned in the docs for [math expression input problems](http://edx.readthedocs.org/projects/edx-partner-course-staff/en/latest/exercises_tools/math_expression_input.html) and [numerical input problems](http://edx.readthedocs.org/projects/edx-partner-course-staff/en/latest/exercises_tools/numerical_input.html) (search for \"$\" on either page to find examples). A look at the platform source shows that it is supported in a wide variety of locations:\r\n\r\n* In capa `<solution></solution>` stanzas: [capa_problem.py#L463](https://github.com/edx/edx-platform/blob/222bdd988bf1e72cf6359f5c6de82934fd9efacf/common/lib/capa/capa/capa_problem.py#L463)\r\n* In capa HTML content: [capa_problem.py#L558](https://github.com/edx/edx-platform/blob/222bdd988bf1e72cf6359f5c6de82934fd9efacf/common/lib/capa/capa/capa_problem.py#L558)\r\n* In the `correct_answer` field of input problems: [responsetypes.py#L217](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L217)\r\n* In the `value` field of response parameters: [responsetypes.py#L713](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L713)\r\n* In the `name` and `correct` fields of multiple choice items: [responsetypes.py#L1173](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L1173)\r\n* In the `correct` field of option response items: [responsetypes.py#L1606](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L1606)\r\n* In the `answer` field and tolerance parameter of numerical response problems: [responsetypes.py#L1682](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L1682)\r\n* In the `answer` field of string response problems, as well as in the `answer` fields of their `<additional_answer>` children and in any hints: [responsetypes.py#L1997](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L1997), [responsetypes.py#L2158](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L2158)\r\n* In the `answer`/`sample` fields and tolerance parameter of formula response problems, as well as in their hints: [responsetypes.py#L3145](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L3145), [responsetypes.py#L3314](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L3314)\r\n* In the `answer` and `tolerance` fields of choice text response problems: [responsetypes.py#L3718](https://github.com/edx/edx-platform/blob/dcc86a3b1b4f03bc84da8e78e063b1ef553714cc/common/lib/capa/capa/responsetypes.py#L3718)\r\n\r\nUnfortunately, variable interpolation is *not* implemented in the `expect` field (and its fallback alias, `answer`) of custom response problems; in these problems, the un-interpolated variable name is displayed when showing the answer, and the problem does not grade correctly. This seems to be a simple oversight, not an intentional design decision.\r\n\r\n**Testing**: Python variable interpolation should work in the `expect` (or `answer`) field of custom response problems. The following (slightly-contrived) OLX should display a random integer when the answer is shown, not the literal string \"$divisor\", and should grade the problem correctly when the randomly-generated divisor is entered as an answer.\r\n```xml\r\n<problem>\r\n<script type=\"loncapa/python\">\r\ndivisor = random.randint(1, 10)\r\n\r\ndef test_divisible(expect, ans):\r\n    try:\r\n        ans=int(ans)\r\n        return ans % int(expect) == 0\r\n    except ValueError:\r\n        return False\r\n</script>\r\n<p>Enter an integer divisible by $divisor. </p>\r\n<customresponse cfn=\"test_divisible\" expect=\"$divisor\">\r\n    <textline size=\"10\"/>\r\n</customresponse>\r\n</problem>\r\n```\r\n\r\n**Studio**: No changes necessary (this is an advanced problem type).\r\n\r\n**Accessibility**: No effect; this behavior is concerned with how answers are generated by custom python code, not with how they're displayed in studio or the LMS.\r\n\r\n**Documentation**: No changes are strictly necessary since the behavior added by this PR is consistent with variable interpolation in other problem fields, many of which are likewise not explicitly mentioned in the docs. Eventually it might be nice to document all the places where interpolated variables can be used (the list in the background section above should be a good start), but that seems like its own issue.","created_at":"2015-12-13T20:51:30Z","updated_at":"2015-12-18T13:41:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d0eb9451c0a9333c704b073639f8103822d60549","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/10971/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/10971/comments","review_comment_url":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-platform/issues/10971/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/f502a5ee098b5fdf0171f84d62bfbc235401cd59","head":{"label":"msegado:msegado/customresponse-expect-context","ref":"msegado/customresponse-expect-context","sha":"f502a5ee098b5fdf0171f84d62bfbc235401cd59","user":{"login":"msegado","id":1887854,"avatar_url":"https://avatars.githubusercontent.com/u/1887854?v=3","gravatar_id":"","url":"https://api.github.com/users/msegado","html_url":"https://github.com/msegado","followers_url":"https://api.github.com/users/msegado/followers","following_url":"https://api.github.com/users/msegado/following{/other_user}","gists_url":"https://api.github.com/users/msegado/gists{/gist_id}","starred_url":"https://api.github.com/users/msegado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msegado/subscriptions","organizations_url":"https://api.github.com/users/msegado/orgs","repos_url":"https://api.github.com/users/msegado/repos","events_url":"https://api.github.com/users/msegado/events{/privacy}","received_events_url":"https://api.github.com/users/msegado/received_events","type":"User","site_admin":false},"repo":{"id":21219542,"name":"edx-platform","full_name":"msegado/edx-platform","owner":{"login":"msegado","id":1887854,"avatar_url":"https://avatars.githubusercontent.com/u/1887854?v=3","gravatar_id":"","url":"https://api.github.com/users/msegado","html_url":"https://github.com/msegado","followers_url":"https://api.github.com/users/msegado/followers","following_url":"https://api.github.com/users/msegado/following{/other_user}","gists_url":"https://api.github.com/users/msegado/gists{/gist_id}","starred_url":"https://api.github.com/users/msegado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msegado/subscriptions","organizations_url":"https://api.github.com/users/msegado/orgs","repos_url":"https://api.github.com/users/msegado/repos","events_url":"https://api.github.com/users/msegado/events{/privacy}","received_events_url":"https://api.github.com/users/msegado/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/msegado/edx-platform","description":"Open edX, the edX platform that powers http://www.edx.org","fork":true,"url":"https://api.github.com/repos/msegado/edx-platform","forks_url":"https://api.github.com/repos/msegado/edx-platform/forks","keys_url":"https://api.github.com/repos/msegado/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/msegado/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/msegado/edx-platform/teams","hooks_url":"https://api.github.com/repos/msegado/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/msegado/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/msegado/edx-platform/events","assignees_url":"https://api.github.com/repos/msegado/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/msegado/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/msegado/edx-platform/tags","blobs_url":"https://api.github.com/repos/msegado/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/msegado/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/msegado/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/msegado/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/msegado/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/msegado/edx-platform/languages","stargazers_url":"https://api.github.com/repos/msegado/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/msegado/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/msegado/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/msegado/edx-platform/subscription","commits_url":"https://api.github.com/repos/msegado/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/msegado/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/msegado/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/msegado/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/msegado/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/msegado/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/msegado/edx-platform/merges","archive_url":"https://api.github.com/repos/msegado/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/msegado/edx-platform/downloads","issues_url":"https://api.github.com/repos/msegado/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/msegado/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/msegado/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/msegado/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/msegado/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/msegado/edx-platform/releases{/id}","created_at":"2014-06-25T21:39:28Z","updated_at":"2014-06-25T12:52:30Z","pushed_at":"2015-12-13T20:39:21Z","git_url":"git://github.com/msegado/edx-platform.git","ssh_url":"git@github.com:msegado/edx-platform.git","clone_url":"https://github.com/msegado/edx-platform.git","svn_url":"https://github.com/msegado/edx-platform","homepage":"http://code.edx.org/","size":525900,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"7a8f372e259daec1a09cd94271b72c5b9a412987","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"The Open edX platform, the software that powers edX!","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-12-18T01:21:22Z","pushed_at":"2015-12-18T13:19:53Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://open.edx.org/","size":634153,"stargazers_count":2314,"watchers_count":2314,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1401,"mirror_url":null,"open_issues_count":132,"forks":1401,"open_issues":132,"watchers":2314,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/10971"},"html":{"href":"https://github.com/edx/edx-platform/pull/10971"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/10971"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/10971/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/10971/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/10971/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/f502a5ee098b5fdf0171f84d62bfbc235401cd59"}}}}