1- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.34 2006/10/16 17:28:03 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.35 2006/10/21 18:33:05 tgl Exp $ -->
22
33<chapter id="plpython">
44 <title>PL/Python - Python Procedural Language</title>
@@ -61,11 +61,11 @@ $$ LANGUAGE plpythonu;
6161
6262 <para>
6363 The body of a function is simply a Python script. When the function
64- is called, all unnamed arguments are passed as elements to the array
65- <varname>args[]</varname> and named arguments as ordinary variables to the
66- Python script. The result is returned from the Python code in the usual way,
67- with <literal>return</literal> or <literal>yield</literal> (in case of
68- a resultset statement).
64+ is called, its arguments are passed as elements of the array
65+ <varname>args[]</varname>; named arguments are also passed as ordinary
66+ variables to the Python script. The result is returned from the Python code
67+ in the usual way, with <literal>return</literal> or
68+ <literal>yield</literal> (in case of a resultset statement).
6969 </para>
7070
7171 <para>
@@ -101,9 +101,9 @@ def __plpython_procedure_pymax_23456():
101101 the global <varname>args</varname> list. In the
102102 <function>pymax</function> example, <varname>args[0]</varname> contains
103103 whatever was passed in as the first argument and
104- <varname>args[1]</varname> contains the second argument's value. Alternatively,
105- one can use named parameters as shown in the example above. This greatly simplifies
106- the reading and writing of <application>PL/Python</application> code .
104+ <varname>args[1]</varname> contains the second argument's
105+ value. Alternatively, one can use named parameters as shown in the example
106+ above. Use of named parameters is usually more readable .
107107 </para>
108108
109109 <para>
@@ -161,31 +161,27 @@ $$ LANGUAGE plpythonu;
161161
162162 <para>
163163 There are multiple ways to return row or composite types from a Python
164- scripts. In following examples we assume to have:
164+ function. The following examples assume we have:
165165
166166<programlisting>
167- CREATE TABLE named_value (
168- name text,
169- value integer
170- );
171- </programlisting>
172- or
173- <programlisting>
174167CREATE TYPE named_value AS (
175168 name text,
176169 value integer
177170);
178171</programlisting>
179172
173+ A composite result can be returned as a:
174+
180175 <variablelist>
181176 <varlistentry>
182- <term>Sequence types ( tuple or list) , but not <literal> set</literal> ( because
177+ <term>Sequence type (a tuple or list, but not a set because
183178 it is not indexable)</term>
184179 <listitem>
185180 <para>
186- Returned sequence objects must have the same number of items as
187- composite types have fields. Item with index 0 is assigned to the first field
188- of the composite type, 1 to second and so on. For example:
181+ Returned sequence objects must have the same number of items as the
182+ composite result type has fields. The item with index 0 is assigned to
183+ the first field of the composite type, 1 to the second and so on. For
184+ example:
189185
190186<programlisting>
191187CREATE FUNCTION make_pair (name text, value integer)
@@ -196,7 +192,7 @@ AS $$
196192$$ LANGUAGE plpythonu;
197193</programlisting>
198194
199- To return SQL null in any column, insert <symbol>None</symbol> at
195+ To return a SQL null for any column, insert <symbol>None</symbol> at
200196 the corresponding position.
201197 </para>
202198 </listitem>
@@ -206,8 +202,8 @@ $$ LANGUAGE plpythonu;
206202 <term>Mapping (dictionary)</term>
207203 <listitem>
208204 <para>
209- Value for a composite type's column is retrieved from the mapping with
210- the column name as key. Example:
205+ The value for each result type column is retrieved from the mapping
206+ with the column name as key. Example:
211207
212208<programlisting>
213209CREATE FUNCTION make_pair (name text, value integer)
@@ -217,8 +213,9 @@ AS $$
217213$$ LANGUAGE plpythonu;
218214</programlisting>
219215
220- Additional dictionary key/value pairs are ignored. Missing keys are
221- treated as errors, i.e. to return an SQL null value for any column, insert
216+ Any extra dictionary key/value pairs are ignored. Missing keys are
217+ treated as errors.
218+ To return a SQL null value for any column, insert
222219 <symbol>None</symbol> with the corresponding column name as the key.
223220 </para>
224221 </listitem>
@@ -228,6 +225,7 @@ $$ LANGUAGE plpythonu;
228225 <term>Object (any object providing method <literal>__getattr__</literal>)</term>
229226 <listitem>
230227 <para>
228+ This works the same as a mapping.
231229 Example:
232230
233231<programlisting>
@@ -261,9 +259,9 @@ $$ LANGUAGE plpythonu;
261259
262260 <para>
263261 A <application>PL/Python</application> function can also return sets of
264- scalar or composite types. There are serveral ways to achieve this because
265- the returned object is internally turned into an iterator. For following
266- examples, let's assume to have composite type:
262+ scalar or composite types. There are several ways to achieve this because
263+ the returned object is internally turned into an iterator. The following
264+ examples assume we have composite type:
267265
268266<programlisting>
269267CREATE TYPE greeting AS (
@@ -272,10 +270,11 @@ CREATE TYPE greeting AS (
272270);
273271</programlisting>
274272
275- Currently known iterable types are:
273+ A set result can be returned from a:
274+
276275 <variablelist>
277276 <varlistentry>
278- <term>Sequence types (tuple, list, set)</term>
277+ <term>Sequence type (tuple, list, set)</term>
279278 <listitem>
280279 <para>
281280<programlisting>
@@ -341,19 +340,17 @@ $$ LANGUAGE plpythonu;
341340 <ulink url="http://sourceforge.net/tracker/index.php?func=detail&aid=1483133&group_id=5470&atid=105470">bug #1483133</ulink>,
342341 some debug versions of Python 2.4
343342 (configured and compiled with option <literal>--with-pydebug</literal>)
344- are known to crash the <productname>PostgreSQL</productname> server.
343+ are known to crash the <productname>PostgreSQL</productname> server
344+ when using an iterator to return a set result.
345345 Unpatched versions of Fedora 4 contain this bug.
346- It does not happen in production version of Python or on patched
346+ It does not happen in production versions of Python or on patched
347347 versions of Fedora 4.
348348 </para>
349349 </warning>
350350 </para>
351351 </listitem>
352352 </varlistentry>
353353 </variablelist>
354-
355- Whenever new iterable types are added to Python language,
356- <application>PL/Python</application> is ready to use it.
357354 </para>
358355
359356 <para>
0 commit comments