Package pytilities :: Package geometry :: Module boundvector
[hide private]
[frames] | no frames]

Source Code for Module pytilities.geometry.boundvector

 1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
 2  #  
 3  # This file is part of pytilities. 
 4  #  
 5  # pytilities is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  #  
10  # pytilities is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  #  
15  # You should have received a copy of the GNU General Public License 
16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
17  # 
18   
19  __docformat__ = 'reStructuredText' 
20   
21  from pytilities import mangle 
22  from . import Vector 
23 24 -class _DelegatedStorage(object):
25 - def __init__(self, target, x_attribute, y_attribute):
26 self.__target = target 27 self.__x_name = mangle(target, x_attribute) 28 self.__y_name = mangle(target, y_attribute)
29 30 @property
31 - def x(self):
32 return getattr(self.__target, self.__x_name)
33 34 @x.setter
35 - def x(self, value):
36 return setattr(self.__target, self.__x_name, value)
37 38 @property
39 - def y(self):
40 return getattr(self.__target, self.__y_name)
41 42 @y.setter
43 - def y(self, value):
44 return setattr(self.__target, self.__y_name, value)
45
46 47 -class BoundVector(Vector):
48 49 """ 50 `Vector` whose x and y values are stored elsewhere. 51 52 Put differently, it creates a vector view of an x and a y value. 53 54 For `Vector` specific documentation, see `Vector`. 55 """ 56
57 - def __init__(self, target, x_attribute, y_attribute):
58 """ 59 Construct a `BoundVector` 60 61 X and y attributes will be looked up on `target` with names 62 `x_attribute` and `y_attribute`. 63 64 Parameters: 65 66 target 67 the object to bind to. The object on which the x and y 68 attributes reside. 69 70 x_attribute :: string 71 name of attribute of the x value on the target 72 73 y_attribute :: string 74 name of attribute of the y value on the target 75 """ 76 assert(isinstance(x_attribute, basestring)) 77 assert(isinstance(y_attribute, basestring)) 78 79 Vector.__init__(self, _DelegatedStorage(target, 80 x_attribute, 81 y_attribute))
82