Skip to content

Commit 0b6f381

Browse files
Address Alex' review
1 parent 82cf3e2 commit 0b6f381

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

Diff for: Doc/includes/sqlite3/converter_point.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def __repr__(self):
88
return f"Point({self.x}, {self.y})"
99

1010
def adapt_point(point):
11-
return ("%f;%f" % (point.x, point.y)).encode("utf-8")
11+
return f"{point.x};{point.y}".encode("utf-8")
1212

1313
def convert_point(s):
1414
x, y = list(map(float, s.split(b";")))

Diff for: Doc/library/sqlite3.rst

+35-29
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ Module functions and constants
199199

200200
.. data:: PARSE_DECLTYPES
201201

202-
Use this flag with the *detect_types* keyword of :meth:`connect` to enable
203-
parsing of declared types for each column it return.
202+
Use this flag with the *detect_types* parameter of :meth:`connect` to enable
203+
parsing of declared types for each column returned.
204204
The types are declared when the database table is created.
205-
:mod:`sqlite3` will look up a converter function using the first word of the
205+
``sqlite3`` will look up a converter function using the first word of the
206206
declared type as the converter dictionary key.
207-
The following SQL code results in the following lookups:
207+
For example, the following SQL code results in the following lookups:
208208

209209
.. code-block:: sql
210210
@@ -220,11 +220,11 @@ Module functions and constants
220220

221221
.. data:: PARSE_COLNAMES
222222

223-
Use this flag with the *detect_types* keyword of :meth:`connect` to enable
223+
Use this flag with the *detect_types* parameter of :meth:`connect` to enable
224224
parsing of column names in queries.
225-
:mod:`sqlite3` will look for strings containing brackets, and will look up a
226-
converter function using the word inside the brackets as the converter
227-
dictionary key.
225+
``sqlite3`` will look for strings containing square brackets (``[]``),
226+
and will look up a converter function using the word inside the brackets as
227+
the converter dictionary key.
228228

229229
.. code-block:: sql
230230
@@ -262,8 +262,8 @@ Module functions and constants
262262
Set it to any combination of :const:`PARSE_DECLTYPES` and
263263
:const:`PARSE_COLNAMES` to enable type detection.
264264
Types cannot be detected for generated fields (for example ``max(data)``),
265-
even when *detect_types* parameter is set. In such cases, the returned type
266-
is :class:`str`.
265+
even when the *detect_types* parameter is set.
266+
In such cases, the returned type is :class:`str`.
267267

268268
By default, *check_same_thread* is :const:`True` and only the creating thread may
269269
use the connection. If set :const:`False`, the returned connection may be shared
@@ -320,9 +320,9 @@ Module functions and constants
320320

321321
.. function:: register_converter(typename, converter)
322322

323-
Register callable *converter* to convert SQLite type name *typename* into a
324-
Python type. The converter is invoked for all SQLite values of type
325-
*typename*. Confer the parameter *detect_types* of
323+
Register the *converter* callable to convert SQLite objects of type *typename* into a
324+
Python object of a specific type. The converter is invoked for all SQLite values of type
325+
*typename*. Consult the parameter *detect_types* of
326326
:meth:`Connection.connect` regarding how type detection works.
327327

328328
Note: *typename* and the name of the type in your query are matched in a
@@ -331,7 +331,7 @@ Module functions and constants
331331

332332
.. function:: register_adapter(type, adapter)
333333

334-
Register callable *adapter* to adapt Python type *type* into an SQLite type.
334+
Register an *adapter* callable to adapt the Python type *type* into an SQLite type.
335335
The adapter is called with a Python object as its sole argument,
336336
and must return a valid SQLite type:
337337
:class:`int`, :class:`float`, :class:`str`, :class:`bytes`, or :const:`None`.
@@ -1219,12 +1219,13 @@ types via converters.
12191219
Using adapters to store custom Python types in SQLite databases
12201220
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12211221

1222+
SQLite supports only a limited set of types natively.
12221223
To store custom Python types in SQLite databases, **adapt** them one of the
12231224
basic types supported by SQLite:
12241225
:class:`int`, :class:`float`, :class:`str`, :class:`bytes`, or :const:`None`.
12251226

