Skip to content

Commit 1bed945

Browse files
committed
Deploying to gh-pages from @ 53b5763 🚀
1 parent c2445ee commit 1bed945

File tree

537 files changed

+1223
-1287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

537 files changed

+1223
-1287
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 4456476df0dbddf14fc886dd81ffe030
3+
config: b4a299ea156afaab0412b26ff0017913
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

_sources/faq/design.rst.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,11 @@ is evaluated in all cases.
259259
Why isn't there a switch or case statement in Python?
260260
-----------------------------------------------------
261261

262-
You can do this easily enough with a sequence of ``if... elif... elif... else``.
263-
For literal values, or constants within a namespace, you can also use a
264-
``match ... case`` statement.
262+
In general, structured switch statements execute one block of code
263+
when an expression has a particular value or set of values.
264+
Since Python 3.10 one can easily match literal values, or constants
265+
within a namespace, with a ``match ... case`` statement.
266+
An older alternative is a sequence of ``if... elif... elif... else``.
265267

266268
For cases where you need to choose from a very large number of possibilities,
267269
you can create a dictionary mapping case values to functions to call. For
@@ -290,6 +292,9 @@ It's suggested that you use a prefix for the method names, such as ``visit_`` in
290292
this example. Without such a prefix, if values are coming from an untrusted
291293
source, an attacker would be able to call any method on your object.
292294

295+
Imitating switch with fallthrough, as with C's switch-case-default,
296+
is possible, much harder, and less needed.
297+
293298

294299
Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?
295300
--------------------------------------------------------------------------------------------------------

_sources/faq/extending.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ to learn Python's C API.
5050
If you need to interface to some C or C++ library for which no Python extension
5151
currently exists, you can try wrapping the library's data types and functions
5252
with a tool such as `SWIG <https://www.swig.org>`_. `SIP
53-
<https://riverbankcomputing.com/software/sip/intro>`__, `CXX
53+
<https://github.com/Python-SIP/sip>`__, `CXX
5454
<https://cxx.sourceforge.net/>`_ `Boost
5555
<https://www.boost.org/libs/python/doc/index.html>`_, or `Weave
5656
<https://github.com/scipy/weave>`_ are also

_sources/howto/logging-cookbook.rst.txt

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,9 +3418,10 @@ The worker thread is implemented using Qt's ``QThread`` class rather than the
34183418
:mod:`threading` module, as there are circumstances where one has to use
34193419
``QThread``, which offers better integration with other ``Qt`` components.
34203420

3421-
The code should work with recent releases of either ``PySide2`` or ``PyQt5``.
3422-
You should be able to adapt the approach to earlier versions of Qt. Please
3423-
refer to the comments in the code snippet for more detailed information.
3421+
The code should work with recent releases of either ``PySide6``, ``PyQt6``,
3422+
``PySide2`` or ``PyQt5``. You should be able to adapt the approach to earlier
3423+
versions of Qt. Please refer to the comments in the code snippet for more
3424+
detailed information.
34243425

34253426
.. code-block:: python3
34263427
@@ -3430,16 +3431,25 @@ refer to the comments in the code snippet for more detailed information.
34303431
import sys
34313432
import time
34323433
3433-
# Deal with minor differences between PySide2 and PyQt5
3434+
# Deal with minor differences between different Qt packages
34343435
try:
3435-
from PySide2 import QtCore, QtGui, QtWidgets
3436+
from PySide6 import QtCore, QtGui, QtWidgets
34363437
Signal = QtCore.Signal
34373438
Slot = QtCore.Slot
34383439
except ImportError:
3439-
from PyQt5 import QtCore, QtGui, QtWidgets
3440-
Signal = QtCore.pyqtSignal
3441-
Slot = QtCore.pyqtSlot
3442-
3440+
try:
3441+
from PyQt6 import QtCore, QtGui, QtWidgets
3442+
Signal = QtCore.pyqtSignal
3443+
Slot = QtCore.pyqtSlot
3444+
except ImportError:
3445+
try:
3446+
from PySide2 import QtCore, QtGui, QtWidgets
3447+
Signal = QtCore.Signal
3448+
Slot = QtCore.Slot
3449+
except ImportError:
3450+
from PyQt5 import QtCore, QtGui, QtWidgets
3451+
Signal = QtCore.pyqtSignal
3452+
Slot = QtCore.pyqtSlot
34433453
34443454
logger = logging.getLogger(__name__)
34453455
@@ -3511,8 +3521,14 @@ refer to the comments in the code snippet for more detailed information.
35113521
while not QtCore.QThread.currentThread().isInterruptionRequested():
35123522
delay = 0.5 + random.random() * 2
35133523
time.sleep(delay)
3514-
level = random.choice(LEVELS)
3515-
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
3524+
try:
3525+
if random.random() < 0.1:
3526+
raise ValueError('Exception raised: %d' % i)
3527+
else:
3528+
level = random.choice(LEVELS)
3529+
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
3530+
except ValueError as e:
3531+
logger.exception('Failed: %s', e, extra=extra)
35163532
i += 1
35173533
35183534
#
@@ -3539,7 +3555,10 @@ refer to the comments in the code snippet for more detailed information.
35393555
self.textedit = te = QtWidgets.QPlainTextEdit(self)
35403556
# Set whatever the default monospace font is for the platform
35413557
f = QtGui.QFont('nosuchfont')
3542-
f.setStyleHint(f.Monospace)
3558+
if hasattr(f, 'Monospace'):
3559+
f.setStyleHint(f.Monospace)
3560+
else:
3561+
f.setStyleHint(f.StyleHint.Monospace) # for Qt6
35433562
te.setFont(f)
35443563
te.setReadOnly(True)
35453564
PB = QtWidgets.QPushButton
@@ -3626,7 +3645,11 @@ refer to the comments in the code snippet for more detailed information.
36263645
app = QtWidgets.QApplication(sys.argv)
36273646
example = Window(app)
36283647
example.show()
3629-
sys.exit(app.exec_())
3648+
if hasattr(app, 'exec'):
3649+
rc = app.exec()
3650+
else:
3651+
rc = app.exec_()
3652+
sys.exit(rc)
36303653
36313654
if __name__=='__main__':
36323655
main()

