Ticket #19366: 19366-4.diff

File 19366-4.diff, 4.4 KB (added by claudep, 2 years ago)
  • django/contrib/gis/geos/mutable_list.py

    diff --git a/django/contrib/gis/geos/mutable_list.py b/django/contrib/gis/geos/mutable_list.py
    index 820cdfa..0418282 100644
    a b class ListMixin(object): 
    149149        return self
    150150
    151151    def __eq__(self, other):
    152         for i in range(len(self)):
     152        olen = len(other)
     153        for i in range(olen):
    153154            try:
    154155                c = self[i] == other[i]
    155             except IndexError:
    156                 # must be other is shorter
     156            except self._IndexError:
     157                # self must be shorter
    157158                return False
    158159            if not c:
    159160                return False
    160         return True
     161        return len(self) == olen
    161162
    162163    def __lt__(self, other):
    163         slen = len(self)
    164         for i in range(slen):
     164        olen = len(other)
     165        for i in range(olen):
    165166            try:
    166167                c = self[i] < other[i]
    167             except IndexError:
    168                 # must be other is shorter
    169                 return False
     168            except self._IndexError:
     169                # self must be shorter
     170                return True
    170171            if c:
    171172                return c
    172         return slen < len(other)
     173            elif other[i] < self[i]:
     174                return False
     175        return len(self) < olen
    173176
    174177    ### Public list interface Methods ###
    175178    ## Non-mutating ##
  • django/contrib/gis/geos/tests/test_geos.py

    diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
    index 283daa4..ec320f9 100644
    a b class GEOSTest(unittest.TestCase, TestDataMixin): 
    451451            self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
    452452            self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
    453453
     454    def test_polygon_comparison(self):
     455        p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
     456        p2 = Polygon(((0, 0), (0, 1), (1, 0), (0, 0)))
     457        self.assertTrue(p1 > p2)
     458        self.assertFalse(p1 < p2)
     459        self.assertFalse(p2 > p1)
     460        self.assertTrue(p2 < p1)
     461
     462        p3 = Polygon(((0, 0), (0, 1), (1, 1), (2, 0), (0, 0)))
     463        p4 = Polygon(((0, 0), (0, 1), (2, 2), (1, 0), (0, 0)))
     464        self.assertFalse(p4 < p3)
     465        self.assertTrue(p3 < p4)
     466        self.assertTrue(p4 > p3)
     467        self.assertFalse(p3 > p4)
     468
    454469    def test_multipolygons(self):
    455470        "Testing MultiPolygon objects."
    456471        prev = fromstr('POINT (0 0)')
  • django/contrib/gis/geos/tests/test_mutable_list.py

    diff --git a/django/contrib/gis/geos/tests/test_mutable_list.py b/django/contrib/gis/geos/tests/test_mutable_list.py
    index 675505f..988d841 100644
    a b class ListMixinTest(unittest.TestCase): 
    363363
    364364        pl, ul = self.lists_of_len()
    365365        self.assertEqual(pl, ul, 'cmp for equal')
     366        self.assertFalse(ul == pl + [2], 'cmp for not equal')
    366367        self.assertTrue(pl >= ul, 'cmp for gte self')
    367368        self.assertTrue(pl <= ul, 'cmp for lte self')
    368369        self.assertTrue(ul >= pl, 'cmp for self gte')
    class ListMixinTest(unittest.TestCase): 
    377378        self.assertTrue(ul < pl + [2], 'cmp')
    378379        self.assertTrue(ul <= pl + [2], 'cmp')
    379380
     381        # Also works with a custom IndexError
     382        ul_longer = ul + [2]
     383        ul_longer._IndexError = TypeError
     384        ul._IndexError = TypeError
     385        self.assertFalse(ul_longer == pl)
     386        self.assertFalse(ul == ul_longer)
     387        self.assertTrue(ul_longer > ul)
     388
    380389        pl[1] = 20
    381390        self.assertTrue(pl > ul, 'cmp for gt self')
    382391        self.assertTrue(ul < pl, 'cmp for self lt')
  • docs/ref/contrib/gis/geos.txt

    diff --git a/docs/ref/contrib/gis/geos.txt b/docs/ref/contrib/gis/geos.txt
    index eb20b1f..7d7c327 100644
    a b is returned instead. 
    656656
    657657   Returns the number of interior rings in this geometry.
    658658
     659.. admonition:: Comparing Polygons
     660
     661    Note that it is possible to compare ``Polygon`` objects directly with ``<``
     662    or ``>``, but as the comparison is made through Polygon's
     663    :class:`LineString`, it does not mean much (but is consistent and quick).
     664    You can always force the comparison with the :attr:`~GEOSGeometry.area`
     665    property::
     666
     667        >>> if poly_1.area > poly_2.area:
     668        >>>     pass
     669
    659670Geometry Collections
    660671====================
    661672
Back to Top