12261227
There are two ways to adapt Python objects to SQLite types:
1227-
letting your object adapt itself, or using an adapter function.
1228+
letting your object adapt itself, or using an *adapter callable*.
12281229
The latter will take precedence above the former. For a library that exports a
12291230
custom type, it may make sense to let that type be able to adapt itself. As an
12301231
application developer, it may make more sense to take control, and register
@@ -1236,20 +1237,21 @@ Letting your object adapt itself
12361237

12371238
Suppose we have ``Point`` class that represents a pair of coordinates,
12381239
``x`` and ``y``, in a Cartesian coordinate system.
1239-
We want to store the coordinate pair as a text string in the database,
1240+
The coordinate pair will be stored as a text string in the database,
12401241
using a semicolon to separate the coordinates.
1241-
We implement this by adding a ``__conform__(self, protocol)`` method which
1242-
returns the adapted value. *protocol* will be :class:`PrepareProtocol`.
1242+
This can be implemented by adding a ``__conform__(self, protocol)``
1243+
method which returns the adapted value.
1244+
The object passed to *protocol* will be of type :class:`PrepareProtocol`.
12431245

12441246
.. literalinclude:: ../includes/sqlite3/adapter_point_1.py
12451247

12461248

12471249
Registering an adapter callable
12481250
"""""""""""""""""""""""""""""""
12491251

1250-
Continuing the above example, let's rewrite it using an adapter function.
1251-
We use :meth:`register_adapter` to add an adapter function that takes a Python
1252-
type as its argument, and returns an SQLite compatible type.
1252+
The other possibility is to create a function that converts the Python object
1253+
to an SQLite-compatible type.
1254+
This function can then be registered using :meth:`register_adapter`.
12531255

12541256
.. literalinclude:: ../includes/sqlite3/adapter_point_2.py
12551257

@@ -1264,15 +1266,19 @@ but as a Unix timestamp.
12641266
Converting SQLite values to custom Python types
12651267
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12661268

1269+
Writing an adapter lets you send custom Python types to SQLite. But to make it
1270+
really useful we need to make the Python to SQLite to Python roundtrip work.
12671271
To be able to convert SQLite value to custom Python types, we use _converters_.
12681272

1269-
Let's revisit the ``Point`` class example from above;
1270-
the coordinate pair is stored in the database as a semicolon separated string.
1271-
We define a converter that accept a string, and return a ``Point`` object.
1273+
Let's go back to the :class:`Point` class. We stored the x and y coordinates
1274+
separated via semicolons as strings in SQLite.
1275+
1276+
First, we'll define a converter function that accepts the string as a parameter
1277+
and constructs a :class:`Point` object from it.
12721278

12731279
.. note::
12741280

1275-
Converter functions **always** are passed a :class:`bytes` object,
1281+
Converter functions are **always** passed a :class:`bytes` object,
12761282
no matter the underlying SQLite data type.
12771283

12781284
::
@@ -1281,9 +1287,9 @@ We define a converter that accept a string, and return a ``Point`` object.
12811287
x, y = map(float, s.split(b";"))
12821288
return Point(x, y)
12831289

1284-
We now need to tell :mod:`sqlite3` when it should convert a given SQLite value.
1285-
This is done when connecting to a database, using the *detect_types* keyword of
1286-
:meth:`connect`. We've got three options:
1290+
We now need to tell ``sqlite3`` when it should convert a given SQLite value.
1291+
This is done when connecting to a database, using the *detect_types* parameter
1292+
of :meth:`connect`. We've got three options:
12871293

12881294
* Implicit: set *detect_types* to :const:`PARSE_DECLTYPES`
12891295
* Explicit: set *detect_types* to :const:`PARSE_COLNAMES`
@@ -1336,7 +1342,7 @@ This section shows recipes for common adapters and converters.
13361342

13371343
import sqlite3
13381344

1339-
# Timezone naive datetime adapters and converters.
1345+
# Timezone-naive datetime adapters and converters.
13401346
def adapt_date(val):
13411347
return val.isoformat()
13421348

0 commit comments

Comments
 (0)