Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b35480e

Browse files
committed
#14770: improve the library FAQ.
1 parent 3918b1e commit b35480e

1 file changed

Lines changed: 45 additions & 54 deletions

File tree

Doc/faq/library.rst

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ How do I find a module or application to perform task X?
1414

1515
Check :ref:`the Library Reference <library-index>` to see if there's a relevant
1616
standard library module. (Eventually you'll learn what's in the standard
17-
library and will able to skip this step.)
17+
library and will be able to skip this step.)
1818

1919
For third-party packages, search the `Python Package Index
2020
<http://pypi.python.org/pypi>`_ or try `Google <http://www.google.com>`_ or
@@ -28,7 +28,7 @@ Where is the math.py (socket.py, regex.py, etc.) source file?
2828
If you can't find a source file for a module it may be a built-in or
2929
dynamically loaded module implemented in C, C++ or other compiled language.
3030
In this case you may not have the source file or it may be something like
31-
mathmodule.c, somewhere in a C source directory (not on the Python Path).
31+
:file:`mathmodule.c`, somewhere in a C source directory (not on the Python Path).
3232

3333
There are (at least) three kinds of modules in Python:
3434

@@ -60,18 +60,18 @@ as the very first line of your file, using the pathname for where the Python
6060
interpreter is installed on your platform.
6161

6262
If you would like the script to be independent of where the Python interpreter
63-
lives, you can use the "env" program. Almost all Unix variants support the
64-
following, assuming the Python interpreter is in a directory on the user's
65-
$PATH::
63+
lives, you can use the :program:`env` program. Almost all Unix variants support
64+
the following, assuming the Python interpreter is in a directory on the user's
65+
:envvar:`PATH`::
6666

6767
#!/usr/bin/env python
6868

69-
*Don't* do this for CGI scripts. The $PATH variable for CGI scripts is often
70-
very minimal, so you need to use the actual absolute pathname of the
69+
*Don't* do this for CGI scripts. The :envvar:`PATH` variable for CGI scripts is
70+
often very minimal, so you need to use the actual absolute pathname of the
7171
interpreter.
7272

73-
Occasionally, a user's environment is so full that the /usr/bin/env program
74-
fails; or there's no env program at all. In that case, you can try the
73+
Occasionally, a user's environment is so full that the :program:`/usr/bin/env`
74+
program fails; or there's no env program at all. In that case, you can try the
7575
following hack (due to Alex Rezinsky)::
7676

7777
#! /bin/sh
@@ -92,11 +92,11 @@ Is there a curses/termcap package for Python?
9292
.. XXX curses *is* built by default, isn't it?
9393
9494
For Unix variants: The standard Python source distribution comes with a curses
95-
module in the ``Modules/`` subdirectory, though it's not compiled by default
96-
(note that this is not available in the Windows distribution -- there is no
97-
curses module for Windows).
95+
module in the :source:`Modules` subdirectory, though it's not compiled by default.
96+
(Note that this is not available in the Windows distribution -- there is no
97+
curses module for Windows.)
9898

99-
The curses module supports basic curses features as well as many additional
99+
The :mod:`curses` module supports basic curses features as well as many additional
100100
functions from ncurses and SYSV curses such as colour, alternative character set
101101
support, pads, and mouse support. This means the module isn't compatible with
102102
operating systems that only have BSD curses, but there don't seem to be any
@@ -110,7 +110,7 @@ Is there an equivalent to C's onexit() in Python?
110110
-------------------------------------------------
111111

112112
The :mod:`atexit` module provides a register function that is similar to C's
113-
onexit.
113+
:c:func:`onexit`.
114114

115115

116116
Why don't my signal handlers work?
@@ -140,8 +140,8 @@ the expected output given in the docstring.
140140
The :mod:`unittest` module is a fancier testing framework modelled on Java and
141141
Smalltalk testing frameworks.
142142

143-
For testing, it helps to write the program so that it may be easily tested by
144-
using good modular design. Your program should have almost all functionality
143+
To make testing easier, you should use good modular design in your program.
144+
Your program should have almost all functionality
145145
encapsulated in either functions or class methods -- and this sometimes has the
146146
surprising and delightful effect of making the program run faster (because local
147147
variable accesses are faster than global accesses). Furthermore the program
@@ -157,7 +157,7 @@ at the bottom of the main module of your program.
157157

