[GRASS-SVN] r61867 - in grass/trunk/lib/python/pygrass: gis vector
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Sep 12 01:45:32 PDT 2014
Author: zarch
Date: 2014-09-12 01:45:32 -0700 (Fri, 12 Sep 2014)
New Revision: 61867
Modified:
grass/trunk/lib/python/pygrass/gis/region.py
grass/trunk/lib/python/pygrass/vector/geometry.py
grass/trunk/lib/python/pygrass/vector/table.py
Log:
Remove DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x when running in mode python3, and fix some doctests.
Modified: grass/trunk/lib/python/pygrass/gis/region.py
===================================================================
--- grass/trunk/lib/python/pygrass/gis/region.py 2014-09-11 23:20:33 UTC (rev 61866)
+++ grass/trunk/lib/python/pygrass/gis/region.py 2014-09-12 08:45:32 UTC (rev 61867)
@@ -225,6 +225,17 @@
return self.__unicode__()
def __eq__(self, reg):
+ """Compare two region.
+
+ >>> r0 = Region()
+ >>> r1 = Region()
+ >>> r2 = Region()
+ >>> r2.nsres = 5
+ >>> r0 == r1
+ True
+ >>> r1 == r2
+ False
+ """
attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
'nsres', 'ewres', 'tbres']
for attr in attrs:
@@ -232,6 +243,12 @@
return False
return True
+ def __ne__(self, other):
+ return not self == other
+
+ # Restore Python 2 hashing beaviour on Python 3
+ __hash__ = object.__hash__
+
def keys(self):
"""Return a list of valid keys. ::
@@ -299,10 +316,9 @@
..
"""
from grass.pygrass.vector import VectorTopo
- vect = VectorTopo(vector_name)
- vect.open()
- bbox = vect.bbox()
- self.set_bbox(bbox)
+ with VectorTopo(vector_name, mode='r') as vect:
+ bbox = vect.bbox()
+ self.set_bbox(bbox)
def get_current(self):
"""Set the current GRASS region to the Region object"""
Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py 2014-09-11 23:20:33 UTC (rev 61866)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py 2014-09-12 08:45:32 UTC (rev 61867)
@@ -378,11 +378,27 @@
return "Point(%s)" % ', '.join(['%f' % coor for coor in self.coords()])
def __eq__(self, pnt):
+ """Return True if the coordinates are the same.
+
+ >>> p0 = Point()
+ >>> p1 = Point()
+ >>> p2 = Point(1, 1)
+ >>> p0 == p1
+ True
+ >>> p1 == p2
+ False
+ """
if isinstance(pnt, Point):
return pnt.coords() == self.coords()
return Point(*pnt).coords() == self.coords()
+ def __ne__(self, other):
+ return not self == other
+
+ # Restore Python 2 hashing beaviour on Python 3
+ __hash__ = object.__hash__
+
def coords(self):
"""Return a tuple with the point coordinates. ::
@@ -619,7 +635,7 @@
def append(self, pnt):
"""Appends one point to the end of a line, using the
``Vect_append_point`` C function.
-
+
:param pnt: the point to add to line
:type pnt: a Point object or a tuple with the coordinates
@@ -739,11 +755,11 @@
return libvect.Vect_line_geodesic_length(self.c_points)
def distance(self, pnt):
- """Calculate the distance between line and a point.
-
+ """Calculate the distance between line and a point.
+
:param pnt: the point to calculate distance
:type pnt: a Point object or a tuple with the coordinates
-
+
Return a tuple with:
* the closest point on the line,
@@ -847,7 +863,7 @@
def prune_thresh(self, threshold):
"""Remove points in threshold, using the ``Vect_line_prune_thresh``
C funtion.
-
+
:param threshold: the threshold value where prune points
:type threshold: num
@@ -867,7 +883,7 @@
def remove(self, pnt):
"""Delete point at given index and move all points above down, using
`Vect_line_delete_point` C function.
-
+
:param pnt: the point to remove
:type pnt: a Point object or a tuple with the coordinates
@@ -1087,7 +1103,7 @@
"""Return left value
:param idonly: True to return only the cat of feature
- :type idonly: bool
+ :type idonly: bool
"""
return self._get_centroid(self.left_id, idonly)
@@ -1131,7 +1147,7 @@
Centoid(0.000000, 10.000000)
>>> from grass.pygrass.vector import VectorTopo
>>> geo = VectorTopo('geology')
- >>> geo.open()
+ >>> geo.open(mode='r')
>>> centroid = Centroid(v_id=1, c_mapinfo=geo.c_mapinfo)
>>> centroid
Centoid(893202.874416, 297339.312795)
@@ -1463,7 +1479,7 @@
def contain_pnt(self, pnt, bbox=None):
"""Check if point is in area.
- :param pnt: the point to analyze
+ :param pnt: the point to analyze
:type pnt: a Point object or a tuple with the coordinates
:param bbox: the bounding box where run the analysis
:type bbox: a Bbox object
Modified: grass/trunk/lib/python/pygrass/vector/table.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/table.py 2014-09-11 23:20:33 UTC (rev 61866)
+++ grass/trunk/lib/python/pygrass/vector/table.py 2014-09-12 08:45:32 UTC (rev 61867)
@@ -214,8 +214,27 @@
return self.odict.__len__()
def __eq__(self, obj):
+ """Return True if two table have the same columns.
+
+ >>> import sqlite3
+ >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
+ >>> connection = sqlite3.connect(get_path(path))
+ >>> cols0 = Columns('census', connection)
+ >>> cols1 = Columns('census', connection)
+ >>> cols2 = Columns('hospitals', connection)
+ >>> cols0 == cols1
+ True
+ >>> cols1 == cols2
+ False
+ """
return obj.tname == self.tname and obj.odict == self.odict
+ def __ne__(self, other):
+ return not self == other
+
+ # Restore Python 2 hashing beaviour on Python 3
+ __hash__ = object.__hash__
+
def is_pg(self):
"""Return True if is a psycopg connection. ::
@@ -499,7 +518,7 @@
>>> cols_sqlite.cast('n_pizzas', 'float8') # doctest: +ELLIPSIS
Traceback (most recent call last):
...
- DBError: u'SQLite does not support to cast columns.'
+ DBError: SQLite does not support to cast columns.
>>> import psycopg2 as pg # doctest: +SKIP
>>> cols_pg = Columns('boundary_municp_pg',
... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
@@ -577,7 +596,7 @@
>>> link = Link(1, 'link0', 'census', 'cat',
... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db', 'sqlite')
- >>> link.number # doctest: +SKIP
+ >>> link.layer
1
>>> link.name
'link0'
@@ -697,12 +716,31 @@
return "Link(%d, %s, %s)" % (self.layer, self.name, self.driver)
def __eq__(self, link):
+ """Return True if two Link instance have the same parameters.
+
+ >>> l0 = Link(1, 'link0', 'census', 'cat',
+ ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db', 'sqlite')
+ >>> l1 = Link(1, 'link0', 'census', 'cat',
+ ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db', 'sqlite')
+ >>> l2 = Link(2, 'link0', 'census', 'cat',
+ ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db', 'sqlite')
+ >>> l0 == l1
+ True
+ >>> l1 == l2
+ False
+ """
attrs = ['layer', 'name', 'table_name', 'key', 'driver']
for attr in attrs:
if getattr(self, attr) != getattr(link, attr):
return False
return True
+ def __ne__(self, other):
+ return not self == other
+
+ # Restore Python 2 hashing beaviour on Python 3
+ __hash__ = object.__hash__
+
def connection(self):
"""Return a connection object. ::
@@ -794,7 +832,7 @@
>>> from grass.pygrass.vector import VectorTopo
>>> cens = VectorTopo('census')
- >>> cens.open()
+ >>> cens.open(mode='r')
>>> dblinks = DBlinks(cens.c_mapinfo)
>>> dblinks
DBlinks([Link(1, census, sqlite)])
@@ -802,6 +840,7 @@
Link(1, census, sqlite)
>>> dblinks['census']
Link(1, census, sqlite)
+ >>> cens.close()
..
"""
@@ -873,7 +912,7 @@
>>> from grass.pygrass.vector import VectorTopo
>>> municip = VectorTopo('census')
- >>> municip.open()
+ >>> municip.open(mode='r')
>>> dblinks = DBlinks(municip.c_mapinfo)
>>> dblinks
DBlinks([Link(1, census, sqlite)])
@@ -900,7 +939,7 @@
>>> from grass.pygrass.vector import VectorTopo
>>> municip = VectorTopo('census')
- >>> municip.open()
+ >>> municip.open(mode='r')
>>> dblinks = DBlinks(municip.c_mapinfo)
>>> dblinks
DBlinks([Link(1, census, sqlite)])
More information about the grass-commit
mailing list