Refactoring. Add ImmutablePoint.
authorRadek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 08:57:06 +0200
changeset 107 1822c37b2688
parent 106 abcadb7e2ef1
child 108 11dac45bfba4
Refactoring. Add ImmutablePoint.
tuikit/core/buffer.py
tuikit/core/coords.py
tuikit/core/widget.py
--- a/tuikit/core/buffer.py	Wed Sep 03 08:53:44 2014 +0200
+++ b/tuikit/core/buffer.py	Wed Sep 03 08:57:06 2014 +0200
@@ -184,7 +184,7 @@
     @property
     def size(self):
         """Width and height of buffer, in characters."""
-        return self._size.readonly()
+        return self._size.immutable()
 
     def resize(self, w, h):
         """Resize buffer."""
--- a/tuikit/core/coords.py	Wed Sep 03 08:53:44 2014 +0200
+++ b/tuikit/core/coords.py	Wed Sep 03 08:57:06 2014 +0200
@@ -5,6 +5,8 @@
 
     Implements attribute access (.x, .y) and list-like access([0],[1]).
 
+    This is heavy-weight mutable object. See also ImmutablePoint.
+
     """
 
     def __init__(self, *args, **kwargs):
@@ -81,7 +83,35 @@
     # string representation
 
     def __repr__(self):
-        return 'Point(x={0.x},y={0.y})'.format(self)
+        return '{0.__class__.__name__}(x={0.x},y={0.y})'.format(self)
+
+    def immutable(self):
+        return ImmutablePoint(*self)
+
+
+class ImmutablePoint:
+
+    """Point class without write access."""
+
+    __slots__ = ('_x', '_y')
+
+    def __init__(self, x, y):
+        self._x = x
+        self._y = y
+
+    @property
+    def x(self):
+        return self._x
+
+    @property
+    def y(self):
+        return self._y
+
+    def __getitem__(self, key):
+        return (self.x, self.y)[key]
+
+    def __repr__(self):
+        return '{0.__class__.__name__}(x={0.x}, y={0.y})'.format(self)
 
 
 class Size:
@@ -90,6 +120,8 @@
 
     Implements attribute access (.w, .h) and list-like access([0],[1]).
 
+    This is heavy-weight mutable object. See also ImmutableSize.
+
     """
 
     def __init__(self, *args, **kwargs):
@@ -101,7 +133,7 @@
         return (self.w, self.h)[key]
 
     def __repr__(self):
-        return 'Size(w={0.w},h={0.h})'.format(self)
+        return '{0.__class__.__name__}(w={0.w},h={0.h})'.format(self)
 
     def update(self, *args, **kwargs):
         """Update size.
@@ -125,30 +157,40 @@
             else:
                 raise ValueError('Bad keyword arg: %r' % key)
 
-    def readonly(self):
-        return ReadonlySize(self)
+    def immutable(self):
+        return ImmutableSize(*self)
 
 
-class ReadonlySize:
+class ImmutableSize:
+
+    """Size class without write access.
 
-    """Wrapper for Size which makes it read-only."""
+    When using reference to (mutable) Size class, properties are not fixed.
+    They can still be changed by original owner and these changes will become
+    visible to other references of the object. ImmutableSize should be used
+    when this is not intended.
 
-    def __init__(self, size):
-        self._size = size
+    """
+
+    __slots__ = ('_w', '_h')
+
+    def __init__(self, w, h):
+        self._w = w
+        self._h = h
 
     @property
     def w(self):
-        return self._size.w
+        return self._w
 
     @property
     def h(self):
-        return self._size.h
+        return self._h
 
     def __getitem__(self, key):
-        return self._size[key]
+        return (self.w, self.h)[key]
 
     def __repr__(self):
-        return 'ReadonlySize(w={0.w},h={0.h})'.format(self._size)
+        return '{0.__class__.__name__}(w={0.w}, h={0.h})'.format(self)
 
 
 class Rect:
@@ -174,7 +216,7 @@
         return Rect(x, y, w, h)
 
     def __repr__(self):
-        return 'Rect(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self)
+        return '{0.__class__.__name__}(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self)
 
     def __contains__(self, point):
         """Test if point is positioned inside rectangle.
--- a/tuikit/core/widget.py	Wed Sep 03 08:53:44 2014 +0200
+++ b/tuikit/core/widget.py	Wed Sep 03 08:57:06 2014 +0200
@@ -65,7 +65,7 @@
 
     @property
     def size(self):
-        return self._size.readonly()
+        return self._size.immutable()
 
     def resize(self, w, h):
         self._size.update(w, h)