GitHub PullRequestReviewCommentEvent at 2014-02-16 02:03:30
Repository: PyMVPA
Actor: yarikoptic
Payload: {"comment":{"url":"https://api.github.com/repos/neurosbh/PyMVPA/pulls/comments/9775534","id":9775534,"diff_hunk":"@@ -0,0 +1,247 @@\n+# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-\n+# vi: set ft=python sts=4 ts=4 sw=4 et:\n+### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n+#\n+#   See COPYING file distributed along with the PyMVPA package for the\n+#   copyright and license terms.\n+#\n+### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##g\n+\"\"\"Parameter representation\"\"\"\n+\n+__docformat__ = 'restructuredtext'\n+\n+import numpy as np\n+\n+if __debug__:\n+    from mvpa2.base import debug\n+\n+class EnsureValue(object):\n+    \"\"\"Base class for input value conversion/validation.\n+\n+    These classes are also meant to be able to generate appropriate\n+    documentation on an appropriate parameter value.\n+    \"\"\"\n+    def __init__(self):\n+        return\n+\n+    def __call__(self, value):\n+        # do any necessary checks or conversions, potentially catch exceptions\n+        # and generate a meaningful error message\n+        return value\n+\n+    # proposed name -- please invent a better one\n+    def long_description(self):\n+        # return meaningful docs or None\n+        # used as a comprehensive description in the parameter list\n+        return self.short_description()\n+\n+    def short_description(self):\n+        # return meaningful docs or None\n+        # used as a condensed primer for the parameter lists\n+        return None\n+\n+class EnsureDType(object):\n+    \"\"\"Ensure that an input is of a particular data type,\n+    and, raises an Exception in case it is not.\n+    \"\"\"\n+    # TODO extend to support numpy-like dtype specs, e.g. 'int64'\n+    # in addition to functors\n+    def __init__(self, dtype):\n+        \"\"\"\n+        Parameters\n+        ----------\n+        dtype : functor\n+        \"\"\"\n+        self._dtype = dtype\n+\n+    def __call__(self, value):\n+        if hasattr(value, '__array__'):\n+            import numpy as np\n+            return np.asanyarray(value, dtype=self._dtype)\n+        elif hasattr(value,'__iter__'):\n+            return map(self._dtype, value)\n+        else:\n+            return self._dtype(value)\n+\n+    def short_description(self):\n+        dtype_descr = str(self._dtype)\n+        if dtype_descr[:7] == \"<type '\" and dtype_descr[-2:] == \"'>\":\n+            dtype_descr = dtype_descr[7:-2]\n+        return dtype_descr\n+\n+    def long_description(self):\n+        return \"value must be convertible to type '%s'\" % self.short_description()\n+\n+class EnsureListOf(EnsureValue):\n+    \"\"\"Ensure that an input is a list of a particular data type\n+    \"\"\"\n+    def __init__(self, dtype):\n+        \"\"\"\n+        Parameters\n+        ----------\n+        dtype : functor\n+        \"\"\"\n+        self._dtype = dtype\n+\n+    def __call__(self, value):\n+        return map(self._dtype, value)\n+\n+    def short_description(self):\n+        dtype_descr = str(self._dtype)\n+        if dtype_descr[:7] == \"<type '\" and dtype_descr[-2:] == \"'>\":\n+            dtype_descr = dtype_descr[7:-2]\n+        return 'list(%s)' % dtype_descr\n+\n+    def long_description(self):\n+        return \"value must be convertible to %s\" % self.short_description()\n+\n+\n+class EnsureInt(EnsureDType):\n+    def __init__(self):\n+        EnsureDType.__init__(self, int)\n+\n+class EnsureFloat(EnsureDType):\n+    def __init__(self):\n+        EnsureDType.__init__(self, float)\n+\n+class EnsureBool(EnsureValue):\n+    def __call__(self, value):\n+        if isinstance(value, bool):\n+            return value\n+        elif value in ('0', 'no', 'off', 'disable', 'false'):\n+            return False\n+        elif value in ('1', 'yes', 'on', 'enable', 'true'):\n+            return True\n+        else:\n+            raise ValueError(\n+                    \"'%s' cannot be converted into a boolean\" % value)","path":"mvpa2/base/constraints.py","position":117,"original_position":117,"commit_id":"afdcf6c5b12a56d5e5df1339e863ce29041d4c5a","original_commit_id":"afdcf6c5b12a56d5e5df1339e863ce29041d4c5a","user":{"login":"yarikoptic","id":39889,"avatar_url":"https://gravatar.com/avatar/6a424256589825f609fd3477144355a0?d=https%3A%2F%2Fidenticons.github.com%2F15d0324c4cba39035aedcf51adc6262b.png&r=x","gravatar_id":"6a424256589825f609fd3477144355a0","url":"https://api.github.com/users/yarikoptic","html_url":"https://github.com/yarikoptic","followers_url":"https://api.github.com/users/yarikoptic/followers","following_url":"https://api.github.com/users/yarikoptic/following{/other_user}","gists_url":"https://api.github.com/users/yarikoptic/gists{/gist_id}","starred_url":"https://api.github.com/users/yarikoptic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yarikoptic/subscriptions","organizations_url":"https://api.github.com/users/yarikoptic/orgs","repos_url":"https://api.github.com/users/yarikoptic/repos","events_url":"https://api.github.com/users/yarikoptic/events{/privacy}","received_events_url":"https://api.github.com/users/yarikoptic/received_events","type":"User","site_admin":false},"body":"may be just \"%r\"? and since just a single argument, either wrap explicitly into a tuple (```(value,)```) or use '%s' with ```repr(value)``` since if value is a tuple with more than 1 element, above would error","created_at":"2014-02-16T02:03:30Z","updated_at":"2014-02-16T02:03:30Z","html_url":"https://github.com/neurosbh/PyMVPA/pull/3#discussion_r9775534","pull_request_url":"https://api.github.com/repos/neurosbh/PyMVPA/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/neurosbh/PyMVPA/pulls/comments/9775534"},"html":{"href":"https://github.com/neurosbh/PyMVPA/pull/3#discussion_r9775534"},"pull_request":{"href":"https://api.github.com/repos/neurosbh/PyMVPA/pulls/3"}}}}