summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/stdlib.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/tutorial/stdlib.rst')
-rw-r--r--Doc/tutorial/stdlib.rst90
1 files changed, 27 insertions, 63 deletions
diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst
index a52653b..4dab074 100644
--- a/Doc/tutorial/stdlib.rst
+++ b/Doc/tutorial/stdlib.rst
@@ -15,7 +15,7 @@ operating system::
>>> import os
>>> os.getcwd() # Return the current working directory
- 'C:\\Python39'
+ 'C:\\Python26'
>>> os.chdir('/server/accesslogs') # Change current working directory
>>> os.system('mkdir today') # Run the command mkdir in the system shell
0
@@ -40,9 +40,7 @@ a higher level interface that is easier to use::
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
- 'archive.db'
>>> shutil.move('/build/executables', 'installdir')
- 'installdir'
.. _tut-file-wildcards:
@@ -69,25 +67,12 @@ instance the following output results from running ``python demo.py one two
three`` at the command line::
>>> import sys
- >>> print(sys.argv)
+ >>> print sys.argv
['demo.py', 'one', 'two', 'three']
-The :mod:`argparse` module provides a more sophisticated mechanism to process
-command line arguments. The following script extracts one or more filenames
-and an optional number of lines to be displayed::
-
- import argparse
-
- parser = argparse.ArgumentParser(prog = 'top',
- description = 'Show top lines from each file')
- parser.add_argument('filenames', nargs='+')
- parser.add_argument('-l', '--lines', type=int, default=10)
- args = parser.parse_args()
- print(args)
-
-When run at the command line with ``python top.py --lines=5 alpha.txt
-beta.txt``, the script sets ``args.lines`` to ``5`` and ``args.filenames``
-to ``['alpha.txt', 'beta.txt']``.
+The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix
+:func:`getopt` function. More powerful and flexible command line processing is
+provided by the :mod:`argparse` module.
.. _tut-stderr:
@@ -136,7 +121,7 @@ The :mod:`math` module gives access to the underlying C library functions for
floating point math::
>>> import math
- >>> math.cos(math.pi / 4)
+ >>> math.cos(math.pi / 4.0)
0.70710678118654757
>>> math.log(1024, 2)
10.0
@@ -146,27 +131,13 @@ The :mod:`random` module provides tools for making random selections::
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
- >>> random.sample(range(100), 10) # sampling without replacement
+ >>> random.sample(xrange(100), 10) # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # random float
0.17970987693706186
>>> random.randrange(6) # random integer chosen from range(6)
4
-The :mod:`statistics` module calculates basic statistical properties
-(the mean, median, variance, etc.) of numeric data::
-
- >>> import statistics
- >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
- >>> statistics.mean(data)
- 1.6071428571428572
- >>> statistics.median(data)
- 1.25
- >>> statistics.variance(data)
- 1.3720238095238095
-
-The SciPy project <https://scipy.org> has many other modules for numerical
-computations.
.. _tut-internet-access:
@@ -174,15 +145,13 @@ Internet Access
===============
There are a number of modules for accessing the internet and processing internet
-protocols. Two of the simplest are :mod:`urllib.request` for retrieving data
-from URLs and :mod:`smtplib` for sending mail::
+protocols. Two of the simplest are :mod:`urllib2` for retrieving data from URLs
+and :mod:`smtplib` for sending mail::
- >>> from urllib.request import urlopen
- >>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
- ... for line in response:
- ... line = line.decode('utf-8') # Decoding the binary data to text.
- ... if 'EST' in line or 'EDT' in line: # look for Eastern Time
- ... print(line)
+ >>> import urllib2
+ >>> for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
+ ... if 'EST' in line or 'EDT' in line: # look for Eastern Time
+ ... print line
<BR>Nov. 25, 09:43:32 PM EST
@@ -231,18 +200,18 @@ Data Compression
================
Common data archiving and compression formats are directly supported by modules
-including: :mod:`zlib`, :mod:`gzip`, :mod:`bz2`, :mod:`lzma`, :mod:`zipfile` and
+including: :mod:`zlib`, :mod:`gzip`, :mod:`bz2`, :mod:`zipfile` and
:mod:`tarfile`. ::
>>> import zlib
- >>> s = b'witch which has which witches wrist watch'
+ >>> s = 'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
- b'witch which has which witches wrist watch'
+ 'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979
@@ -290,10 +259,10 @@ documentation::
def average(values):
"""Computes the arithmetic mean of a list of numbers.
- >>> print(average([20, 30, 70]))
+ >>> print average([20, 30, 70])
40.0
"""
- return sum(values) / len(values)
+ return sum(values, 0.0) / len(values)
import doctest
doctest.testmod() # automatically validate the embedded tests
@@ -325,29 +294,24 @@ Batteries Included
Python has a "batteries included" philosophy. This is best seen through the
sophisticated and robust capabilities of its larger packages. For example:
-* The :mod:`xmlrpc.client` and :mod:`xmlrpc.server` modules make implementing
+* The :mod:`xmlrpclib` and :mod:`SimpleXMLRPCServer` modules make implementing
remote procedure calls into an almost trivial task. Despite the modules
names, no direct knowledge or handling of XML is needed.
* The :mod:`email` package is a library for managing email messages, including
- MIME and other :rfc:`2822`-based message documents. Unlike :mod:`smtplib` and
+ MIME and other RFC 2822-based message documents. Unlike :mod:`smtplib` and
:mod:`poplib` which actually send and receive messages, the email package has
a complete toolset for building or decoding complex message structures
(including attachments) and for implementing internet encoding and header
protocols.
-* The :mod:`json` package provides robust support for parsing this
- popular data interchange format. The :mod:`csv` module supports
- direct reading and writing of files in Comma-Separated Value format,
- commonly supported by databases and spreadsheets. XML processing is
- supported by the :mod:`xml.etree.ElementTree`, :mod:`xml.dom` and
- :mod:`xml.sax` packages. Together, these modules and packages
- greatly simplify data interchange between Python applications and
- other tools.
-
-* The :mod:`sqlite3` module is a wrapper for the SQLite database
- library, providing a persistent database that can be updated and
- accessed using slightly nonstandard SQL syntax.
+* The :mod:`xml.dom` and :mod:`xml.sax` packages provide robust support for
+ parsing this popular data interchange format. Likewise, the :mod:`csv` module
+ supports direct reads and writes in a common database format. Together, these
+ modules and packages greatly simplify data interchange between Python
+ applications and other tools.
* Internationalization is supported by a number of modules including
:mod:`gettext`, :mod:`locale`, and the :mod:`codecs` package.
+
+