{"action":"created","comment":{"url":"https://api.github.com/repos/enthought/python-analytics/pulls/comments/26590077","id":26590077,"diff_hunk":"@@ -0,0 +1,122 @@\n+from __future__ import absolute_import, unicode_literals\n+\n+from abc import ABCMeta, abstractmethod\n+from weakref import WeakKeyDictionary\n+\n+from six import add_metaclass, text_type\n+\n+\n+NoValue = object()\n+\n+\n+@add_metaclass(ABCMeta)\n+class _TrackedAttribute(object):\n+    def __init__(self, type_, required):\n+        self._name = None\n+        self._required = required\n+        self._type = type_\n+        self._data = WeakKeyDictionary()\n+\n+    def __get__(self, instance, owner):\n+        value = self._get_value(instance, owner)\n+        return self._format(value)\n+\n+    def __set__(self, instance, value):\n+        if self._type is not None and not isinstance(value, self._type):\n+            raise TypeError('Expected value {!r} to be of type {!r}'.format(\n+                value, self._type))\n+        self._data[instance] = value\n+\n+    def set_attribute_name(self, name):\n+        self._name = name\n+\n+    def _get_value(self, instance, owner):\n+        value = self._data.get(instance, NoValue)\n+        if self._required and value is NoValue:\n+            raise ValueError(\n+                'Missing required attribute {!r}'.format(self._name))\n+        return value\n+\n+    @abstractmethod\n+    def _format(self, value):\n+        \"\"\"Format the key and value for Google Analytics.\n+\n+        \"\"\"\n+\n+\n+class TrackedAttribute(_TrackedAttribute):\n+\n+    def __init__(self, target_name, type_=None, required=False):\n+        super(TrackedAttribute, self).__init__(type_, required)\n+        self._target_name = target_name\n+\n+    def _format(self, value):\n+        if value is NoValue:\n+            return None\n+        return (self._target_name, value)\n+\n+\n+class _CustomAttribute(_TrackedAttribute):\n+\n+    FORMAT = None\n+    TYPE = None\n+\n+    def __init__(self, index, required=False):\n+        super(_CustomAttribute, self).__init__(self.TYPE, required)\n+        self._index = index\n+\n+    def _format(self, value):\n+        if value is NoValue:\n+            return None\n+        return (self.FORMAT.format(self._index), value)\n+\n+\n+class CustomDimension(_CustomAttribute):\n+\n+    FORMAT = 'cd{:d}'\n+    TYPE = text_type\n+\n+\n+class CustomMetric(_CustomAttribute):\n+\n+    FORMAT = 'cm{:d}'\n+    TYPE = int\n+\n+\n+class Encoder(object):\n+    def __init__(self, **kwargs):\n+        for name, value in kwargs.items():\n+            setattr(self, name, value)\n+\n+    def __setattr__(self, name, value):\n+        if name not in self._tracked_attributes:\n+            raise AttributeError(name)\n+        super(Encoder, self).__setattr__(name, value)\n+\n+    def to_dict(self):\n+        encoded = {}\n+        for attribute_name in self._tracked_attributes:\n+            item = getattr(self, attribute_name)\n+            if item is None:","path":"python_analytics/event_encoder.py","position":100,"original_position":100,"commit_id":"e5b74959b9cde162d5e76a854c9b3eadd83477ea","original_commit_id":"e5b74959b9cde162d5e76a854c9b3eadd83477ea","user":{"login":"sjagoe","id":80012,"avatar_url":"https://avatars.githubusercontent.com/u/80012?v=3","gravatar_id":"","url":"https://api.github.com/users/sjagoe","html_url":"https://github.com/sjagoe","followers_url":"https://api.github.com/users/sjagoe/followers","following_url":"https://api.github.com/users/sjagoe/following{/other_user}","gists_url":"https://api.github.com/users/sjagoe/gists{/gist_id}","starred_url":"https://api.github.com/users/sjagoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjagoe/subscriptions","organizations_url":"https://api.github.com/users/sjagoe/orgs","repos_url":"https://api.github.com/users/sjagoe/repos","events_url":"https://api.github.com/users/sjagoe/events{/privacy}","received_events_url":"https://api.github.com/users/sjagoe/received_events","type":"User","site_admin":false},"body":"If an optional attribute is missing, this is where it is excluded from the encoding.","created_at":"2015-03-17T16:38:57Z","updated_at":"2015-03-17T16:38:57Z","html_url":"https://github.com/enthought/python-analytics/pull/3#discussion_r26590077","pull_request_url":"https://api.github.com/repos/enthought/python-analytics/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/comments/26590077"},"html":{"href":"https://github.com/enthought/python-analytics/pull/3#discussion_r26590077"},"pull_request":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/enthought/python-analytics/pulls/3","id":31341406,"html_url":"https://github.com/enthought/python-analytics/pull/3","diff_url":"https://github.com/enthought/python-analytics/pull/3.diff","patch_url":"https://github.com/enthought/python-analytics/pull/3.patch","issue_url":"https://api.github.com/repos/enthought/python-analytics/issues/3","number":3,"state":"open","locked":false,"title":"Enhancements","user":{"login":"sjagoe","id":80012,"avatar_url":"https://avatars.githubusercontent.com/u/80012?v=3","gravatar_id":"","url":"https://api.github.com/users/sjagoe","html_url":"https://github.com/sjagoe","followers_url":"https://api.github.com/users/sjagoe/followers","following_url":"https://api.github.com/users/sjagoe/following{/other_user}","gists_url":"https://api.github.com/users/sjagoe/gists{/gist_id}","starred_url":"https://api.github.com/users/sjagoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjagoe/subscriptions","organizations_url":"https://api.github.com/users/sjagoe/orgs","repos_url":"https://api.github.com/users/sjagoe/repos","events_url":"https://api.github.com/users/sjagoe/events{/privacy}","received_events_url":"https://api.github.com/users/sjagoe/received_events","type":"User","site_admin":false},"body":"More full-featured event implementation with generalized custom field support.\r\n\r\nAdds Python 2.7 support.","created_at":"2015-03-17T14:46:09Z","updated_at":"2015-03-17T16:38:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f9d895fcb8761fd501a5b823e64df807b5c73a5d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/enthought/python-analytics/pulls/3/commits","review_comments_url":"https://api.github.com/repos/enthought/python-analytics/pulls/3/comments","review_comment_url":"https://api.github.com/repos/enthought/python-analytics/pulls/comments{/number}","comments_url":"https://api.github.com/repos/enthought/python-analytics/issues/3/comments","statuses_url":"https://api.github.com/repos/enthought/python-analytics/statuses/e5b74959b9cde162d5e76a854c9b3eadd83477ea","head":{"label":"enthought:enhancements","ref":"enhancements","sha":"e5b74959b9cde162d5e76a854c9b3eadd83477ea","user":{"login":"enthought","id":539651,"avatar_url":"https://avatars.githubusercontent.com/u/539651?v=3","gravatar_id":"","url":"https://api.github.com/users/enthought","html_url":"https://github.com/enthought","followers_url":"https://api.github.com/users/enthought/followers","following_url":"https://api.github.com/users/enthought/following{/other_user}","gists_url":"https://api.github.com/users/enthought/gists{/gist_id}","starred_url":"https://api.github.com/users/enthought/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enthought/subscriptions","organizations_url":"https://api.github.com/users/enthought/orgs","repos_url":"https://api.github.com/users/enthought/repos","events_url":"https://api.github.com/users/enthought/events{/privacy}","received_events_url":"https://api.github.com/users/enthought/received_events","type":"Organization","site_admin":false},"repo":{"id":32328564,"name":"python-analytics","full_name":"enthought/python-analytics","owner":{"login":"enthought","id":539651,"avatar_url":"https://avatars.githubusercontent.com/u/539651?v=3","gravatar_id":"","url":"https://api.github.com/users/enthought","html_url":"https://github.com/enthought","followers_url":"https://api.github.com/users/enthought/followers","following_url":"https://api.github.com/users/enthought/following{/other_user}","gists_url":"https://api.github.com/users/enthought/gists{/gist_id}","starred_url":"https://api.github.com/users/enthought/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enthought/subscriptions","organizations_url":"https://api.github.com/users/enthought/orgs","repos_url":"https://api.github.com/users/enthought/repos","events_url":"https://api.github.com/users/enthought/events{/privacy}","received_events_url":"https://api.github.com/users/enthought/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/enthought/python-analytics","description":"Google Universal Analytics for Python","fork":false,"url":"https://api.github.com/repos/enthought/python-analytics","forks_url":"https://api.github.com/repos/enthought/python-analytics/forks","keys_url":"https://api.github.com/repos/enthought/python-analytics/keys{/key_id}","collaborators_url":"https://api.github.com/repos/enthought/python-analytics/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/enthought/python-analytics/teams","hooks_url":"https://api.github.com/repos/enthought/python-analytics/hooks","issue_events_url":"https://api.github.com/repos/enthought/python-analytics/issues/events{/number}","events_url":"https://api.github.com/repos/enthought/python-analytics/events","assignees_url":"https://api.github.com/repos/enthought/python-analytics/assignees{/user}","branches_url":"https://api.github.com/repos/enthought/python-analytics/branches{/branch}","tags_url":"https://api.github.com/repos/enthought/python-analytics/tags","blobs_url":"https://api.github.com/repos/enthought/python-analytics/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/enthought/python-analytics/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/enthought/python-analytics/git/refs{/sha}","trees_url":"https://api.github.com/repos/enthought/python-analytics/git/trees{/sha}","statuses_url":"https://api.github.com/repos/enthought/python-analytics/statuses/{sha}","languages_url":"https://api.github.com/repos/enthought/python-analytics/languages","stargazers_url":"https://api.github.com/repos/enthought/python-analytics/stargazers","contributors_url":"https://api.github.com/repos/enthought/python-analytics/contributors","subscribers_url":"https://api.github.com/repos/enthought/python-analytics/subscribers","subscription_url":"https://api.github.com/repos/enthought/python-analytics/subscription","commits_url":"https://api.github.com/repos/enthought/python-analytics/commits{/sha}","git_commits_url":"https://api.github.com/repos/enthought/python-analytics/git/commits{/sha}","comments_url":"https://api.github.com/repos/enthought/python-analytics/comments{/number}","issue_comment_url":"https://api.github.com/repos/enthought/python-analytics/issues/comments{/number}","contents_url":"https://api.github.com/repos/enthought/python-analytics/contents/{+path}","compare_url":"https://api.github.com/repos/enthought/python-analytics/compare/{base}...{head}","merges_url":"https://api.github.com/repos/enthought/python-analytics/merges","archive_url":"https://api.github.com/repos/enthought/python-analytics/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/enthought/python-analytics/downloads","issues_url":"https://api.github.com/repos/enthought/python-analytics/issues{/number}","pulls_url":"https://api.github.com/repos/enthought/python-analytics/pulls{/number}","milestones_url":"https://api.github.com/repos/enthought/python-analytics/milestones{/number}","notifications_url":"https://api.github.com/repos/enthought/python-analytics/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/enthought/python-analytics/labels{/name}","releases_url":"https://api.github.com/repos/enthought/python-analytics/releases{/id}","created_at":"2015-03-16T13:58:05Z","updated_at":"2015-03-16T19:04:04Z","pushed_at":"2015-03-17T16:18:44Z","git_url":"git://github.com/enthought/python-analytics.git","ssh_url":"git@github.com:enthought/python-analytics.git","clone_url":"https://github.com/enthought/python-analytics.git","svn_url":"https://github.com/enthought/python-analytics","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"enthought:master","ref":"master","sha":"26c6641c1fedbd42f1b41b87bc42da2792878506","user":{"login":"enthought","id":539651,"avatar_url":"https://avatars.githubusercontent.com/u/539651?v=3","gravatar_id":"","url":"https://api.github.com/users/enthought","html_url":"https://github.com/enthought","followers_url":"https://api.github.com/users/enthought/followers","following_url":"https://api.github.com/users/enthought/following{/other_user}","gists_url":"https://api.github.com/users/enthought/gists{/gist_id}","starred_url":"https://api.github.com/users/enthought/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enthought/subscriptions","organizations_url":"https://api.github.com/users/enthought/orgs","repos_url":"https://api.github.com/users/enthought/repos","events_url":"https://api.github.com/users/enthought/events{/privacy}","received_events_url":"https://api.github.com/users/enthought/received_events","type":"Organization","site_admin":false},"repo":{"id":32328564,"name":"python-analytics","full_name":"enthought/python-analytics","owner":{"login":"enthought","id":539651,"avatar_url":"https://avatars.githubusercontent.com/u/539651?v=3","gravatar_id":"","url":"https://api.github.com/users/enthought","html_url":"https://github.com/enthought","followers_url":"https://api.github.com/users/enthought/followers","following_url":"https://api.github.com/users/enthought/following{/other_user}","gists_url":"https://api.github.com/users/enthought/gists{/gist_id}","starred_url":"https://api.github.com/users/enthought/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enthought/subscriptions","organizations_url":"https://api.github.com/users/enthought/orgs","repos_url":"https://api.github.com/users/enthought/repos","events_url":"https://api.github.com/users/enthought/events{/privacy}","received_events_url":"https://api.github.com/users/enthought/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/enthought/python-analytics","description":"Google Universal Analytics for Python","fork":false,"url":"https://api.github.com/repos/enthought/python-analytics","forks_url":"https://api.github.com/repos/enthought/python-analytics/forks","keys_url":"https://api.github.com/repos/enthought/python-analytics/keys{/key_id}","collaborators_url":"https://api.github.com/repos/enthought/python-analytics/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/enthought/python-analytics/teams","hooks_url":"https://api.github.com/repos/enthought/python-analytics/hooks","issue_events_url":"https://api.github.com/repos/enthought/python-analytics/issues/events{/number}","events_url":"https://api.github.com/repos/enthought/python-analytics/events","assignees_url":"https://api.github.com/repos/enthought/python-analytics/assignees{/user}","branches_url":"https://api.github.com/repos/enthought/python-analytics/branches{/branch}","tags_url":"https://api.github.com/repos/enthought/python-analytics/tags","blobs_url":"https://api.github.com/repos/enthought/python-analytics/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/enthought/python-analytics/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/enthought/python-analytics/git/refs{/sha}","trees_url":"https://api.github.com/repos/enthought/python-analytics/git/trees{/sha}","statuses_url":"https://api.github.com/repos/enthought/python-analytics/statuses/{sha}","languages_url":"https://api.github.com/repos/enthought/python-analytics/languages","stargazers_url":"https://api.github.com/repos/enthought/python-analytics/stargazers","contributors_url":"https://api.github.com/repos/enthought/python-analytics/contributors","subscribers_url":"https://api.github.com/repos/enthought/python-analytics/subscribers","subscription_url":"https://api.github.com/repos/enthought/python-analytics/subscription","commits_url":"https://api.github.com/repos/enthought/python-analytics/commits{/sha}","git_commits_url":"https://api.github.com/repos/enthought/python-analytics/git/commits{/sha}","comments_url":"https://api.github.com/repos/enthought/python-analytics/comments{/number}","issue_comment_url":"https://api.github.com/repos/enthought/python-analytics/issues/comments{/number}","contents_url":"https://api.github.com/repos/enthought/python-analytics/contents/{+path}","compare_url":"https://api.github.com/repos/enthought/python-analytics/compare/{base}...{head}","merges_url":"https://api.github.com/repos/enthought/python-analytics/merges","archive_url":"https://api.github.com/repos/enthought/python-analytics/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/enthought/python-analytics/downloads","issues_url":"https://api.github.com/repos/enthought/python-analytics/issues{/number}","pulls_url":"https://api.github.com/repos/enthought/python-analytics/pulls{/number}","milestones_url":"https://api.github.com/repos/enthought/python-analytics/milestones{/number}","notifications_url":"https://api.github.com/repos/enthought/python-analytics/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/enthought/python-analytics/labels{/name}","releases_url":"https://api.github.com/repos/enthought/python-analytics/releases{/id}","created_at":"2015-03-16T13:58:05Z","updated_at":"2015-03-16T19:04:04Z","pushed_at":"2015-03-17T16:18:44Z","git_url":"git://github.com/enthought/python-analytics.git","ssh_url":"git@github.com:enthought/python-analytics.git","clone_url":"https://github.com/enthought/python-analytics.git","svn_url":"https://github.com/enthought/python-analytics","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/3"},"html":{"href":"https://github.com/enthought/python-analytics/pull/3"},"issue":{"href":"https://api.github.com/repos/enthought/python-analytics/issues/3"},"comments":{"href":"https://api.github.com/repos/enthought/python-analytics/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/enthought/python-analytics/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/enthought/python-analytics/statuses/e5b74959b9cde162d5e76a854c9b3eadd83477ea"}}}}