{"action":"created","comment":{"url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/comments/87971247","pull_request_review_id":8558936,"id":87971247,"diff_hunk":"@@ -0,0 +1,192 @@\n+import re\n+\n+from collections import OrderedDict\n+\n+\n+class Answer(object):\n+    def __init__(self, answer_id, block_id, value, answer_instance=None):\n+        answer_instance = answer_instance or 0\n+\n+        self.answer_id = answer_id\n+        self.block_id = block_id\n+        self.answer_instance = answer_instance\n+        self.value = value\n+\n+    def equals(self, answer):\n+        \"\"\"\n+        Check to see if two answers are the same.\n+        Two answers are considered the same if they share the same block, answer and instance id.\n+\n+        :param answer: An answer to compare\n+        :return: True if both answers are the same, otherwise False.\n+        \"\"\"\n+        return self.block_id == answer.block_id and \\\n+            self.answer_id == answer.answer_id and \\\n+            self.answer_instance == answer.answer_instance\n+\n+    def equals_dict(self, answer_dict):\n+        \"\"\"\n+        Check to see if a dict describes an answer the same as this object.\n+\n+        :param answer_dict: A dict providing\n+        :return:\n+        \"\"\"\n+\n+        return self.equals(Answer(\n+            answer_dict['answer_id'],\n+            answer_dict['block_id'],\n+            \"\",\n+            answer_dict['answer_instance'],\n+        ))\n+\n+\n+class AnswerStore(object):\n+    \"\"\"\n+    An object that stores and updates a collection of answers, ready for serialisation\n+    via the Questionnaire Store.\n+    \"\"\"\n+    def __init__(self, existing_answers=None):\n+        self.answers = existing_answers or []\n+\n+    @staticmethod\n+    def _validate(answer):\n+        if type(answer) is not Answer:\n+            raise TypeError(\"Method only supports Answer argument type\")\n+\n+    def add(self, answer):\n+        \"\"\"\n+        Add a new answer into the answer store.\n+\n+        :param answer: A dict of flattened answer details.\n+        \"\"\"\n+        self._validate(answer)\n+\n+        answer_to_add = answer.__dict__.copy()\n+\n+        if not self.exists(answer):\n+            self.answers.append(answer_to_add)\n+\n+    def update(self, answer):\n+        \"\"\"\n+        Update the value of an answer already in the answer store.\n+\n+        :param answer: A dict of flattened answer details.\n+        \"\"\"\n+        self._validate(answer)\n+\n+        for index, existing in enumerate(self.answers):\n+            if answer.equals_dict(existing):\n+                self.answers[index]['value'] = answer.value\n+                break\n+\n+    def add_or_update(self, answer):\n+        \"\"\"\n+        Add a new answer into the answer store, or update if it exists.\n+\n+        :param answer: An answer object.\n+        \"\"\"\n+        self._validate(answer)\n+\n+        if self.exists(answer):\n+            self.update(answer)\n+        else:\n+            self.add(answer)\n+\n+    def get(self, answer):\n+        \"\"\"\n+        Returns the value of an answer.\n+\n+        :param answer: The ids for the answer ro return\n+        :return: The value of the answer found\n+        \"\"\"\n+        self._validate(answer)\n+\n+        for existing in self.answers:\n+            if answer.equals_dict(existing):\n+                return existing['value']\n+\n+        return None\n+\n+    def exists(self, answer):\n+        \"\"\"\n+        Checks to see if an answer exists in the answer store.\n+\n+        :param answer: A dict of flattened answer details.\n+        :return: True if the answer is in the store, False if not.\n+        \"\"\"\n+        self._validate(answer)\n+\n+        return self.count(answer) > 0\n+\n+    def count(self, answer):\n+        \"\"\"\n+        Count of the number of instances of an answer in the answer store.\n+\n+        :param answer: A dict of flattened answer details.\n+        :return: 0 if the answer doesn't exist, otherwise the number of instances.\n+        \"\"\"\n+        self._validate(answer)\n+\n+        return len(self.filter(answer.answer_id, answer.block_id, answer.answer_instance))\n+\n+    def filter(self, answer_id=None, block_id=None, answer_instance=None):\n+        \"\"\"\n+        Find all answers in the answer store for a given set of filter parameter matches.\n+\n+        :param answer_id:\n+        :param block_id:\n+        :param answer_instance:\n+        :return:\n+        \"\"\"\n+        filtered = []\n+        filter_vars = {\n+            \"answer_id\": answer_id,\n+            \"block_id\": block_id,\n+            \"answer_instance\": answer_instance,\n+        }\n+        for answer in self.answers:\n+            matches = True","path":"app/data_model/answer_store.py","position":148,"original_position":148,"commit_id":"d0a795d3151b9c00ce92575001714687053c8c1d","original_commit_id":"d0a795d3151b9c00ce92575001714687053c8c1d","user":{"login":"benjefferies","id":15050019,"avatar_url":"https://avatars.githubusercontent.com/u/15050019?v=3","gravatar_id":"","url":"https://api.github.com/users/benjefferies","html_url":"https://github.com/benjefferies","followers_url":"https://api.github.com/users/benjefferies/followers","following_url":"https://api.github.com/users/benjefferies/following{/other_user}","gists_url":"https://api.github.com/users/benjefferies/gists{/gist_id}","starred_url":"https://api.github.com/users/benjefferies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjefferies/subscriptions","organizations_url":"https://api.github.com/users/benjefferies/orgs","repos_url":"https://api.github.com/users/benjefferies/repos","events_url":"https://api.github.com/users/benjefferies/events{/privacy}","received_events_url":"https://api.github.com/users/benjefferies/received_events","type":"User","site_admin":false},"body":"I think you need it because it will likely need to match all the criteria to be added","created_at":"2016-11-15T08:56:12Z","updated_at":"2016-11-15T08:56:13Z","html_url":"https://github.com/ONSdigital/eq-survey-runner/pull/559#discussion_r87971247","pull_request_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559","_links":{"self":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/comments/87971247"},"html":{"href":"https://github.com/ONSdigital/eq-survey-runner/pull/559#discussion_r87971247"},"pull_request":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559"}}},"pull_request":{"url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559","id":93356760,"html_url":"https://github.com/ONSdigital/eq-survey-runner/pull/559","diff_url":"https://github.com/ONSdigital/eq-survey-runner/pull/559.diff","patch_url":"https://github.com/ONSdigital/eq-survey-runner/pull/559.patch","issue_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/559","number":559,"state":"open","locked":false,"title":"AnswerStore","user":{"login":"iwootten","id":3598161,"avatar_url":"https://avatars.githubusercontent.com/u/3598161?v=3","gravatar_id":"","url":"https://api.github.com/users/iwootten","html_url":"https://github.com/iwootten","followers_url":"https://api.github.com/users/iwootten/followers","following_url":"https://api.github.com/users/iwootten/following{/other_user}","gists_url":"https://api.github.com/users/iwootten/gists{/gist_id}","starred_url":"https://api.github.com/users/iwootten/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iwootten/subscriptions","organizations_url":"https://api.github.com/users/iwootten/orgs","repos_url":"https://api.github.com/users/iwootten/repos","events_url":"https://api.github.com/users/iwootten/events{/privacy}","received_events_url":"https://api.github.com/users/iwootten/received_events","type":"User","site_admin":false},"body":"### What is the context of this PR?\r\n\r\nAdded an AnswerStore to support the various repeating question requirements for census Household composition questions. \r\n\r\nMigrated the existing calls to get_answers to use AnswerStores map() method in order to support original structure.\r\n\r\n### How to review \r\n\r\nCheck the approach, see if the tests cover behaviour.","created_at":"2016-11-11T16:15:14Z","updated_at":"2016-11-15T08:56:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"587eaa0f9b4887fdc1f25e3901c6c61af3751fce","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559/commits","review_comments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559/comments","review_comment_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/559/comments","statuses_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/statuses/d0a795d3151b9c00ce92575001714687053c8c1d","head":{"label":"ONSdigital:eq-719-answerstore","ref":"eq-719-answerstore","sha":"d0a795d3151b9c00ce92575001714687053c8c1d","user":{"login":"ONSdigital","id":6865137,"avatar_url":"https://avatars.githubusercontent.com/u/6865137?v=3","gravatar_id":"","url":"https://api.github.com/users/ONSdigital","html_url":"https://github.com/ONSdigital","followers_url":"https://api.github.com/users/ONSdigital/followers","following_url":"https://api.github.com/users/ONSdigital/following{/other_user}","gists_url":"https://api.github.com/users/ONSdigital/gists{/gist_id}","starred_url":"https://api.github.com/users/ONSdigital/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ONSdigital/subscriptions","organizations_url":"https://api.github.com/users/ONSdigital/orgs","repos_url":"https://api.github.com/users/ONSdigital/repos","events_url":"https://api.github.com/users/ONSdigital/events{/privacy}","received_events_url":"https://api.github.com/users/ONSdigital/received_events","type":"Organization","site_admin":false},"repo":{"id":49205589,"name":"eq-survey-runner","full_name":"ONSdigital/eq-survey-runner","owner":{"login":"ONSdigital","id":6865137,"avatar_url":"https://avatars.githubusercontent.com/u/6865137?v=3","gravatar_id":"","url":"https://api.github.com/users/ONSdigital","html_url":"https://github.com/ONSdigital","followers_url":"https://api.github.com/users/ONSdigital/followers","following_url":"https://api.github.com/users/ONSdigital/following{/other_user}","gists_url":"https://api.github.com/users/ONSdigital/gists{/gist_id}","starred_url":"https://api.github.com/users/ONSdigital/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ONSdigital/subscriptions","organizations_url":"https://api.github.com/users/ONSdigital/orgs","repos_url":"https://api.github.com/users/ONSdigital/repos","events_url":"https://api.github.com/users/ONSdigital/events{/privacy}","received_events_url":"https://api.github.com/users/ONSdigital/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ONSdigital/eq-survey-runner","description":"eQ Survey Runner","fork":false,"url":"https://api.github.com/repos/ONSdigital/eq-survey-runner","forks_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/forks","keys_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/teams","hooks_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/hooks","issue_events_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/events{/number}","events_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/events","assignees_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/assignees{/user}","branches_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/branches{/branch}","tags_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/tags","blobs_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/refs{/sha}","trees_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/statuses/{sha}","languages_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/languages","stargazers_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/stargazers","contributors_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/contributors","subscribers_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/subscribers","subscription_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/subscription","commits_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/commits{/sha}","git_commits_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/commits{/sha}","comments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/comments{/number}","issue_comment_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/comments{/number}","contents_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/contents/{+path}","compare_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/merges","archive_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/downloads","issues_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues{/number}","pulls_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls{/number}","milestones_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/milestones{/number}","notifications_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/labels{/name}","releases_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/releases{/id}","deployments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/deployments","created_at":"2016-01-07T13:15:26Z","updated_at":"2016-11-10T09:21:31Z","pushed_at":"2016-11-14T18:47:00Z","git_url":"git://github.com/ONSdigital/eq-survey-runner.git","ssh_url":"git@github.com:ONSdigital/eq-survey-runner.git","clone_url":"https://github.com/ONSdigital/eq-survey-runner.git","svn_url":"https://github.com/ONSdigital/eq-survey-runner","homepage":null,"size":8124,"stargazers_count":5,"watchers_count":5,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":14,"forks":2,"open_issues":14,"watchers":5,"default_branch":"master"}},"base":{"label":"ONSdigital:master","ref":"master","sha":"d8ba8cafc34b3a1799903ed09dbcce5b0b7f4e06","user":{"login":"ONSdigital","id":6865137,"avatar_url":"https://avatars.githubusercontent.com/u/6865137?v=3","gravatar_id":"","url":"https://api.github.com/users/ONSdigital","html_url":"https://github.com/ONSdigital","followers_url":"https://api.github.com/users/ONSdigital/followers","following_url":"https://api.github.com/users/ONSdigital/following{/other_user}","gists_url":"https://api.github.com/users/ONSdigital/gists{/gist_id}","starred_url":"https://api.github.com/users/ONSdigital/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ONSdigital/subscriptions","organizations_url":"https://api.github.com/users/ONSdigital/orgs","repos_url":"https://api.github.com/users/ONSdigital/repos","events_url":"https://api.github.com/users/ONSdigital/events{/privacy}","received_events_url":"https://api.github.com/users/ONSdigital/received_events","type":"Organization","site_admin":false},"repo":{"id":49205589,"name":"eq-survey-runner","full_name":"ONSdigital/eq-survey-runner","owner":{"login":"ONSdigital","id":6865137,"avatar_url":"https://avatars.githubusercontent.com/u/6865137?v=3","gravatar_id":"","url":"https://api.github.com/users/ONSdigital","html_url":"https://github.com/ONSdigital","followers_url":"https://api.github.com/users/ONSdigital/followers","following_url":"https://api.github.com/users/ONSdigital/following{/other_user}","gists_url":"https://api.github.com/users/ONSdigital/gists{/gist_id}","starred_url":"https://api.github.com/users/ONSdigital/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ONSdigital/subscriptions","organizations_url":"https://api.github.com/users/ONSdigital/orgs","repos_url":"https://api.github.com/users/ONSdigital/repos","events_url":"https://api.github.com/users/ONSdigital/events{/privacy}","received_events_url":"https://api.github.com/users/ONSdigital/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ONSdigital/eq-survey-runner","description":"eQ Survey Runner","fork":false,"url":"https://api.github.com/repos/ONSdigital/eq-survey-runner","forks_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/forks","keys_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/teams","hooks_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/hooks","issue_events_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/events{/number}","events_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/events","assignees_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/assignees{/user}","branches_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/branches{/branch}","tags_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/tags","blobs_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/refs{/sha}","trees_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/statuses/{sha}","languages_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/languages","stargazers_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/stargazers","contributors_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/contributors","subscribers_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/subscribers","subscription_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/subscription","commits_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/commits{/sha}","git_commits_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/git/commits{/sha}","comments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/comments{/number}","issue_comment_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/comments{/number}","contents_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/contents/{+path}","compare_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/merges","archive_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/downloads","issues_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues{/number}","pulls_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls{/number}","milestones_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/milestones{/number}","notifications_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/labels{/name}","releases_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/releases{/id}","deployments_url":"https://api.github.com/repos/ONSdigital/eq-survey-runner/deployments","created_at":"2016-01-07T13:15:26Z","updated_at":"2016-11-10T09:21:31Z","pushed_at":"2016-11-14T18:47:00Z","git_url":"git://github.com/ONSdigital/eq-survey-runner.git","ssh_url":"git@github.com:ONSdigital/eq-survey-runner.git","clone_url":"https://github.com/ONSdigital/eq-survey-runner.git","svn_url":"https://github.com/ONSdigital/eq-survey-runner","homepage":null,"size":8124,"stargazers_count":5,"watchers_count":5,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":14,"forks":2,"open_issues":14,"watchers":5,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559"},"html":{"href":"https://github.com/ONSdigital/eq-survey-runner/pull/559"},"issue":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/559"},"comments":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/issues/559/comments"},"review_comments":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559/comments"},"review_comment":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/pulls/559/commits"},"statuses":{"href":"https://api.github.com/repos/ONSdigital/eq-survey-runner/statuses/d0a795d3151b9c00ce92575001714687053c8c1d"}}}}
{"actor":{"display_login":"benjefferies"}}