158158
Once your program is organized as a tractable collection of functions and class
159159
behaviours you should write test functions that exercise the behaviours. A test
160-
suite can be associated with each module which automates a sequence of tests.
160+
suite that automates a sequence of tests can be associated with each module.
161161
This sounds like a lot of work, but since Python is so terse and flexible it's
162162
surprisingly easy. You can make coding much more pleasant and fun by writing
163163
your test functions in parallel with the "production code", since this makes it
@@ -186,7 +186,7 @@ docstrings is `epydoc <http://epydoc.sf.net/>`_. `Sphinx
186186
How do I get a single keypress at a time?
187187
-----------------------------------------
188188

189-
For Unix variants: There are several solutions. It's straightforward to do this
189+
For Unix variants there are several solutions. It's straightforward to do this
190190
using curses, but curses is a fairly large module to learn.
191191

192192
.. XXX this doesn't work out of the box, some IO expert needs to check why
@@ -275,7 +275,7 @@ A simple fix is to add a tiny sleep to the start of the run function::
275275

276276
time.sleep(10)
277277

278-
Instead of trying to guess how long a :func:`time.sleep` delay will be enough,
278+
Instead of trying to guess a good delay value for :func:`time.sleep`,
279279
it's better to use some kind of semaphore mechanism. One idea is to use the
280280
:mod:`queue` module to create a queue object, let each thread append a token to
281281
the queue when it finishes, and let the main thread read as many tokens from the
@@ -291,9 +291,9 @@ especially the :mod:`~concurrent.futures.ThreadPoolExecutor` class.
291291
Or, if you want fine control over the dispatching algorithm, you can write
292292
your own logic manually. Use the :mod:`queue` module to create a queue
293293
containing a list of jobs. The :class:`~queue.Queue` class maintains a
294-
list of objects with ``.put(obj)`` to add an item to the queue and ``.get()``
295-
to return an item. The class will take care of the locking necessary to
296-
ensure that each job is handed out exactly once.
294+
list of objects and has a ``.put(obj)`` method that adds items to the queue and
295+
a ``.get()`` method to return them. The class will take care of the locking
296+
necessary to ensure that each job is handed out exactly once.
297297

298298
Here's a trivial example::
299299

@@ -302,7 +302,7 @@ Here's a trivial example::
302302
# The worker thread gets jobs off the queue. When the queue is empty, it
303303
# assumes there will be no more work and exits.
304304
# (Realistically workers will run until terminated.)
305-
def worker ():
305+
def worker():
306306
print('Running worker')
307307
time.sleep(0.1)
308308
while True:
@@ -333,7 +333,9 @@ Here's a trivial example::
333333
print('Main thread sleeping')
334334
time.sleep(5)
335335

336-
When run, this will produce the following output::
336+
When run, this will produce the following output:
337+
338+
.. code-block:: none
337339
338340
Running worker
339341
Running worker
@@ -349,8 +351,8 @@ When run, this will produce the following output::
349351
Worker <Thread(worker 1, started 130283832797456)> running with argument 5
350352
...
351353
352-
Consult the module's documentation for more details; the ``Queue`` class
353-
provides a featureful interface.
354+
Consult the module's documentation for more details; the :class:`~queue.Queue``
355+
class provides a featureful interface.
354356

355357

356358
What kinds of global value mutation are thread-safe?
@@ -467,7 +469,7 @@ To rename a file, use ``os.rename(old_path, new_path)``.
467469
To truncate a file, open it using ``f = open(filename, "rb+")``, and use
468470
``f.truncate(offset)``; offset defaults to the current seek position. There's
469471
also ``os.ftruncate(fd, offset)`` for files opened with :func:`os.open`, where
470-
``fd`` is the file descriptor (a small integer).
472+
*fd* is the file descriptor (a small integer).
471473

472474
The :mod:`shutil` module also contains a number of functions to work on files
473475
including :func:`~shutil.copyfile`, :func:`~shutil.copytree`, and
@@ -501,15 +503,15 @@ The '>' in the format string forces big-endian data; the letter 'h' reads one
501503
"short integer" (2 bytes), and 'l' reads one "long integer" (4 bytes) from the
502504
string.
503505

504-
For data that is more regular (e.g. a homogeneous list of ints or thefloats),
506+
For data that is more regular (e.g. a homogeneous list of ints or floats),
505507
you can also use the :mod:`array` module.
506508

