{"id":99,"date":"2009-09-16T21:48:48","date_gmt":"2009-09-16T22:48:48","guid":{"rendered":"http:\/\/www.tamats.com\/blog\/?p=99"},"modified":"2009-09-17T10:14:40","modified_gmt":"2009-09-17T11:14:40","slug":"hackpact-day-8-refactoring-and-some-classes","status":"publish","type":"post","link":"https:\/\/www.tamats.com\/blog\/?p=99","title":{"rendered":"Hackpact Day 8: Refactoring, classes and operators"},"content":{"rendered":"<p>I&#8217;ve been improving my canvas app these days but I didn&#8217;t have time to blog about it, sorry, thats the reason why I&#8217;m a few days behind. Mainly because most of the work done is not really interesting, it is more about refactoring my old code, arranging it in a more clever way, and dealing with stupid problems.<\/p>\n<p>I have improved the way the brushes behave, I created new brushes, and solve some bugs.<\/p>\n<p>The only interesting thing I did was to create a Vector class, the common class you use to store the coordinates of a point. I overwrited all the operators so now is transparent to use the class, it behaves more like a list, but you can multiply it or divide it, operate between Vectors, etc.<\/p>\n<p>That task is kind of fustrating, when you are an experienced C++ programmer and you jump to a high level language like python you always miss some of the low level part of programming. For instance, in python if I have an instance in A and I do &#8220;B = A&#8221; then A and B share the same instance, so vars behave more like pointers.<\/p>\n<p>That is a big source of bugs, because most of the times I dont realize python don&#8217;t do copies unless you say it explicitly, and I have several vars sharing the same instance. So now I tend to solve the problem having an option in the constructor of a class that receives an instance. So I can do:<\/p>\n<p>a = vec([10,10])<\/p>\n<p>b = vec(a) # this is a copy<\/p>\n<p>All the information you need about OOP in python is on the internet, so that is not a big problem. But to code the class Vector was more like a test, because I will end up using the CG library I wrote about some posts ago, I don&#8217;t like to have more dependencies but I don&#8217;t want to code all those low level maths, more when I hardly know how to make efficient functions.<\/p>\n<p>Here is my Vector class, it can be used for 2,3,4,or N dimension vectors, and you can use it where the app expects a list and it wont crash:<\/p>\n<pre><strong>from copy import copy\r\nfrom math import *\r\n\r\nclass vec:\r\n    def __init__(self,v=[0.0,0.0]):\r\n        if type(v) == list:\r\n            self.data=v\r\n        elif type(v) == tuple:\r\n            self.data=list(v)\r\n        elif v.__class__.__name__== self.__class__.__name__:\r\n            self.data = copy(v.data)\r\n        else:\r\n            raise Exception(\"Wrong parameter type:\" + str( type(v)) )\r\n\r\n    def __repr__(self):\r\n        s = \"vec(\"\r\n        for a in self.data: s += \"%0.3f,\"%a\r\n        return s[:-1] + \")\"\r\n\r\n    def toList(self):\r\n        return copy(v.data)\r\n\r\n    # overload []\r\n    def __getitem__(self, index):\r\n        return self.data[index]\r\n\r\n    # overload set []\r\n    def __setitem__(self, key, item):\r\n        self.data[key] = item\r\n\r\n    def __add__(self, other):\r\n        return vec( map(lambda a,b:a+b,self,other) ) #[self.data[0] + other.data[0],self.data[1] + other.data[1]] )\r\n\r\n    def __sub__(self, other):\r\n        return vec( map(lambda a,b:a-b,self,other) )\r\n\r\n    def __mul__(self, other):\r\n        if type(other) == int or type(other) == float:\r\n            return vec( map(lambda a:a*other,self) )\r\n        else:\r\n            return vec( map(lambda a,b:a*b,self,other) )\r\n\r\n    def __div__(self, other):\r\n        if type(other) == int or type(other) == float:\r\n            return vec( map(lambda a:a\/float(other),self) )\r\n        else:\r\n            return vec( map(lambda a,b:a\/float(b),self,other) )\r\n\r\n    # return size to len()\r\n    def __len__(self):\r\n            return len(self.data)\r\n\r\n    def copy(self,v):\r\n        self.data = copy(v.data)\r\n\r\n    def module(self):\r\n        return sqrt(sum(map(lambda a: a*a,self.data) )  )\r\n\r\n    def distance(self,b):\r\n        return (b-self).module()\r\n<\/strong><\/pre>\n<p>Today there is no screenshots or code, sorry, but check the next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been improving my canvas app these days but I didn&#8217;t have time to blog about it, sorry, thats the reason why I&#8217;m a few days behind. Mainly because most of the work done is not really interesting, it is more about refactoring my old code, arranging it in a more clever way, and dealing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-devlog"],"_links":{"self":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":5,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tamats.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}