annotate Doc/libarray.tex @ 6022:802470c7a50f legacy-trunk

Added buffer_info() docs.
author Guido van Rossum <guido@python.org>
date Thu, 14 Aug 1997 19:50:37 +0000
parents 3468ea8645ca
children d03f2e2fbf36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
1 \section{Built-in Module \sectcode{array}}
5818
3468ea8645ca AMK's megapatch:
Guido van Rossum <guido@python.org>
parents: 4877
diff changeset
2 \label{module-array}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 \bimodindex{array}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 \index{arrays}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 This module defines a new object type which can efficiently represent
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 an array of basic values: characters, integers, floating point
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 numbers. Arrays are sequence types and behave very much like lists,
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 except that the type of objects stored in them is constrained. The
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 type is specified at object creation time by using a \dfn{type code},
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 which is a single character. The following type codes are defined:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 \begin{tableiii}{|c|c|c|}{code}{Typecode}{Type}{Minimal size in bytes}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 \lineiii{'c'}{character}{1}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 \lineiii{'b'}{signed integer}{1}
4877
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
16 \lineiii{'B'}{unsigned integer}{1}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 \lineiii{'h'}{signed integer}{2}
4877
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
18 \lineiii{'H'}{unsigned integer}{2}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 \lineiii{'i'}{signed integer}{2}
4877
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
20 \lineiii{'I'}{unsigned integer}{2}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 \lineiii{'l'}{signed integer}{4}
4877
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
22 \lineiii{'L'}{unsigned integer}{4}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 \lineiii{'f'}{floating point}{4}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 \lineiii{'d'}{floating point}{8}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 \end{tableiii}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 The actual representation of values is determined by the machine
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
28 architecture (strictly speaking, by the C implementation). The actual
4877
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
29 size can be accessed through the \var{itemsize} attribute. The values
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
30 stored for \code{'L'} and \code{'I'} items will be represented as
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
31 Python long integers when retrieved, because Python's plain integer
e57a8de5aeb3 Added unsigned data formats (B, H, I, L).
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
32 type can't represent the full range of C's unsigned (long) integers.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
2609
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
34 See also built-in module \code{struct}.
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
35 \bimodindex{struct}
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
36
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 The module defines the following function:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 \renewcommand{\indexsubitem}{(in module array)}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
41 \begin{funcdesc}{array}{typecode\optional{\, initializer}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 Return a new array whose items are restricted by \var{typecode}, and
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 initialized from the optional \var{initializer} value, which must be a
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 list or a string. The list or string is passed to the new array's
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 \code{fromlist()} or \code{fromstring()} method (see below) to add
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 initial items to the array.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 Array objects support the following data items and methods:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 \begin{datadesc}{typecode}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 The typecode character used to create the array.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 \end{datadesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 \begin{datadesc}{itemsize}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 The length in bytes of one array item in the internal representation.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 \end{datadesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 \begin{funcdesc}{append}{x}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 Append a new item with value \var{x} to the end of the array.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
6022
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
63 \begin{funcdesc}{buffer_info}{}
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
64 Return a tuple \code{(\var{address}, var{length})} giving the current
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
65 memory address and the length in bytes of the buffer used to hold
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
66 array's contents. This is occasionally useful when working with
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
67 low-level (and inherently unsafe) I/O interfaces that require memory
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
68 addresses, such as certain \code{ioctl} operations. The returned
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
69 numbers are valid as long as the array exists and no length-changing
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
70 operations are applied to it.
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
71 \end{funcdesc}
802470c7a50f Added buffer_info() docs.
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
72
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 \begin{funcdesc}{byteswap}{x}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 ``Byteswap'' all items of the array. This is only supported for
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1471
diff changeset
75 integer values. It is useful when reading data from a file written
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 on a machine with a different byte order.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 \begin{funcdesc}{fromfile}{f\, n}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 Read \var{n} items (as machine values) from the file object \var{f}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 and append them to the end of the array. If less than \var{n} items
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 are available, \code{EOFError} is raised, but the items that were
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
83 available are still inserted into the array. \var{f} must be a real
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
84 built-in file object; something else with a \code{read()} method won't
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
85 do.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 \begin{funcdesc}{fromlist}{list}
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
89 Append items from the list. This is equivalent to
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
90 \code{for x in \var{list}:\ a.append(x)}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 except that if there is a type error, the array is unchanged.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 \begin{funcdesc}{fromstring}{s}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 Appends items from the string, interpreting the string as an
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 array of machine values (i.e. as if it had been read from a
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 file using the \code{fromfile()} method).
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 \begin{funcdesc}{insert}{i\, x}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 Insert a new item with value \var{x} in the array before position
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 \var{i}.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 \begin{funcdesc}{tofile}{f}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 Write all items (as machine values) to the file object \var{f}.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 \begin{funcdesc}{tolist}{}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 Convert the array to an ordinary list with the same items.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 \begin{funcdesc}{tostring}{}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 Convert the array to an array of machine values and return the
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 string representation (the same sequence of bytes that would
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 be written to a file by the \code{tofile()} method.)
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 When an array object is printed or converted to a string, it is
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 represented as \code{array(\var{typecode}, \var{initializer})}. The
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 \var{initializer} is omitted if the array is empty, otherwise it is a
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 string if the \var{typecode} is \code{'c'}, otherwise it is a list of
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 numbers. The string is guaranteed to be able to be converted back to
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 an array with the same type and value using reverse quotes
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 (\code{``}). Examples:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 \bcode\begin{verbatim}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 array('l')
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 array('c', 'hello world')
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 array('l', [1, 2, 3, 4, 5])
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 array('d', [1.0, 2.0, 3.14])
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 \end{verbatim}\ecode