{"id":381,"date":"2009-04-26T07:17:40","date_gmt":"2009-04-26T06:17:40","guid":{"rendered":"http:\/\/fiber-space.de\/wordpress\/?p=381"},"modified":"2009-04-26T07:17:40","modified_gmt":"2009-04-26T06:17:40","slug":"python-vs-ttcn-3","status":"publish","type":"post","link":"http:\/\/fiber-space.de\/wordpress\/2009\/04\/26\/python-vs-ttcn-3\/","title":{"rendered":"Python vs TTCN-3"},"content":{"rendered":"<p>Some time ago computing scientists Bernard Stepien and Liam Peyton from the University of Ottawa <a href=\"http:\/\/www.site.uottawa.ca\/%7Ebernard\/A%20comparison%20between%20ttcn-3%20and%20python%20v%2012.pdf\">compared<\/a> Python with <a href=\"http:\/\/www.ttcn-3.org\/\">TTCN-3<\/a>. TTCN-3 means <span style=\"font-style: italic;\">Testing and Test Control Notation<\/span> and it is domain specific language specifically designed for writing tests in the domain of telecommunication. In almost any category poor Python just loses in comparison.<\/p>\n<p>A few things of the presentation are just odd. At several places it contains Python code that is not even consistent. Examples: on slide 42 an &#8220;else if&#8221; construct is used instead of the correct &#8220;elif&#8221;. On slide 13 fields are not assigned to <span style=\"font-style: italic;\">self<\/span> which leads to code that fails to run. But leaving this carelessnes aside there is something important that estranges an experienced Python programmer. Take slide 12 for example. Here the authors state:<\/p>\n<blockquote><p>TTCN-3 templates to Python<\/p>\n<ul>\n<li><span style=\"font-weight: bold;\">TTCN-3 templates could be mapped to Python object instances.<\/span><\/li>\n<li>However, there are serious limitations using the above technique with Python.<\/li>\n<li>Python objects are practically typeless.<\/li>\n<\/ul>\n<\/blockquote>\n<p>Then the presentation goes over 18 slides just to explain how bad plain Python classes are for serving the same purposes as TTCN-3 templates. Although this is correct why should anyone care? If you want to experience an exhilarating effect you have to turn grapes into wine and do not expect the same fun from grape juice. Then you can compare cheap wine with premium one. That&#8217;s also why people are used to compare Django to Ruby On Rails and not Django to Ruby or Rails to Python. That&#8217;s why libraries and frameworks exist in the first place.<\/p>\n<p>So what about modeling a `Template` class that has same functionality as a TTCN-3 template? Here is a first attempt:<\/p>\n<pre lang=\"python\">class Template(object):\r\n    def __init__(self):\r\n        self._frozen = False\r\n        self._fields = []        \r\n\r\n    def __setattr__(self, name, value):\r\n        if name in ('_fields', '_frozen'):\r\n            object.__setattr__(self, name, value)\r\n            return\r\n        if self._frozen:\r\n            raise RuntimeError(\"Cannot set attribute value on a frozen template\")\r\n        if name not in self._fields:\r\n            self._fields.append(name)\r\n        object.__setattr__(self, name, value)\r\n\r\n    def __eq__(self, other):\r\n        if len(self._fields)!=len(other._fields):\r\n            return False\r\n        else:\r\n            for f_self, f_other in zip(self._fields, other._fields):\r\n                val_self  = getattr(self, f_self)\r\n                val_other = getattr(self, f_other)\r\n                if val_self != val_other:\r\n                    return False\r\n            return True\r\n\r\n    def __ne__(self, other):\r\n        return not self.__eq__(other)\r\n\r\n    def freeze(self):\r\n        self._frozen = True\r\n\r\n    def clone(self):\r\n        T = Template()\r\n        T._fields = self._fields[:]\r\n        for field in T._fields:\r\n            setattr(T, field, getattr(self, field))\r\n        return T<\/pre>\n<p>The `Template` class manages an internal `_fields` attribute that contains the attribute names created by `__setattr__` dynamically. This is done to preserve the sequential order of the fields on template comparison. In the current implementation there aren&#8217;t any fields marked as <em>optional<\/em> and it will take additional effort to introduce them and modify `__eq__`.<\/p>\n<p>It is now easy to demonstrate the behavior of wildcards. First we have to introduce another class for this purpose:<\/p>\n<pre lang=\"python\">class Wildcard(object):\r\n    def __eq__(self, other):\r\n        return True\r\n\r\n    def __ne__(self, other):\r\n        return False<\/pre>\n<p>For any object `X` which is compared to `wildcard` it is always `X == wildcard == True`. So following template instances are equal:<\/p>\n<pre lang=\"python\">\r\ntempl_1 = Template()\r\ntempl_1.field_1 = wildcard\r\ntempl_1.field_2 = \"abc\"\r\n\r\ntempl_2 = Template()\r\ntempl_2.field_1 = \"xyz\"\r\ntempl_2.field_2 = wildcard\r\n\r\nassert templ_1 == templ_2  \r\n<\/pre>\n<p>A `Pattern` class can be created in the same way as the Wildcard class:<\/p>\n<pre lang=\"python\">class Pattern(object):\r\n    def __init__(self, regexp):\r\n        self.pattern = re.compile(regexp)\r\n\r\n    def __eq__(self, other):\r\n        try:\r\n            return bool(self.pattern.match(other))\r\n        except TypeError:\r\n            return True\r\n\r\n    def __ne__(self, other):\r\n        return not self.__eq__(other)<\/pre>\n<p>An `&#8217;==&#8217;` comparison of a `Pattern` instance with a string will match the string against the pattern and yield `True` if the string could be matched and `False` otherwise.<\/p>\n<p>So it is perfectly possible to reproduce most aspects of TTCN-3 templates by just a few lines of Python code. On the downside of `TTCN-3` it isn&#8217;t possible to do things the other way round. No matter how hard you work in TTCN-3 it is impossible to create new TTCN-3 types showing interesting behavior. Pythons expressiveness is miles ahead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago computing scientists Bernard Stepien and Liam Peyton from the University of Ottawa compared Python with TTCN-3. TTCN-3 means Testing and Test Control Notation and it is domain specific language specifically designed for writing tests in the domain &hellip; <a href=\"http:\/\/fiber-space.de\/wordpress\/2009\/04\/26\/python-vs-ttcn-3\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,4,21],"tags":[],"_links":{"self":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/381"}],"collection":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":10,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":391,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/381\/revisions\/391"}],"wp:attachment":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}