Skip to content

Commit

Permalink
Update PL/Python to always be explicitly Python 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Trigona-Harany committed Apr 17, 2022
1 parent 83caf80 commit 2dc6740
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The ``Geometry`` class and its subclasses can be used to convert to and from Pos
return Geometry.from_shapely(largest)
else:
return None
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
This can then be called as part of an SQL query:

Expand Down
8 changes: 4 additions & 4 deletions doc/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Some functions that analyze or manipulate geometries are possible in SQL but are
return Geometry.from_shapely(largest)
else:
return None
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
A pure PL/pgSQL function will have significantly better performance:

Expand Down Expand Up @@ -57,7 +57,7 @@ Another application of ``plpygis`` is accessing external services or commands di
nominatim = Nominatim()
location = nominatim.reverse((lat, lon))
return location.address
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
.. code-block:: psql
Expand Down Expand Up @@ -85,7 +85,7 @@ The `gj2ascii <https://pypi.python.org/pypi/gj2ascii/0.4.1>`_ project allows geo
from plpygis import Geometry
g = Geometry(geom)
return render(g)
$$ LANGUAGE plpythonu
$$ LANGUAGE plpython3u
.. code-block:: psql
Expand Down Expand Up @@ -191,6 +191,6 @@ The function ``_final_geom_show`` will take the ``STYPE`` as the single paramete
geojsons = [Geometry(g) for g in geoms]
layers = zip(geojsons, chars)
return render_multiple(layers, width)
$$ LANGUAGE plpythonu
$$ LANGUAGE plpython3u
PL/Python automatically maps lists to Python arrays, so ``plpygis`` is only responsible for converting each elment of the list (in the example, above this is done using list comprehension: ``[Geometry(g) for g in geoms]``).
20 changes: 10 additions & 10 deletions doc/source/plpython.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Prior to using PL/Python, it must be loaded in the current database:

.. code-block:: psql
# CREATE LANGUAGE plpythonu;
# CREATE LANGUAGE plpython3u;
.. warning::

Expand All @@ -21,7 +21,7 @@ Prior to using PL/Python, it must be loaded in the current database:
Python 2 and Python 3
~~~~~~~~~~~~~~~~~~~~~

``plpygis`` is compatible with both Python 2 and Python 3. ``plpythonu``, however, always refers to Python 2 in PostgreSQL. For Python 3, the language is ``plpython3u`` (Python 2 can also explicitly be used with ``plpython2u``).
``plpygis`` is compatible with both Python 2 and Python 3. For Python 3, the language is ``plpython3u`` and for Python 2 it is ``plpython2u`` (the generic ``plpythonu`` currently refers to Python 2 in PostgreSQL but this may change in the future).

Function declarations
---------------------
Expand All @@ -34,7 +34,7 @@ PL/Python function declarations follow the following template:
RETURNS return-type
AS $$
# PL/Python function body
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
Named arguments are provided as a comma-separated list, with the argument name preceding the argument type:

Expand All @@ -44,7 +44,7 @@ Named arguments are provided as a comma-separated list, with the argument name p
RETURNS geometry
AS $$
# PL/Python function body
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
.. warning::

Expand All @@ -68,7 +68,7 @@ When authoring a Postgres function that takes a PostGIS geometry as an input par
from plpygis import Point
p = Point(x, y)
return p
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
Input parameter
^^^^^^^^^^^^^^^
Expand All @@ -94,7 +94,7 @@ A PostGIS geometry passed as the argument to :meth:`Geometry() <plpygis.geometry
return "East"
else:
return "Meridian"
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
.. code-block:: psql
Expand Down Expand Up @@ -125,7 +125,7 @@ A :class:`Geometry <plpygis.geometry.Geometry>` can be returned directly from a
AS $$
from plpygis import Point
return Point((x, y))
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
.. code-block:: psql
Expand Down Expand Up @@ -185,7 +185,7 @@ In addition to returning single values, ``plpygis`` functions may return a list
p1 = Point(x, y)
p2 = Point(y, x)
return [Geometry.shape(p1), Geometry.shape(p2)]
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
db=# SELECT ST_AsText(make_points(10,20));
st_astext
Expand Down Expand Up @@ -245,7 +245,7 @@ The function indicated by ``SFUNC`` must accept the ``STYPE`` as the first param
RETURNS geometry[]
AS $$
# incremental clustering algorithm here
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;
Alternatively, the ``SFUNC`` can simply collect all the individual geometries into a list and then rely on a single ``FINALFUNC`` to create a new list of geometries that represents the clustered points.

Expand All @@ -267,4 +267,4 @@ The parameter of the ``FINALFUNC`` will be a single ``geometry[]``, representing
RETURNS geometry[]
AS $$
# clustering algorithm here
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpython3u;

0 comments on commit 2dc6740

Please sign in to comment.