Changeset 1578

Show
Ignore:
Timestamp:
07/05/07 12:13:15 (1 year ago)
Author:
arj..@yirdis.nl
Message:

Updated Rectangle's performance. Now you can only add/substract rectangles from rectangles.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/trunk/gaphas/geometry.py

    r1518 r1578  
    133133        >>> r=Rectangle(5, 7, 20, 25) 
    134134        >>> r + (0, 0) 
    135         Rectangle(0, 0, 25, 32) 
     135        Traceback (most recent call last): 
     136          ... 
     137        AttributeError: Can only add Rectangle or tuple (x, y, width, height), not (0, 0). 
    136138        >>> r + (20, 30, 40, 50) 
    137139        Rectangle(5, 7, 55, 73) 
     
    149151        Traceback (most recent call last): 
    150152          ... 
    151         AttributeError: Don't know how to handle <type 'str'> aap. Convert to a Rectangle first
    152         """ 
    153         if isinstance(obj, Rectangle) or len(obj) == 4
     153        AttributeError: Can only add Rectangle or tuple (x, y, width, height), not 'aap'
     154        """ 
     155        try
    154156            x, y, width, height = obj 
    155             x1, y1 = x + width, y + height 
    156         elif len(obj) == 2: 
    157             # extend using a point 
    158             x, y = obj 
    159             width, height = 0, 0 
    160             x1, y1 = obj 
    161         else: 
    162             raise AttributeError, "Don't know how to handle %s %s." \ 
    163                     " Convert to a Rectangle first." % (type(obj), obj) 
     157        except ValueError: 
     158            raise AttributeError, "Can only add Rectangle or tuple (x, y, width, height), not %s." % repr(obj) 
     159        x1, y1 = x + width, y + height 
    164160        if self: 
    165             ox1, oy1 = self.x1, self.y1 
     161            ox1, oy1 = self.x + self.width, self.y + self.height 
    166162            self.x = min(self.x, x) 
    167163            self.y = min(self.y, y) 
     
    195191        Traceback (most recent call last): 
    196192          ... 
    197         AttributeError: Don't know how to handle <type 'str'> aap. Convert to a Rectangle first
    198         """ 
    199         if isinstance(obj, Rectangle) or len(obj) == 4
     193        AttributeError: Can only substract Rectangle or tuple (x, y, width, height), not 'aap'
     194        """ 
     195        try
    200196            x, y, width, height = obj 
    201             x1, y1 = x + width, y + height 
    202         else: 
    203             raise AttributeError, "Don't know how to handle %s %s." \ 
    204                     " Convert to a Rectangle first." % (type(obj), obj) 
     197        except ValueError: 
     198            raise AttributeError, "Can only substract Rectangle or tuple (x, y, width, height), not %s." % repr(obj) 
     199        x1, y1 = x + width, y + height 
     200 
    205201        if self: 
    206             ox1, oy1 = self.x1, self.y1 
     202            ox1, oy1 = self.x + self.width, self.y + self.height 
    207203            self.x = max(self.x, x) 
    208204            self.y = max(self.y, y) 
     
    238234        Traceback (most recent call last): 
    239235          ... 
    240         AttributeError: Don't know how to handle <type 'str'> aap. Convert to a Rectangle or tuple first
    241         """ 
    242         if isinstance(obj, Rectangle) or len(obj) == 4
     236        AttributeError: Should compare to Rectangle, tuple (x, y, width, height) or point (x, y), not 'aap'
     237        """ 
     238        try
    243239            x, y, width, height = obj 
    244240            x1, y1 = x + width, y + width 
    245         elif len(obj) == 2
     241        except ValueError
    246242            # point 
    247             x, y = obj 
    248             x1, y1 = obj 
    249         else: 
    250             raise AttributeError, "Don't know how to handle %s %s." \ 
    251                     " Convert to a Rectangle or tuple first." % (type(obj), obj) 
     243            try: 
     244                x, y = obj 
     245                x1, y1 = obj 
     246            except ValueError: 
     247                raise AttributeError, "Should compare to Rectangle, tuple (x, y, width, height) or point (x, y), not %s." % repr(obj) 
    252248        return x >= self.x and x1 <= self.x1 and \ 
    253249               y >= self.y and y1 <= self.y1