10000 gh-90016: Reword sqlite3 adapter/converter docs by erlend-aasland · Pull Request #93095 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-90016: Reword sqlite3 adapter/converter docs #93095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
598e26a
gh-90016: Reword sqlite3 adapter/converter docs
erlend-aasland May 23, 2022
e5b1b88
Add current adapters/converter as recipes: improve this
erlend-aasland May 23, 2022
9399b54
default role
erlend-aasland May 23, 2022
40bb59a
Formatting
erlend-aasland May 23, 2022
82cf3e2
Address Serhiy's review
erlend-aasland May 23, 2022
0b6f381
Address Alex' review
erlend-aasland May 25, 2022
c4dde48
Nit
erlend-aasland May 25, 2022
72013ef
Address Alex' nitpicks
erlend-aasland May 25, 2022
3c70d52
Address in-house review
erlend-aasland May 25, 2022
681baad
Recipes, take 1
erlend-aasland May 25, 2022
bf4cb1f
Docstring wording
erlend-aasland May 25, 2022
2d1a0c1
Update Doc/library/sqlite3.rst
Jun 22, 2022
94308ff
Update Doc/library/sqlite3.rst
Jun 22, 2022
06657f2
Update Doc/library/sqlite3.rst
Jun 22, 2022
b98e363
Update Doc/library/sqlite3.rst
Jun 22, 2022
97812bb
Update Doc/library/sqlite3.rst
Jun 22, 2022
d3dd5a2
Update Doc/library/sqlite3.rst
Jun 22, 2022
f381b65
Update Doc/library/sqlite3.rst
Jun 22, 2022
65eb45c
Update Doc/library/sqlite3.rst
Jun 22, 2022
172c7d9
Update Doc/library/sqlite3.rst
Jun 22, 2022
5ac2af9
Update Doc/library/sqlite3.rst
Jun 22, 2022
433bf5a
Update Doc/library/sqlite3.rst
Jun 22, 2022
f7646de
Update Doc/library/sqlite3.rst
Jun 22, 2022
6fbcff8
Assuming direct control
Jun 22, 2022
b319b54
Address the last part of CAM's review
erlend-aasland Jun 23, 2022
4e3b8fd
Merge branch 'main' into sqlite-doc-converters
erlend-aasland Jun 23, 2022
e821a7e
Clarify parse column names further
erlend-aasland Jun 23, 2022
bc295d8
Revert unneeded change
erlend-aasland Jun 23, 2022
9235f8d
Further clarify register_converter
erlend-aasland Jun 23, 2022
8484164
Use testsetup/doctest
erlend-aasland Jun 23, 2022
b579f67
Revert "Use testsetup/doctest"
erlend-aasland Jun 23, 2022
0e42fa6
Update Doc/library/sqlite3.rst
Jun 24, 2022
8d97fcb
Reflow
erlend-aasland Jun 25, 2022
d300b33
Merge remote-tracking branch 'upstream/main' into sqlite-doc-converters
erlend-aasland Jun 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
10000
Diff view
Prev Previous commit
Next Next commit
Recipes, take 1
  • Loading branch information
erlend-aasland committed May 25, 2022
commit 681baadb2a51816ca8f5b811152ede637944d90d
17 changes: 0 additions & 17 deletions Doc/includes/sqlite3/adapter_datetime.py

This file was deleted.

57 changes: 27 additions & 30 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1255,13 +1255,6 @@ This function can then be registered using :meth:`register_adapter`.

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

The :mod:`sqlite3` module has two default adapters for Python's built-in
:class:`datetime.date` and :class:`datetime.datetime` types.
Suppose we want to store :class:`datetime.datetime` objects not in ISO
representation, but as a Unix timestamp.

.. literalinclude:: ../includes/sqlite3/adapter_datetime.py


Converting SQLite values to custom Python types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -1333,41 +1326,45 @@ timestamp converter.
.. _sqlite3-adapter-converter-recipes:

Adapter and Converter Recipes
-----------------------------
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This section shows recipes for timezone-naive adapters and converters.
This section shows recipes for common adapters and converters.

.. testcode::
.. code-block::

import datetime
import sqlite3

def adapt_date(val):
def adapt_date_iso(val):
"""Adapt datetime.date to ISO 8601 date."""
return val.isoformat()

def adapt_datetime_iso(val):
"""Adapt datetime.datetime to timezone-naive ISO 8601 date."""
return val.isoformat()

def adapt_datetime(val):
return val.isoformat(" ")
def adapt_datetime_epoch(val)
"""Adapt datetime.datetime to Unix timestamp."""
return int(val.timestamp())

sqlite3.register_adapter(datetime.date, adapt_date_iso)
sqlite3.register_adapter(datetime.datetime, adapt_datetime_iso)
sqlite3.register_adapter(datetime.datetime, adapt_datetime_epoch)

def convert_date(val):
import datetime
return datetime.date(*map(int, val.split(b"-")))
"""Convert ISO 8601 date to datetime.date object."""
return datetime.date.fromisoformat(val)

def convert_datetime(val):
"""Convert ISO 8601 datetime to datetime.datetime object."""
return datetime.datetime.fromisoformat(val)

def convert_timestamp(val):
import datetime
datepart, timepart = val.split(b" ")
year, month, day = map(int, datepart.split(b"-"))
timepart_full = timepart.split(b".")
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
if len(timepart_full) == 2:
microseconds = int("{:0<6.6}".format(timepart_full[1].decode()))
else:
microseconds = 0

val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
return val

sqlite3.register_adapter(datetime.date, adapt_date)
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
"""Convert Unix epoch timestamp to datetime.datetime object."""
return datetime.datetime.fromtimestamp(val)

sqlite3.register_converter("date", convert_date)
sqlite3.register_converter("datetime", convert_datetime)
sqlite3.register_converter("timestamp", convert_timestamp)


Expand Down
0