507-
.. note::
508-
To read and write binary data, it is mandatory to open the file in
509-
binary mode (here, passing ``"rb"`` to :func:`open`). If you use
510-
``"r"`` instead (the default), the file will be open in text mode
511-
and ``f.read()`` will return :class:`str` objects rather than
512-
:class:`bytes` objects.
509+
.. note::
510+
To read and write binary data, it is mandatory to open the file in
511+
binary mode (here, passing ``"rb"`` to :func:`open`). If you use
512+
``"r"`` instead (the default), the file will be open in text mode
513+
and ``f.read()`` will return :class:`str` objects rather than
514+
:class:`bytes` objects.
513515

514516

515517
I can't seem to use os.read() on a pipe created with os.popen(); why?
@@ -518,7 +520,7 @@ I can't seem to use os.read() on a pipe created with os.popen(); why?
518520
:func:`os.read` is a low-level function which takes a file descriptor, a small
519521
integer representing the opened file. :func:`os.popen` creates a high-level
520522
file object, the same type returned by the built-in :func:`open` function.
521-
Thus, to read n bytes from a pipe p created with :func:`os.popen`, you need to
523+
Thus, to read *n* bytes from a pipe *p* created with :func:`os.popen`, you need to
522524
use ``p.read(n)``.
523525

524526

@@ -538,8 +540,8 @@ use ``p.read(n)``.
538540
Warning: in general it is unwise to do this because you can easily cause a
539541
deadlock where your process is blocked waiting for output from the child
540542
while the child is blocked waiting for input from you. This can be caused
541-
because the parent expects the child to output more text than it does, or it
542-
can be caused by data being stuck in stdio buffers due to lack of flushing.
543+
by the parent expecting the child to output more text than it does or
544+
by data being stuck in stdio buffers due to lack of flushing.
543545
The Python parent can of course explicitly flush the data it sends to the
544546
child before it reads any output, but if the child is a naive C program it
545547
may have been written to never explicitly flush its output, even if it is
@@ -561,7 +563,7 @@ use ``p.read(n)``.
561563
get the result back. Unless the amount of data is very large, the easiest
562564
way to do this is to write it to a temporary file and run the command with
563565
that temporary file as input. The standard module :mod:`tempfile` exports a
564-
``mktemp()`` function to generate unique temporary file names. ::
566+
:func:`~tempfile.mktemp` function to generate unique temporary file names. ::
565567
566568
import tempfile
567569
import os
@@ -681,8 +683,8 @@ Yes. Here's a simple example that uses urllib.request::
681683
msg, hdrs = req.read(), req.info()
682684

683685
Note that in general for percent-encoded POST operations, query strings must be
684-
quoted using :func:`urllib.parse.urlencode`. For example to send name="Guy Steele,
685-
Jr."::
686+
quoted using :func:`urllib.parse.urlencode`. For example, to send
687+
``name=Guy Steele, Jr.``::
686688

687689
>>> import urllib.parse
688690
>>> urllib.parse.urlencode({'name': 'Guy Steele, Jr.'})
@@ -696,19 +698,8 @@ What module should I use to help with generating HTML?
696698

697699
.. XXX add modern template languages
698700
699-
There are many different modules available:
700-
701-
* HTMLgen is a class library of objects corresponding to all the HTML 3.2 markup
702-
tags. It's used when you are writing in Python and wish to synthesize HTML
703-
pages for generating a web or for CGI forms, etc.
704-
705-
* DocumentTemplate and Zope Page Templates are two different systems that are
706-
part of Zope.
707-
708-
* Quixote's PTL uses Python syntax to assemble strings of text.
709-
710-
Consult the `Web Programming wiki pages
711-
<http://wiki.python.org/moin/WebProgramming>`_ for more links.
701+
You can find a collection of useful links on the `Web Programming wiki page
702+
<http://wiki.python.org/moin/WebProgramming>`_.
712703

713704

714705
How do I send mail from a Python script?
@@ -737,7 +728,7 @@ work on any host that supports an SMTP listener. ::
737728
server.quit()
738729

739730
A Unix-only alternative uses sendmail. The location of the sendmail program
740-
varies between systems; sometimes it is ``/usr/lib/sendmail``, sometime
731+
varies between systems; sometimes it is ``/usr/lib/sendmail``, sometimes
741732
``/usr/sbin/sendmail``. The sendmail manual page will help you out. Here's
742733
some sample code::
743734

0 commit comments

Comments
 (0)