_sources/howto/logging.rst.txt

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ or *severity*.
2525
When to use logging
2626
^^^^^^^^^^^^^^^^^^^
2727

28-
Logging provides a set of convenience functions for simple logging usage. These
29-
are :func:`debug`, :func:`info`, :func:`warning`, :func:`error` and
30-
:func:`critical`. To determine when to use logging, see the table below, which
31-
states, for each of a set of common tasks, the best tool to use for it.
28+
You can access logging functionality by creating a logger via ``logger =
29+
getLogger(__name__)``, and then calling the logger's :meth:`~Logger.debug`,
30+
:meth:`~Logger.info`, :meth:`~Logger.warning`, :meth:`~Logger.error` and
31+
:meth:`~Logger.critical` methods. To determine when to use logging, and to see
32+
which logger methods to use when, see the table below. It states, for each of a
33+
set of common tasks, the best tool to use for that task.
3234

3335
+-------------------------------------+--------------------------------------+
3436
| Task you want to perform | The best tool for the task |
@@ -37,8 +39,8 @@ states, for each of a set of common tasks, the best tool to use for it.
3739
| usage of a command line script or | |
3840
| program | |
3941
+-------------------------------------+--------------------------------------+
40-
| Report events that occur during | :func:`logging.info` (or |
41-
| normal operation of a program (e.g. | :func:`logging.debug` for very |
42+
| Report events that occur during | A logger's :meth:`~Logger.info` (or |
43+
| normal operation of a program (e.g. | :meth:`~Logger.debug` method for very|
4244
| for status monitoring or fault | detailed output for diagnostic |
4345
| investigation) | purposes) |
4446
+-------------------------------------+--------------------------------------+
@@ -47,22 +49,23 @@ states, for each of a set of common tasks, the best tool to use for it.
4749
| | the client application should be |
4850
| | modified to eliminate the warning |
4951
| | |
50-
| | :func:`logging.warning` if there is |
51-
| | nothing the client application can do|
52-
| | about the situation, but the event |
53-
| | should still be noted |
52+
| | A logger's :meth:`~Logger.warning` |
53+
| | method if there is nothing the client|
54+
| | application can do about the |
55+
| | situation, but the event should still|
56+
| | be noted |
5457
+-------------------------------------+--------------------------------------+
5558
| Report an error regarding a | Raise an exception |
5659
| particular runtime event | |
5760
+-------------------------------------+--------------------------------------+
58-
| Report suppression of an error | :func:`logging.error`, |
59-
| without raising an exception (e.g. | :func:`logging.exception` or |
60-
| error handler in a long-running | :func:`logging.critical` as |
61+
| Report suppression of an error | A logger's :meth:`~Logger.error`, |
62+
| without raising an exception (e.g. | :meth:`~Logger.exception` or |
63+
| error handler in a long-running | :meth:`~Logger.critical` method as |
6164
| server process) | appropriate for the specific error |
6265
| | and application domain |
6366
+-------------------------------------+--------------------------------------+
6467

65-
The logging functions are named after the level or severity of the events
68+
The logger methods are named after the level or severity of the events
6669
they are used to track. The standard levels and their applicability are
6770
described below (in increasing order of severity):
6871

@@ -116,12 +119,18 @@ If you type these lines into a script and run it, you'll see:
116119
WARNING:root:Watch out!
117120
118121
printed out on the console. The ``INFO`` message doesn't appear because the
119-
default level is ``WARNING``. The printed message includes the indication of
120-
the level and the description of the event provided in the logging call, i.e.
121-
'Watch out!'. Don't worry about the 'root' part for now: it will be explained
122-
later. The actual output can be formatted quite flexibly if you need that;
123-
formatting options will also be explained later.
124-
122+
default level is ``WARNING``. The printed message includes the indication of the
123+
level and the description of the event provided in the logging call, i.e.
124+
'Watch out!'. The actual output can be formatted quite flexibly if you need
125+
that; formatting options will also be explained later.
126+
127+
Notice that in this example, we use functions directly on the ``logging``
128+
module, like ``logging.debug``, rather than creating a logger and calling
129+
functions on it. These functions operation on the root logger, but can be useful
130+
as they will call :func:`~logging.basicConfig` for you if it has not been called yet, like in
131+
this example. In larger programs you'll usually want to control the logging
132+
configuration explicitly however - so for that reason as well as others, it's
133+
better to create loggers and call their methods.
125134

126135
Logging to a file
127136
^^^^^^^^^^^^^^^^^
@@ -131,11 +140,12 @@ look at that next. Be sure to try the following in a newly started Python
131140
interpreter, and don't just continue from the session described above::
132141

133142
import logging
143+
logger = logging.getLogger(__name__)
134144
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
135-
logging.debug('This message should go to the log file')
136-
logging.info('So should this')
137-
logging.warning('And this, too')
138-
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
145+
logger.debug('This message should go to the log file')
146+
logger.info('So should this')
147+
logger.warning('And this, too')
148+
logger.error('And non-ASCII stuff, too, like Øresund and Malmö')
139149

140150
.. versionchanged:: 3.9
141151
The *encoding* argument was added. In earlier Python versions, or if not
@@ -149,10 +159,10 @@ messages:
149159

150160
.. code-block:: none
151161
152-
DEBUG:root:This message should go to the log file
153-
INFO:root:So should this
154-
WARNING:root:And this, too
155-
ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö
162+
DEBUG:__main__:This message should go to the log file
163+
INFO:__main__:So should this
164+
WARNING:__main__:And this, too
165+
ERROR:__main__:And non-ASCII stuff, too, like Øresund and Malmö
156166
157167
This example also shows how you can set the logging level which acts as the
158168
threshold for tracking. In this case, because we set the threshold to
@@ -181,11 +191,9 @@ following example::
181191
raise ValueError('Invalid log level: %s' % loglevel)
182192
logging.basicConfig(level=numeric_level, ...)
183193

184-
The call to :func:`basicConfig` should come *before* any calls to
185-
:func:`debug`, :func:`info`, etc. Otherwise, those functions will call
186-
:func:`basicConfig` for you with the default options. As it's intended as a
187-
one-off simple configuration facility, only the first call will actually do
188-
anything: subsequent calls are effectively no-ops.
194+
The call to :func:`basicConfig` should come *before* any calls to a logger's
195+
methods such as :meth:`~Logger.debug`, :meth:`~Logger.info`, etc. Otherwise,
196+
that logging event may not be handled in the desired manner.
189197

190198
If you run the above script several times, the messages from successive runs
191199
are appended to the file *example.log*. If you want each run to start afresh,
@@ -198,50 +206,6 @@ The output will be the same as before, but the log file is no longer appended
198206
to, so the messages from earlier runs are lost.
199207

200208

201-
Logging from multiple modules
202-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203-
204-
If your program consists of multiple modules, here's an example of how you
205-
could organize logging in it::
206-
207-
# myapp.py
208-
import logging
209-
import mylib
210-
211-
def main():
212-
logging.basicConfig(filename='myapp.log', level=logging.INFO)
213-
logging.info('Started')
214-
mylib.do_something()
215-
logging.info('Finished')
216-
217-
if __name__ == '__main__':
218-
main()
219-
220-
::
221-
222-
# mylib.py
223-
import logging
224-
225-
def do_something():
226-
logging.info('Doing something')
227-
228-
If you run *myapp.py*, you should see this in *myapp.log*:
229-
230-
.. code-block:: none
231-
232-
INFO:root:Started
233-
INFO:root:Doing something
234-
INFO:root:Finished
235-
236-
which is hopefully what you were expecting to see. You can generalize this to
237-
multiple modules, using the pattern in *mylib.py*. Note that for this simple
238-
usage pattern, you won't know, by looking in the log file, *where* in your
239-
application your messages came from, apart from looking at the event
240-
description. If you want to track the location of your messages, you'll need
241-
to refer to the documentation beyond the tutorial level -- see
242-
:ref:`logging-advanced-tutorial`.
243-
244-
245209
Logging variable data
246210
^^^^^^^^^^^^^^^^^^^^^
247211

0 commit comments

Comments
 (0)