Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1 | % Format this file with latex. |
| 2 | |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 3 | %\documentstyle[garamond,11pt,myformat]{article} |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 4 | \documentstyle[11pt,myformat]{article} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 6 | % A command to force the text after an item to start on a new line |
| 7 | \newcommand{\itembreak}{ |
| 8 | \mbox{}\\*[0mm] |
| 9 | } |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 11 | % A command to define a function item |
| 12 | \newcommand{\funcitem}[2]{\item[#1(#2)]} |
| 13 | |
| 14 | % A command to define an exception item |
| 15 | \newcommand{\excitem}[2]{ |
| 16 | \item[#1 = {\tt '#2'}] |
| 17 | \itembreak |
| 18 | } |
| 19 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 20 | \title{\bf |
| 21 | Python Library Reference \\ |
| 22 | (DRAFT) |
| 23 | } |
| 24 | |
| 25 | \author{ |
| 26 | Guido van Rossum \\ |
| 27 | Dept. CST, CWI, Kruislaan 413 \\ |
| 28 | 1098 SJ Amsterdam, The Netherlands \\ |
| 29 | E-mail: {\tt guido@cwi.nl} |
| 30 | } |
| 31 | |
| 32 | \begin{document} |
| 33 | |
| 34 | \pagenumbering{roman} |
| 35 | |
| 36 | \maketitle |
| 37 | |
| 38 | \begin{abstract} |
| 39 | |
| 40 | \noindent |
| 41 | This document describes the built-in types, exceptions and functions and |
| 42 | the standard modules that come with the {\Python} system. |
| 43 | It assumes basic knowledge about the {\Python} language. |
| 44 | For an informal introduction to the language, see the Tutorial document. |
| 45 | The Language Reference document (XXX not yet existing) |
| 46 | gives a more formal reference to the language. |
| 47 | |
| 48 | \end{abstract} |
| 49 | |
| 50 | \pagebreak |
| 51 | |
| 52 | \tableofcontents |
| 53 | |
| 54 | \pagebreak |
| 55 | |
| 56 | \pagenumbering{arabic} |
| 57 | |
| 58 | \section{Introduction} |
| 59 | |
| 60 | The {\Python} library consists of three parts, with different levels of |
| 61 | integration with the interpreter. |
| 62 | Closest to the interpreter are built-in types, exceptions and functions. |
| 63 | Next are built-in modules, which are written in C and linked statically |
| 64 | with the interpreter. |
| 65 | Finally there are standard modules that are implemented entirely in |
| 66 | {\Python}, but are always available. |
| 67 | For efficiency, some standard modules may become built-in modules in |
| 68 | future versions of the interpreter. |
| 69 | |
| 70 | \section{Built-in Types, Exceptions and Functions} |
| 71 | |
| 72 | Names for built-in exceptions and functions are found in a separate |
| 73 | read-only symbol table which cannot be modified. |
| 74 | This table is searched last, so local and global user-defined names can |
| 75 | override built-in names. |
| 76 | Built-in types have no names but are created by syntactic constructs |
| 77 | (such as constants) or built-in functions. |
| 78 | They are described together here for easy reference.% |
| 79 | \footnote{ |
| 80 | The descriptions sorely lack explanations of the exceptions that |
| 81 | may be raised---this will be fixed in a future version of this |
| 82 | document. |
| 83 | } |
| 84 | |
| 85 | \subsection{Built-in Types} |
| 86 | |
| 87 | The following sections describe the standard types that are built into the |
| 88 | interpreter. |
| 89 | \subsubsection{Numeric Types} |
| 90 | |
| 91 | There are two numeric types: integers and floating point numbers. |
| 92 | Integers are implemented using {\tt long} in C, so they have at least 32 |
| 93 | bits of precision. |
| 94 | Floating point numbers are implemented using {\tt double} in C. |
| 95 | All bets on precision are off. |
| 96 | Numbers are created by numeric constants or as the result of built-in |
| 97 | functions and operators. |
| 98 | |
| 99 | Numeric types support the following operations: |
| 100 | |
| 101 | \begin{center} |
| 102 | \begin{tabular}{|c|l|c|} |
| 103 | \hline |
| 104 | Operation & Result & Notes \\ |
| 105 | \hline |
| 106 | {\tt abs}({\em x}) & absolute value of {\em x} & \\ |
| 107 | {\tt int}({\em x}) & {\em x} converted to integer & (1) \\ |
| 108 | {\tt float}({\em x}) & {\em x} converted to floating point & \\ |
| 109 | {\tt -}{\em x} & {\em x} negated & \\ |
| 110 | {\tt +}{\em x} & {\em x} unchanged & \\ |
| 111 | {\em x}{\tt +}{\em y} & sum of {\em x} and {\em y} & \\ |
| 112 | {\em x}{\tt -}{\em y} & difference of {\em x} and {\em y} & \\ |
| 113 | {\em x}{\tt *}{\em y} & product of {\em x} and {\em y} & \\ |
| 114 | {\em x}{\tt /}{\em y} & quotient of {\em x} and {\em y} & (2) \\ |
| 115 | {\em x}{\tt \%}{\em y} & remainder of {\em x}{\tt /}{\em y} & (3) \\ |
| 116 | \hline |
| 117 | \end{tabular} |
| 118 | \end{center} |
| 119 | |
| 120 | \noindent |
| 121 | Notes: |
| 122 | \begin{description} |
| 123 | \item[(1)] |
| 124 | This may round or truncate as in C; see functions {\tt floor} and |
| 125 | {\tt ceil} in module {\tt math}. |
| 126 | \item[(2)] |
| 127 | Integer division is defined as in C: the result is an integer; with |
| 128 | positive operands, it truncates towards zero; with a negative operand, |
| 129 | the result is unspecified. |
| 130 | \item[(3)] |
| 131 | Only defined for integers. |
| 132 | \end{description} |
| 133 | |
| 134 | Mixed arithmetic is not supported; both operands must have the same type. |
| 135 | Mixed comparisons return the wrong result (floats always compare smaller |
| 136 | than integers).% |
| 137 | \footnote{ |
| 138 | These restrictions are bugs in the language definitions and will be |
| 139 | fixed in the future. |
| 140 | } |
| 141 | \subsubsection{Sequence Types} |
| 142 | |
| 143 | There are three sequence types: strings, lists and tuples. |
| 144 | Strings constants are written in single quotes: {\tt 'xyzzy'}. |
| 145 | Lists are constructed with square brackets: {\tt [a,~b,~c]}. |
| 146 | Tuples are constructed by the comma operator or with an empty set of |
| 147 | parentheses: {\tt a,~b,~c} or {\tt ()}. |
| 148 | |
| 149 | Sequence types support the following operations ({\em s} and {\em t} are |
| 150 | sequences of the same type; {\em n}, {\em i} and {\em j} are integers): |
| 151 | |
| 152 | \begin{center} |
| 153 | \begin{tabular}{|c|l|c|} |
| 154 | \hline |
| 155 | Operation & Result & Notes \\ |
| 156 | \hline |
| 157 | {\tt len}({\em s}) & length of {\em s} & \\ |
| 158 | {\tt min}({\em s}) & smallest item of {\em s} & \\ |
| 159 | {\tt max}({\em s}) & largest item of {\em s} & \\ |
| 160 | {\em x} {\tt in} {\em s} & |
| 161 | true if an item of {\em s} is equal to {\em x} & \\ |
| 162 | {\em x} {\tt not} {\tt in} {\em s} & |
| 163 | false if an item of {\em s} is equal to {\em x} & \\ |
| 164 | {\em s}{\tt +}{\em t} & the concatenation of {\em s} and {\em t} & \\ |
| 165 | {\em s}{\tt *}{\em n}, {\em n}*{\em s} & |
| 166 | {\em n} copies of {\em s} concatenated & (1) \\ |
| 167 | {\em s}[{\em i}] & {\em i}'th item of {\em s} & \\ |
| 168 | {\em s}[{\em i}:{\em j}] & |
| 169 | slice of {\em s} from {\em i} to {\em j} & (2) \\ |
| 170 | \hline |
| 171 | \end{tabular} |
| 172 | \end{center} |
| 173 | |
| 174 | \noindent |
| 175 | Notes: |
| 176 | \begin{description} |
| 177 | \item[(1)] |
| 178 | Sequence repetition is only supported for strings. |
| 179 | \item[(2)] |
| 180 | The slice of $s$ from $i$ to $j$ is defined as the sequence |
| 181 | of items with index $k$ such that $i \leq k < j$. |
| 182 | Special rules apply for negative and omitted indices; see the Tutorial |
| 183 | or the Reference Manual. |
| 184 | \end{description} |
| 185 | |
| 186 | \paragraph{Mutable Sequence Types.} |
| 187 | |
| 188 | List objects support additional operations that allow in-place |
| 189 | modification of the object. |
| 190 | These operations would be supported by other mutable sequence types |
| 191 | (when added to the language) as well. |
| 192 | Strings and tuples are immutable sequence types and such objects cannot |
| 193 | be modified once created. |
| 194 | The following operations are defined on mutable sequence types (where |
| 195 | {\em x} is an arbitrary object): |
| 196 | |
| 197 | \begin{center} |
| 198 | \begin{tabular}{|c|l|} |
| 199 | \hline |
| 200 | Operation & Result \\ |
| 201 | \hline |
| 202 | {\em s}[{\em i}] = {\em x} & |
| 203 | item {\em i} of {\em s} is replaced by {\em x} \\ |
| 204 | {\em s}[{\em i}:{\em j}] = {\em t} & |
| 205 | slice of {\em s} from {\em i} to {\em j} is replaced by {\em t} \\ |
| 206 | {\tt del} {\em s}[{\em i}:{\em j}] & |
| 207 | same as {\em s}[{\em i}:{\em j}] = [] \\ |
| 208 | {\em s}.{\tt append}({\em x}) & |
| 209 | same as {\em s}[{\tt len}({\em x}):{\tt len}({\em x})] = [{\em x}] \\ |
| 210 | {\em s}.{\tt insert}({\em i}, {\em x}) & |
| 211 | same as {\em s}[{\em i}:{\em i}] = [{\em x}] \\ |
| 212 | {\em s}.{\tt sort}() & |
| 213 | the items of {\em s} are permuted to satisfy \\ |
| 214 | & |
| 215 | $s[i] \leq s[j]$ for $i < j$\\ |
| 216 | \hline |
| 217 | \end{tabular} |
| 218 | \end{center} |
| 219 | |
| 220 | \subsubsection{Mapping Types} |
| 221 | |
| 222 | A |
| 223 | {\em mapping} |
| 224 | object maps values of one type (the key type) to arbitrary objects. |
| 225 | Mappings are mutable objects. |
| 226 | There is currently only one mapping type, the |
| 227 | {\em dictionary}. |
| 228 | A dictionary's keys are strings. |
| 229 | An empty dictionary is created by the expression \verb"{}". |
| 230 | An extension of this notation is used to display dictionaries when |
| 231 | written (see the example below). |
| 232 | |
| 233 | The following operations are defined on mappings (where {\em a} is a |
| 234 | mapping, {\em k} is a key and {\em x} is an arbitrary object): |
| 235 | |
| 236 | \begin{center} |
| 237 | \begin{tabular}{|c|l|c|} |
| 238 | \hline |
| 239 | Operation & Result & Notes\\ |
| 240 | \hline |
| 241 | {\tt len}({\em a}) & the number of elements in {\em a} & \\ |
| 242 | {\em a}[{\em k}] & the item of {\em a} with key {\em k} & \\ |
| 243 | {\em a}[{\em k}] = {\em x} & set {\em a}[{\em k}] to {\em x} & \\ |
| 244 | {\tt del} {\em a}[{\em k}] & remove {\em a}[{\em k}] from {\em a} & \\ |
| 245 | {\em a}.{\tt keys}() & a copy of {\em a}'s list of keys & (1) \\ |
| 246 | {\em a}.{\tt has\_key}({\em k}) & true if {\em a} has a key {\em k} & \\ |
| 247 | \hline |
| 248 | \end{tabular} |
| 249 | \end{center} |
| 250 | |
| 251 | \noindent |
| 252 | Notes: |
| 253 | \begin{description} |
| 254 | \item[(1)] |
| 255 | Keys are listed in random order. |
| 256 | \end{description} |
| 257 | |
| 258 | A small example using a dictionary: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 259 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 260 | >>> tel = {} |
| 261 | >>> tel['jack'] = 4098 |
| 262 | >>> tel['sape'] = 4139 |
| 263 | >>> tel['guido'] = 4127 |
| 264 | >>> tel['jack'] |
| 265 | 4098 |
| 266 | >>> tel |
| 267 | {'sape': 4139; 'guido': 4127; 'jack': 4098} |
| 268 | >>> del tel['sape'] |
| 269 | >>> tel['irv'] = 4127 |
| 270 | >>> tel |
| 271 | {'guido': 4127; 'irv': 4127; 'jack': 4098} |
| 272 | >>> tel.keys() |
| 273 | ['guido', 'irv', 'jack'] |
| 274 | >>> tel.has_key('guido') |
| 275 | 1 |
| 276 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 277 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 278 | \subsubsection{Other Built-in Types} |
| 279 | |
| 280 | The interpreter supports several other kinds of objects. |
| 281 | Most of these support only one or two operations. |
| 282 | |
| 283 | \paragraph{Modules.} |
| 284 | |
| 285 | The only operation on a module is member acces: {\em m}{\tt .}{\em name}, |
| 286 | where {\em m} is a module and {\em name} accesses a name defined in |
| 287 | {\em m}'s symbol table. |
| 288 | Module members can be assigned to. |
| 289 | |
| 290 | \paragraph{Classes and Class Objects.} |
| 291 | |
| 292 | XXX Classes will be explained at length in a later version of this |
| 293 | document. |
| 294 | |
| 295 | \paragraph{Functions.} |
| 296 | |
| 297 | Function objects are created by function definitions. |
| 298 | The only operation on a function object is to call it: |
| 299 | {\em func}({\em optional-arguments}). |
| 300 | |
| 301 | Built-in functions have a different type than user-defined functions, |
| 302 | but they support the same operation. |
| 303 | |
| 304 | \paragraph{Methods.} |
| 305 | |
| 306 | Methods are functions that are called using the member acces notation. |
| 307 | There are two flavors: built-in methods (such as {\tt append()} on |
| 308 | lists) and class member methods. |
| 309 | Built-in methods are described with the types that support them. |
| 310 | XXX Class member methods will be described in a later version of this |
| 311 | document. |
| 312 | |
| 313 | \paragraph{Type Objects.} |
| 314 | |
| 315 | Type objects represent the various object types. |
| 316 | An object's type is accessed by the built-in function |
| 317 | {\tt type()}. |
| 318 | There are no operations on type objects. |
| 319 | |
| 320 | \paragraph{The Null Object.} |
| 321 | |
| 322 | This object is returned by functions that don't explicitly return a |
| 323 | value. |
| 324 | It supports no operations. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 325 | There is exactly one null object, named {\tt None} |
| 326 | (a built-in name). |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 327 | |
| 328 | \paragraph{File Objects.} |
| 329 | |
| 330 | File objects are implemented using C's |
| 331 | {\em stdio} |
| 332 | package and can be created with the built-in function |
| 333 | {\tt open()}. |
| 334 | They have the following methods: |
| 335 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 336 | \funcitem{close}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 337 | Closes the file. |
| 338 | A closed file cannot be read or written anymore. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 339 | \funcitem{read}{size} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 340 | Reads at most |
| 341 | {\tt size} |
| 342 | bytes from the file (less if the read hits EOF). |
| 343 | The bytes are returned as a string object. |
| 344 | An empty string is returned when EOF is hit immediately. |
| 345 | (For certain files, like ttys, it makes sense to continue reading after |
| 346 | an EOF is hit.) |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 347 | \funcitem{readline}{size} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 348 | Reads a line of at most |
| 349 | {\tt size} |
| 350 | bytes from the file. |
| 351 | A trailing newline character, if present, is kept in the string. |
| 352 | The size is optional and defaults to a large number (but not infinity). |
| 353 | EOF is reported as by |
| 354 | {\tt read().} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 355 | \funcitem{write}{str} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 356 | Writes a string to the file. |
| 357 | Returns no value. |
| 358 | \end{description} |
| 359 | |
| 360 | \subsection{Built-in Exceptions} |
| 361 | |
| 362 | The following exceptions can be generated by the interpreter or |
| 363 | built-in functions. |
| 364 | Except where mentioned, they have a string argument (also known as the |
| 365 | `associated value' of an exception) indicating the detailed cause of the |
| 366 | error. |
| 367 | The strings listed with the exception names are their values when used |
| 368 | in an expression or printed. |
| 369 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 370 | \excitem{EOFError}{end-of-file read} |
| 371 | (No argument.) |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 372 | Raised when a built-in function ({\tt input()} or {\tt raw\_input()}) |
| 373 | hits an end-of-file condition (EOF) without reading any data. |
| 374 | (N.B.: the {\tt read()} and {\tt readline()} methods of file objects |
| 375 | return an empty string when they hit EOF.) |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 376 | \excitem{KeyboardInterrupt}{end-of-file read} |
| 377 | (No argument.) |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 378 | Raised when the user hits the interrupt key (normally Control-C or DEL). |
| 379 | During execution, a check for interrupts is made regularly. |
| 380 | Interrupts typed when a built-in function ({\tt input()} or |
| 381 | {\tt raw\_input()}) is waiting for input also raise this exception. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 382 | \excitem{MemoryError}{out of memory} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 383 | %.br |
| 384 | Raised when an operation runs out of memory but the situation |
| 385 | may still be rescued (by deleting some objects). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 386 | \excitem{NameError}{undefined name} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 387 | %.br |
| 388 | Raised when a name is not found. |
| 389 | This applies to unqualified names, module names (on {\tt import}), |
| 390 | module members and object methods. |
| 391 | The string argument is the name that could not be found. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 392 | \excitem{RuntimeError}{run-time error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 393 | %.br |
| 394 | Raised for a variety of reasons, e.g., division by zero or index out of |
| 395 | range. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 396 | \excitem{SystemError}{system error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 397 | %.br |
| 398 | Raised when the interpreter finds an internal error, but the situation |
| 399 | does not look so serious to cause it to abandon all hope. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 400 | \excitem{TypeError}{type error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 401 | %.br |
| 402 | Raised when an operation or built-in function is applied to an object of |
| 403 | inappropriate type. |
| 404 | \end{description} |
| 405 | |
| 406 | \subsection{Built-in Functions} |
| 407 | |
| 408 | The {\Python} interpreter has a small number of functions built into it that |
| 409 | are always available. |
| 410 | They are listed here in alphabetical order. |
| 411 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 412 | \funcitem{abs}{x} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 413 | Returns the absolute value of a number. |
| 414 | The argument may be an integer or floating point number. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 415 | \funcitem{chr}{i} |
| 416 | Returns a string of one character |
| 417 | whose ASCII code is the integer {\tt i}, |
| 418 | e.g., {\tt chr(97)} returns the string {\tt 'a'}. |
| 419 | This is the inverse of {\tt ord()}. |
| 420 | \funcitem{dir}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 421 | Without arguments, this function returns the list of names in the |
| 422 | current local symbol table, sorted alphabetically. |
| 423 | With a module object as argument, it returns the sorted list of names in |
| 424 | that module's global symbol table. |
| 425 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 426 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 427 | >>> import sys |
| 428 | >>> dir() |
| 429 | ['sys'] |
| 430 | >>> dir(sys) |
| 431 | ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout'] |
| 432 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 433 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 434 | \funcitem{divmod}{a, b} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 435 | %.br |
| 436 | Takes two integers as arguments and returns a pair of integers |
| 437 | consisting of their quotient and remainder. |
| 438 | For |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 439 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 440 | q, r = divmod(a, b) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 441 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 442 | the invariants are: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 443 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 444 | a = q*b + r |
| 445 | abs(r) < abs(b) |
| 446 | r has the same sign as b |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 447 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 448 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 449 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 450 | >>> divmod(100, 7) |
| 451 | (14, 2) |
| 452 | >>> divmod(-100, 7) |
| 453 | (-15, 5) |
| 454 | >>> divmod(100, -7) |
| 455 | (-15, -5) |
| 456 | >>> divmod(-100, -7) |
| 457 | (14, -2) |
| 458 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 459 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 460 | \funcitem{eval}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 461 | Takes a string as argument and parses and evaluates it as a {\Python} |
| 462 | expression. |
| 463 | The expression is executed using the current local and global symbol |
| 464 | tables. |
| 465 | Syntax errors are reported as exceptions. |
| 466 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 467 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 468 | >>> x = 1 |
| 469 | >>> eval('x+1') |
| 470 | 2 |
| 471 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 472 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 473 | \funcitem{exec}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 474 | Takes a string as argument and parses and evaluates it as a sequence of |
| 475 | {\Python} statements. |
| 476 | The string should end with a newline (\verb"'\n'"). |
| 477 | The statement is executed using the current local and global symbol |
| 478 | tables. |
| 479 | Syntax errors are reported as exceptions. |
| 480 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 481 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 482 | >>> x = 1 |
| 483 | >>> exec('x = x+1\n') |
| 484 | >>> x |
| 485 | 2 |
| 486 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 487 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 488 | \funcitem{float}{x} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 489 | Converts a number to floating point. |
| 490 | The argument may be an integer or floating point number. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 491 | \funcitem{input}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 492 | Equivalent to |
| 493 | {\tt eval(raw\_input(s))}. |
| 494 | As for |
| 495 | {\tt raw\_input()}, |
| 496 | the argument is optional. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 497 | \funcitem{int}{x} |
| 498 | Converts a number to integer. |
| 499 | The argument may be an integer or floating point number. |
| 500 | \funcitem{len}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 501 | Returns the length (the number of items) of an object. |
| 502 | The argument may be a sequence (string, tuple or list) or a mapping |
| 503 | (dictionary). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 504 | \funcitem{max}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 505 | Returns the largest item of a non-empty sequence (string, tuple or list). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 506 | \funcitem{min}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 507 | Returns the smallest item of a non-empty sequence (string, tuple or list). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 508 | \funcitem{open}{name, mode} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 509 | %.br |
| 510 | Returns a file object (described earlier under Built-in Types). |
| 511 | The string arguments are the same as for stdio's |
| 512 | {\tt fopen()}: |
| 513 | {\tt 'r'} |
| 514 | opens the file for reading, |
| 515 | {\tt 'w'} |
| 516 | opens it for writing (truncating an existing file), |
| 517 | {\tt 'a'} |
| 518 | opens it for appending.% |
| 519 | \footnote{ |
| 520 | This function should go into a built-in module |
| 521 | {\tt io}. |
| 522 | } |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 523 | \funcitem{ord}{c} |
| 524 | Takes a string of one character and returns its |
| 525 | ASCII value, e.g., {\tt ord('a')} returns the integer {\tt 97}. |
| 526 | This is the inverse of {\tt chr()}. |
| 527 | \funcitem{range}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 528 | This is a versatile function to create lists containing arithmetic |
| 529 | progressions of integers. |
| 530 | With two integer arguments, it returns the ascending sequence of |
| 531 | integers starting at the first and ending one before the second |
| 532 | argument. |
| 533 | A single argument is used as the end point of the sequence, with 0 used |
| 534 | as the starting point. |
| 535 | A third argument specifies the step size; negative steps are allowed and |
| 536 | work as expected, but don't specify a zero step. |
| 537 | The resulting list may be empty. |
| 538 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 539 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 540 | >>> range(10) |
| 541 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 542 | >>> range(1, 1+10) |
| 543 | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 544 | >>> range(0, 30, 5) |
| 545 | [0, 5, 10, 15, 20, 25] |
| 546 | >>> range(0, 10, 3) |
| 547 | [0, 3, 6, 9] |
| 548 | >>> range(0, -10, -1) |
| 549 | [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] |
| 550 | >>> range(0) |
| 551 | [] |
| 552 | >>> range(1, 0) |
| 553 | [] |
| 554 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 555 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 556 | \funcitem{raw\_input}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 557 | %.br |
| 558 | The argument is optional; if present, it is written to standard output |
| 559 | without a trailing newline. |
| 560 | The function then reads a line from input, converts it to a string |
| 561 | (stripping a trailing newline), and returns that. |
| 562 | EOF is reported as an exception. |
| 563 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 564 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 565 | >>> raw_input('Type anything: ') |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 566 | Type anything: Mutant Teenage Ninja Turtles |
| 567 | 'Mutant Teenage Ninja Turtles' |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 568 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 569 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 570 | \funcitem{reload}{module} |
| 571 | Causes an already imported module to be re-parsed and re-initialized. |
| 572 | This is useful if you have edited the module source file and want to |
| 573 | try out the new version without leaving {\Python}. |
| 574 | \funcitem{type}{x} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 575 | Returns the type of an object. |
| 576 | Types are objects themselves: |
| 577 | the type of a type object is its own type. |
| 578 | \end{description} |
| 579 | |
| 580 | \section{Built-in Modules} |
| 581 | |
| 582 | The modules described in this section are built into the interpreter. |
| 583 | They must be imported using |
| 584 | {\tt import}. |
| 585 | Some modules are not always available; it is a configuration option to |
| 586 | provide them. |
| 587 | Details are listed with the descriptions, but the best way to see if |
| 588 | a module exists in a particular implementation is to attempt to import |
| 589 | it. |
| 590 | |
| 591 | \subsection{Built-in Module {\tt sys}} |
| 592 | |
| 593 | This module provides access to some variables used or maintained by the |
| 594 | interpreter and to functions that interact strongly with the interpreter. |
| 595 | It is always available. |
| 596 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 597 | \funcitem{argv} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 598 | The list of command line arguments passed to a {\Python} script. |
| 599 | {\tt sys.argv[0]} |
| 600 | is the script name. |
| 601 | If no script name was passed to the {\Python} interpreter, |
| 602 | {\tt sys.argv} |
| 603 | is empty. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 604 | \funcitem{exit}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 605 | Exits from {\Python} with numeric exit status |
| 606 | {\tt n}. |
| 607 | This closes all open files and performs other cleanup-actions required by |
| 608 | the interpreter (but |
| 609 | {\em finally clauses} |
| 610 | of |
| 611 | {\tt try} |
| 612 | statements are not executed!). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 613 | \funcitem{modules} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 614 | Gives the list of modules that have already been loaded. |
| 615 | This can be manipulated to force reloading of modules and other tricks. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 616 | \funcitem{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 617 | A list of strings that specifies the search path for modules. |
| 618 | Initialized from the environment variable {\tt PYTHONPATH}, or an |
| 619 | installation-dependent default. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 620 | \funcitem{ps1,~ps2} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 621 | Strings specifying the primary and secondary prompt of the interpreter. |
| 622 | These are only defined if the interpreter is in interactive mode. |
| 623 | Their initial values in this case are |
| 624 | {\tt '>>> '} |
| 625 | and |
| 626 | {\tt '... '}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 627 | \funcitem{stdin, stdout, stderr} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 628 | %.br |
| 629 | File objects corresponding to the interpreter's standard input, output |
| 630 | and error streams. |
| 631 | {\tt sys.stdin} |
| 632 | is used for all interpreter input except for scripts but including calls |
| 633 | to |
| 634 | {\tt input()} |
| 635 | and |
| 636 | {\tt raw\_input()}. |
| 637 | {\tt sys.stdout} |
| 638 | is used for the output of |
| 639 | {\tt print} and expression statements |
| 640 | and for the prompts of |
| 641 | {\tt input()} |
| 642 | and |
| 643 | {\tt raw\_input()}. |
| 644 | The interpreter's own prompts and its error messages are written to |
| 645 | stderr. |
| 646 | Assigning to |
| 647 | {\tt sys.stderr} |
| 648 | has no effect on the interpreter; it can be used to write error messages |
| 649 | to stderr using |
| 650 | {\tt print}. |
| 651 | \end{description} |
| 652 | |
| 653 | \subsection{Built-in Module {\tt math}} |
| 654 | |
| 655 | This module is always available. |
| 656 | It provides access to the mathematical functions defined by the C |
| 657 | standard. |
| 658 | They are: |
| 659 | {\tt acos(x)}, |
| 660 | {\tt asin(x)}, |
| 661 | {\tt atan(x)}, |
| 662 | {\tt atan2(x,y)}, |
| 663 | {\tt ceil(x)}, |
| 664 | {\tt cos(x)}, |
| 665 | {\tt cosh(x)}, |
| 666 | {\tt exp(x)}, |
| 667 | {\tt fabs(x)}, |
| 668 | {\tt floor(x)}, |
| 669 | %{\tt fmod(...)} XXX not yet |
| 670 | %{\tt frexp(...)} XXX not yet |
| 671 | %{\tt ldexp(...)} XXX not yet |
| 672 | {\tt log(x)}, |
| 673 | {\tt log10(x)}, |
| 674 | %{\tt modf(...)} XXX not yet |
| 675 | {\tt pow(x,y)}, |
| 676 | {\tt sin(x)}, |
| 677 | {\tt sinh(x)}, |
| 678 | {\tt sqrt(x)}, |
| 679 | {\tt tan(x)}, |
| 680 | {\tt tanh(x)}. |
| 681 | |
| 682 | It also defines two mathematical constants: |
| 683 | {\tt pi} |
| 684 | and |
| 685 | {\tt e}. |
| 686 | |
| 687 | \subsection{Built-in Module {\tt time}} |
| 688 | |
| 689 | This module provides various time-related functions. |
| 690 | It is always available. |
| 691 | Functions are: |
| 692 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 693 | \funcitem{sleep}{secs} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 694 | Suspends execution for the given number of seconds. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 695 | \funcitem{time}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 696 | Returns the time in seconds since the Epoch (Thursday January 1, |
| 697 | 00:00:00, 1970 UCT on \UNIX\ machines). |
| 698 | \end{description} |
| 699 | |
| 700 | \noindent |
| 701 | In some versions (Amoeba, Mac) the following functions also exist: |
| 702 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 703 | \funcitem{millisleep}{msecs} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 704 | Suspends execution for the given number of milliseconds. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 705 | \funcitem{millitimer}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 706 | Returns the number of milliseconds of real time elapsed since some point |
| 707 | in the past that is fixed per execution of the python interpreter (but |
| 708 | may change in each following run). |
| 709 | \end{description} |
| 710 | |
| 711 | \noindent |
| 712 | The granularity of the milliseconds functions may be more than a |
| 713 | millisecond (100 msecs on Amoeba, 1/60 sec on the Mac). |
| 714 | |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 715 | \subsection{Built-in Module {\tt regexp}} |
| 716 | |
| 717 | This module provides a regular expression matching operation. |
| 718 | It is always available. |
| 719 | |
| 720 | The module defines a function and an exception: |
| 721 | |
| 722 | \begin{description} |
| 723 | |
| 724 | \funcitem{compile}{pattern} |
| 725 | |
| 726 | Compile a regular expression given as a string into a regular |
| 727 | expression object. |
| 728 | The string must be an egrep-style regular expression; |
| 729 | this means that the characters {\tt '(' ')' '*' '+' '?' '|' '^' '$'} |
| 730 | are special. |
| 731 | (It is implemented using Henry Spencer's regular expression matching |
| 732 | functions.) |
| 733 | |
| 734 | excitem{error}{regexp.error} |
| 735 | |
| 736 | Exception raised when a string passed to {\tt compile()} is not a |
| 737 | valid regular expression (e.g., unmatched parentheses) or when some other |
| 738 | error occurs during compilation or matching |
| 739 | (``no match found'' is not an error). |
| 740 | |
| 741 | \end{description} |
| 742 | |
| 743 | Compiled regular expression objects support a single method: |
| 744 | |
| 745 | \begin{description} |
| 746 | |
| 747 | \funcitem{exec}{str} |
| 748 | |
| 749 | Find the first occurrence of the compiled regular expression in the |
| 750 | string {\tt str}. |
| 751 | The return value is a tuple of pairs specifying where a match was |
| 752 | found and where matches were found for subpatterns specified with |
| 753 | {\tt '('} and {\tt ')'} in the pattern. |
| 754 | If no match is found, an empty tuple is returned; otherwise the first |
| 755 | item of the tuple is a pair of slice indices into the search string |
| 756 | giving the match found. |
| 757 | If there were any subpatterns in the pattern, the returned tuple has an |
| 758 | additional item for each subpattern, giving the slice indices into the |
| 759 | search string where that subpattern was found. |
| 760 | |
| 761 | \end{description} |
| 762 | |
| 763 | For example: |
| 764 | \bcode\begin{verbatim} |
| 765 | >>> import regexp |
| 766 | >>> r = regexp.compile('--(.*)--') |
| 767 | >>> s = 'a--b--c' |
| 768 | >>> r.exec(s) |
| 769 | ((1, 6), (3, 4)) |
| 770 | >>> s[1:6] # The entire match |
| 771 | '--b--' |
| 772 | >>> s[3:4] # The subpattern |
| 773 | 'b' |
| 774 | >>> |
| 775 | \end{verbatim}\ecode |
| 776 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 777 | \subsection{Built-in Module {\tt posix}} |
| 778 | |
| 779 | This module provides access to operating system functionality that is |
| 780 | standardized by the C Standard and the POSIX standard (a thinly diguised |
| 781 | {\UNIX} interface). |
| 782 | It is available in all {\Python} versions except on the Macintosh. |
| 783 | Errors are reported exceptions. |
| 784 | It defines the following items: |
| 785 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 786 | \funcitem{chdir}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 787 | Changes the current directory to |
| 788 | {\tt path}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 789 | \funcitem{chmod}{path, mode} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 790 | Change the mode of |
| 791 | {\tt path} |
| 792 | to the numeric |
| 793 | {\tt mode}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 794 | \funcitem{environ} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 795 | A dictionary representing the string environment at the time |
| 796 | the interpreter was started. |
| 797 | (Modifying this dictionary does not affect the string environment of the |
| 798 | interpreter.) |
| 799 | For example, |
| 800 | {\tt posix.environ['HOME']} |
| 801 | is the pathname of your home directory, equivalent to |
| 802 | {\tt getenv("HOME")} |
| 803 | in C. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 804 | \excitem{error}{posix.error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 805 | %.br |
| 806 | The exception raised when an POSIX function returns an error. |
| 807 | The value accompanying this exception is a pair containing the numeric |
| 808 | error code from |
| 809 | {\tt errno} |
| 810 | and the corresponding string, as would be printed by the C function |
| 811 | {\tt perror()}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 812 | \funcitem{getcwd}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 813 | Returns a string representing the current working directory. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 814 | \funcitem{link}{src, dst} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 815 | Creates a hard link pointing to |
| 816 | {\tt src} |
| 817 | named |
| 818 | {\tt dst}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 819 | \funcitem{listdir}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 820 | Returns a list containing the names of the entries in the |
| 821 | directory. |
| 822 | The list is in arbitrary order. |
| 823 | It includes the special entries |
| 824 | {\tt '.'} |
| 825 | and |
| 826 | {\tt '..'} |
| 827 | if they are present in the directory. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 828 | \funcitem{mkdir}{path, mode} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 829 | Creates a directory named |
| 830 | {\tt path} |
| 831 | with numeric mode |
| 832 | {\tt mode}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 833 | \funcitem{rename}{src, dst} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 834 | Renames the file or directory |
| 835 | {\tt src} |
| 836 | to |
| 837 | {\tt dst}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 838 | \funcitem{rmdir}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 839 | Removes the directory |
| 840 | {\tt path}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 841 | \funcitem{stat}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 842 | Performs a |
| 843 | {\em stat} |
| 844 | system call on the given path. |
| 845 | The return value is a tuple of at least 10 integers giving the most |
| 846 | important (and portable) members of the |
| 847 | {\em stat} |
| 848 | structure, in the order |
| 849 | {\tt st\_mode}, |
| 850 | {\tt st\_ino}, |
| 851 | {\tt st\_dev}, |
| 852 | {\tt st\_nlink}, |
| 853 | {\tt st\_uid}, |
| 854 | {\tt st\_gid}, |
| 855 | {\tt st\_size}, |
| 856 | {\tt st\_atime}, |
| 857 | {\tt st\_mtime}, |
| 858 | {\tt st\_ctime}. |
| 859 | More items may be added at the end by some implementations. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 860 | \funcitem{system}{command} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 861 | Executes the command (a string) in a subshell. |
| 862 | This is implemented by calling the Standard C function |
| 863 | {\tt system()}, |
| 864 | and has the same limitations. |
| 865 | Changes to |
| 866 | {\tt posix.environ}, |
| 867 | {\tt sys.stdin} |
| 868 | etc. are not reflected in the environment of the executed command. |
| 869 | The return value is the exit status of the process as returned by |
| 870 | Standard C |
| 871 | {\tt system()}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 872 | \funcitem{umask}{mask} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 873 | Sets the current numeric umask and returns the previous umask. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 874 | \funcitem{unlink}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 875 | Unlinks the file |
| 876 | {\tt path}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 877 | \funcitem{utimes(path, }{atime, mtime)} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 878 | %.br |
| 879 | Sets the access and modified time of the file to the given values. |
| 880 | (The second argument is a tuple of two items.) |
| 881 | \end{description} |
| 882 | |
| 883 | The following functions are only available on systems that support |
| 884 | symbolic links: |
| 885 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 886 | \funcitem{lstat}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 887 | Like |
| 888 | {\tt stat()}, |
| 889 | but does not follow symbolic links. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 890 | \funcitem{readlink}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 891 | Returns a string representing the path to which the symbolic link |
| 892 | points. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 893 | \funcitem{symlink}{src, dst} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 894 | Creates a symbolic link pointing to |
| 895 | {\tt src} |
| 896 | named |
| 897 | {\tt dst}. |
| 898 | \end{description} |
| 899 | |
| 900 | \subsection{Built-in Module {\tt stdwin}} |
| 901 | |
| 902 | This module defines several new object types and functions that |
| 903 | provide access to the functionality of the Standard Window System |
| 904 | Interface, STDWIN [CWI report CR-R8817]. |
| 905 | It is available on systems to which STDWIN has been ported (which is |
| 906 | most systems). |
| 907 | It is only available if the {\tt DISPLAY} environment variable is set |
| 908 | or an explicit `{\tt -display \it displayname}' argument is passed to |
| 909 | the interpreter. |
| 910 | |
| 911 | Functions have names that usually resemble their C STDWIN counterparts |
| 912 | with the initial `w' dropped. |
| 913 | Points are represented by pairs of integers; rectangles |
| 914 | by pairs of points. |
| 915 | For a complete description of STDWIN please refer to the documentation |
| 916 | of STDWIN for C programmers (aforementioned CWI report). |
| 917 | \subsubsection{Functions Defined in Module {\tt stdwin}} |
| 918 | |
| 919 | The following functions are defined in the {\tt stdwin} module: |
| 920 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 921 | \funcitem{open}{title} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 922 | %.br |
| 923 | Opens a new window whose initial title is given by the string argument. |
| 924 | Returns a window object; window object methods are described below.% |
| 925 | \footnote{ |
| 926 | The {\Python} version of STDWIN does not support draw procedures; all |
| 927 | drawing requests are reported as draw events. |
| 928 | } |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 929 | \funcitem{getevent}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 930 | %.br |
| 931 | Waits for and returns the next event. |
| 932 | An event is returned as a triple: the first element is the event |
| 933 | type, a small integer; the second element is the window object to which |
| 934 | the event applies, or |
| 935 | {\tt None} |
| 936 | if it applies to no window in particular; |
| 937 | the third element is type-dependent. |
| 938 | Names for event types and command codes are defined in the standard |
| 939 | module |
| 940 | {\tt stdwinevent}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 941 | \funcitem{setdefwinpos}{h, v} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 942 | %.br |
| 943 | Sets the default window position. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 944 | \funcitem{setdefwinsize}{width, height} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 945 | %.br |
| 946 | Sets the default window size. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 947 | \funcitem{menucreate}{title} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 948 | %.br |
| 949 | Creates a menu object referring to a global menu (a menu that appears in |
| 950 | all windows). |
| 951 | Methods of menu objects are described below. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 952 | \funcitem{fleep}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 953 | %.br |
| 954 | Causes a beep or bell (or perhaps a `visual bell' or flash, hence the |
| 955 | name). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 956 | \funcitem{message}{string} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 957 | %.br |
| 958 | Displays a dialog box containing the string. |
| 959 | The user must click OK before the function returns. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 960 | \funcitem{askync}{prompt, default} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 961 | %.br |
| 962 | Displays a dialog that prompts the user to answer a question with yes or |
| 963 | no. |
| 964 | The function returns 0 for no, 1 for yes. |
| 965 | If the user hits the Return key, the default (which must be 0 or 1) is |
| 966 | returned. |
| 967 | If the user cancels the dialog, the |
| 968 | {\tt KeyboardInterrupt} |
| 969 | exception is raised. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 970 | \funcitem{askstr}{prompt, default} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 971 | %.br |
| 972 | Displays a dialog that prompts the user for a string. |
| 973 | If the user hits the Return key, the default string is returned. |
| 974 | If the user cancels the dialog, the |
| 975 | {\tt KeyboardInterrupt} |
| 976 | exception is raised. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 977 | \funcitem{askfile}{prompt, default, new} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 978 | %.br |
| 979 | Asks the user to specify a filename. |
| 980 | If |
| 981 | {\tt new} |
| 982 | is zero it must be an existing file; otherwise, it must be a new file. |
| 983 | If the user cancels the dialog, the |
| 984 | {\tt KeyboardInterrupt} |
| 985 | exception is raised. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 986 | \funcitem{setcutbuffer}{i, string} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 987 | %.br |
| 988 | Stores the string in the system's cut buffer number |
| 989 | {\tt i}, |
| 990 | where it can be found (for pasting) by other applications. |
| 991 | On X11, there are 8 cut buffers (numbered 0..7). |
| 992 | Cut buffer number 0 is the `clipboard' on the Macintosh. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 993 | \funcitem{getcutbuffer}{i} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 994 | %.br |
| 995 | Returns the contents of the system's cut buffer number |
| 996 | {\tt i}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 997 | \funcitem{rotatebutbuffers}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 998 | %.br |
| 999 | On X11, this rotates the 8 cut buffers by |
| 1000 | {\tt n}. |
| 1001 | Ignored on the Macintosh. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1002 | \funcitem{getselection}{i} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1003 | %.br |
| 1004 | Returns X11 selection number |
| 1005 | {\tt i.} |
| 1006 | Selections are not cut buffers. |
| 1007 | Selection numbers are defined in module |
| 1008 | {\tt stdwinevents}. |
| 1009 | Selection {\tt WS\_PRIMARY} is the |
| 1010 | {\em primary} |
| 1011 | selection (used by |
| 1012 | xterm, |
| 1013 | for instance); |
| 1014 | selection {\tt WS\_SECONDARY} is the |
| 1015 | {\em secondary} |
| 1016 | selection; selection {\tt WS\_CLIPBOARD} is the |
| 1017 | {\em clipboard} |
| 1018 | selection (used by |
| 1019 | xclipboard). |
| 1020 | On the Macintosh, this always returns an empty string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1021 | \funcitem{resetselection}{i} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1022 | %.br |
| 1023 | Resets selection number |
| 1024 | {\tt i}, |
| 1025 | if this process owns it. |
| 1026 | (See window method |
| 1027 | {\tt setselection()}). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1028 | \funcitem{baseline}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1029 | %.br |
| 1030 | Return the baseline of the current font (defined by STDWIN as the |
| 1031 | vertical distance between the baseline and the top of the |
| 1032 | characters).% |
| 1033 | \footnote{ |
| 1034 | There is no way yet to set the current font. |
| 1035 | This will change in a future version. |
| 1036 | } |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1037 | \funcitem{lineheight}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1038 | %.br |
| 1039 | Return the total line height of the current font. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1040 | \funcitem{textbreak}{str, width} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1041 | %.br |
| 1042 | Return the number of characters of the string that fit into a space of |
| 1043 | {\tt width} |
| 1044 | bits wide when drawn in the curent font. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1045 | \funcitem{textwidth}{str} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1046 | %.br |
| 1047 | Return the width in bits of the string when drawn in the current font. |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1048 | \end{description} |
| 1049 | |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1050 | \subsubsection{Window Object Methods} |
| 1051 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1052 | Window objects are created by |
| 1053 | {\tt stdwin.open()}. |
| 1054 | There is no explicit function to close a window; windows are closed when |
| 1055 | they are garbage-collected. |
| 1056 | Window objects have the following methods: |
| 1057 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1058 | \funcitem{begindrawing}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1059 | Returns a drawing object, whose methods (described below) allow drawing |
| 1060 | in the window. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1061 | \funcitem{change}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1062 | Invalidates the given rectangle; this may cause a draw event. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1063 | \funcitem{gettitle}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1064 | Returns the window's title string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1065 | \funcitem{getdocsize}{} |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1066 | \begin{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1067 | Returns a pair of integers giving the size of the document as set by |
| 1068 | {\tt setdocsize()}. |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1069 | \end{sloppypar} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1070 | \funcitem{getorigin}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1071 | Returns a pair of integers giving the origin of the window with respect |
| 1072 | to the document. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1073 | \funcitem{getwinsize}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1074 | Returns a pair of integers giving the size of the window. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1075 | \funcitem{menucreate}{title} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1076 | Creates a menu object referring to a local menu (a menu that appears |
| 1077 | only in this window). |
| 1078 | Methods menu objects are described below. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1079 | \funcitem{scroll}{rect,~point} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1080 | Scrolls the given rectangle by the vector given by the point. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1081 | \funcitem{setwincursor}{name} |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1082 | \begin{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1083 | Sets the window cursor to a cursor of the given name. |
| 1084 | It raises the |
| 1085 | {\tt Runtime\-Error} |
| 1086 | exception if no cursor of the given name exists. |
| 1087 | Suitable names are |
| 1088 | {\tt 'ibeam'}, |
| 1089 | {\tt 'arrow'}, |
| 1090 | {\tt 'cross'}, |
| 1091 | {\tt 'watch'} |
| 1092 | and |
| 1093 | {\tt 'plus'}. |
| 1094 | On X11, there are many more (see |
| 1095 | {\tt <X11/cursorfont.h>}). |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1096 | \end{sloppypar} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1097 | \funcitem{setdocsize}{point} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1098 | Sets the size of the drawing document. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1099 | \funcitem{setorigin}{point} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1100 | Moves the origin of the window to the given point in the document. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1101 | \funcitem{setselection}{i, str} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1102 | Attempts to set X11 selection number |
| 1103 | {\tt i} |
| 1104 | to the string |
| 1105 | {\tt str}. |
| 1106 | (See stdwin method |
| 1107 | {\tt getselection()} |
| 1108 | for the meaning of |
| 1109 | {\tt i}.) |
| 1110 | Returns true if it succeeds. |
| 1111 | If it succeeds, the window ``owns'' the selection until |
| 1112 | (a) another applications takes ownership of the selection; or |
| 1113 | (b) the window is deleted; or |
| 1114 | (c) the application clears ownership by calling |
| 1115 | {\tt stdwin.resetselection(i)}. |
| 1116 | When another application takes ownership of the selection, a |
| 1117 | {\tt WE\_LOST\_SEL} |
| 1118 | event is received for no particular window and with the selection number |
| 1119 | as detail. |
| 1120 | Ignored on the Macintosh. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1121 | \funcitem{settitle}{title} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1122 | Sets the window's title string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1123 | \funcitem{settimer}{dsecs} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1124 | Schedules a timer event for the window in |
| 1125 | {\tt dsecs/10} |
| 1126 | seconds. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1127 | \funcitem{show}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1128 | Tries to ensure that the given rectangle of the document is visible in |
| 1129 | the window. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1130 | \funcitem{textcreate}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1131 | Creates a text-edit object in the document at the given rectangle. |
| 1132 | Methods of text-edit objects are described below. |
| 1133 | \end{description} |
| 1134 | |
| 1135 | \subsubsection{Drawing Object Methods} |
| 1136 | |
| 1137 | Drawing objects are created exclusively by the window method |
| 1138 | {\tt begindrawing()}. |
| 1139 | Only one drawing object can exist at any given time; the drawing object |
| 1140 | must be deleted to finish drawing. |
| 1141 | No drawing object may exist when |
| 1142 | {\tt stdwin.getevent()} |
| 1143 | is called. |
| 1144 | Drawing objects have the following methods: |
| 1145 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1146 | \funcitem{box}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1147 | Draws a box around a rectangle. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1148 | \funcitem{circle}{center, radius} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1149 | %.br |
| 1150 | Draws a circle with given center point and radius. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1151 | \funcitem{elarc(center, (rh, rv), }{a1, a2)} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1152 | %.br |
| 1153 | Draws an elliptical arc with given center point. |
| 1154 | {\tt (rh, rv)} |
| 1155 | gives the half sizes of the horizontal and vertical radii. |
| 1156 | {\tt (a1, a2)} |
| 1157 | gives the angles (in degrees) of the begin and end points. |
| 1158 | 0 degrees is at 3 o'clock, 90 degrees is at 12 o'clock. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1159 | \funcitem{erase}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1160 | Erases a rectangle. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1161 | \funcitem{invert}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1162 | Inverts a rectangle. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1163 | \funcitem{line}{p1, p2} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1164 | Draws a line from point |
| 1165 | {\tt p1} |
| 1166 | to |
| 1167 | {\tt p2}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1168 | \funcitem{paint}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1169 | Fills a rectangle. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1170 | \funcitem{text}{p, str} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1171 | Draws a string starting at point p (the point specifies the |
| 1172 | top left coordinate of the string). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1173 | \funcitem{shade}{rect, percent} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1174 | %.br |
| 1175 | Fills a rectangle with a shading pattern that is about |
| 1176 | {\tt percent} |
| 1177 | percent filled. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1178 | \funcitem{xorline}{p1, p2} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1179 | Draws a line in XOR mode. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1180 | \funcitem{baseline(), lineheight(), textbreak(), textwidth}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1181 | %.br |
| 1182 | These functions are similar to the corresponding functions described |
| 1183 | above for the |
| 1184 | {\tt stdwin} |
| 1185 | module, but use the current font of the window instead of the (global) |
| 1186 | default font. |
| 1187 | \end{description} |
| 1188 | |
| 1189 | \subsubsection{Menu Object Methods} |
| 1190 | |
| 1191 | A menu object represents a menu. |
| 1192 | The menu is destroyed when the menu object is deleted. |
| 1193 | The following methods are defined: |
| 1194 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1195 | \funcitem{additem}{text, shortcut} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1196 | %.br |
| 1197 | Adds a menu item with given text. |
| 1198 | The shortcut must be a string of length 1, or omitted (to specify no |
| 1199 | shortcut). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1200 | \funcitem{setitem}{i, text} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1201 | Sets the text of item number |
| 1202 | {\tt i}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1203 | \funcitem{enable}{i, flag} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1204 | Enables or disables item |
| 1205 | {\tt i}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1206 | \funcitem{check}{i, flag} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1207 | Sets or clears the |
| 1208 | {\em check mark} |
| 1209 | for item |
| 1210 | {\tt i}. |
| 1211 | \end{description} |
| 1212 | |
| 1213 | \subsubsection{Text-edit Object Methods} |
| 1214 | |
| 1215 | A text-edit object represents a text-edit block. |
| 1216 | For semantics, see the STDWIN documentation for C programmers. |
| 1217 | The following methods exist: |
| 1218 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1219 | \funcitem{arrow}{code} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1220 | Passes an arrow event to the text-edit block. |
| 1221 | The |
| 1222 | {\tt code} |
| 1223 | must be one of |
| 1224 | {\tt WC\_LEFT}, |
| 1225 | {\tt WC\_RIGHT}, |
| 1226 | {\tt WC\_UP} |
| 1227 | or |
| 1228 | {\tt WC\_DOWN} |
| 1229 | (see module |
| 1230 | {\tt stdwinevents}). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1231 | \funcitem{draw}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1232 | Passes a draw event to the text-edit block. |
| 1233 | The rectangle specifies the redraw area. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1234 | \funcitem{event}{type, window, detail} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1235 | %.br |
| 1236 | Passes an event gotten from |
| 1237 | {\tt stdwin.getevent()} |
| 1238 | to the text-edit block. |
| 1239 | Returns true if the event was handled. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1240 | \funcitem{getfocus}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1241 | Returns 2 integers representing the start and end positions of the |
| 1242 | focus, usable as slice indices on the string returned by |
| 1243 | {\tt getfocustext()}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1244 | \funcitem{getfocustext}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1245 | Returns the text in the focus. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1246 | \funcitem{getrect}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1247 | Returns a rectangle giving the actual position of the text-edit block. |
| 1248 | (The bottom coordinate may differ from the initial position because |
| 1249 | the block automatically shrinks or grows to fit.) |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1250 | \funcitem{gettext}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1251 | Returns the entire text buffer. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1252 | \funcitem{move}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1253 | Specifies a new position for the text-edit block in the document. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1254 | \funcitem{replace}{str} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1255 | Replaces the focus by the given string. |
| 1256 | The new focus is an insert point at the end of the string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1257 | \funcitem{setfocus}{i,~j} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1258 | Specifies the new focus. |
| 1259 | Out-of-bounds values are silently clipped. |
| 1260 | \end{description} |
| 1261 | |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1262 | \subsubsection{Example} |
| 1263 | |
| 1264 | Here is a simple example of using STDWIN in Python. |
| 1265 | It creates a window and draws the string ``Hello world'' in the top |
| 1266 | left corner of the window. |
| 1267 | The window will be correctly redrawn when covered and re-exposed. |
| 1268 | The program quits when the close icon or menu item is requested. |
| 1269 | \bcode\begin{verbatim} |
| 1270 | import stdwin |
| 1271 | from stdwinevents import * |
| 1272 | |
| 1273 | def main(): |
| 1274 | mywin = stdwin.open('Hello') |
| 1275 | # |
| 1276 | while 1: |
| 1277 | (type, win, detail) = stdwin.getevent() |
| 1278 | if type = WE_DRAW: |
| 1279 | draw = win.begindrawing() |
| 1280 | draw.text((0, 0), 'Hello, world') |
| 1281 | del draw |
| 1282 | elif type = WE_CLOSE: |
| 1283 | break |
| 1284 | |
| 1285 | main() |
| 1286 | \end{verbatim}\ecode |
| 1287 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1288 | \subsection{Built-in Module {\tt amoeba}} |
| 1289 | |
| 1290 | This module provides some object types and operations useful for |
| 1291 | Amoeba applications. |
| 1292 | It is only available on systems that support Amoeba operations. |
| 1293 | RPC errors and other Amoeba errors are reported as the exception |
| 1294 | {\tt amoeba.error = 'amoeba.error'}. |
| 1295 | The module |
| 1296 | {\tt amoeba} |
| 1297 | defines the following items: |
| 1298 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1299 | \funcitem{name\_append}{path,~cap} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1300 | %.br |
| 1301 | Stores a capability in the Amoeba directory tree. |
| 1302 | Arguments are the pathname (a string) and the capability (a capability |
| 1303 | object as returned by |
| 1304 | {\tt name\_lookup()}). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1305 | \funcitem{name\_delete}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1306 | %.br |
| 1307 | Deletes a capability from the Amoeba directory tree. |
| 1308 | Argument is the pathname. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1309 | \funcitem{name\_lookup}{path} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1310 | %.br |
| 1311 | Looks up a capability. |
| 1312 | Argument is the pathname. |
| 1313 | Returns a |
| 1314 | {\em capability} |
| 1315 | object, to which various interesting operations apply, described below. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1316 | \funcitem{name\_replace}{path,~cap} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1317 | %.br |
| 1318 | Replaces a capability in the Amoeba directory tree. |
| 1319 | Arguments are the pathname and the new capability. |
| 1320 | (This differs from |
| 1321 | {\tt name\_append()} |
| 1322 | in the behavior when the pathname already exists: |
| 1323 | {\tt name\_append()} |
| 1324 | finds this an error while |
| 1325 | {\tt name\_replace()} |
| 1326 | allows it, as its name suggests.) |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1327 | \funcitem{capv} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1328 | A table representing the capability environment at the time the |
| 1329 | interpreter was started. |
| 1330 | (Alas, modifying this table does not affect the capability environment |
| 1331 | of the interpreter.) |
| 1332 | For example, |
| 1333 | {\tt amoeba.capv['ROOT']} |
| 1334 | is the capability of your root directory, similar to |
| 1335 | {\tt getcap("ROOT")} |
| 1336 | in C. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1337 | \excitem{error}{amoeba.error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1338 | %.br |
| 1339 | The exception raised when an Amoeba function returns an error. |
| 1340 | The value accompanying this exception is a pair containing the numeric |
| 1341 | error code and the corresponding string, as returned by the C function |
| 1342 | {\tt err\_why()}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1343 | \funcitem{timeout}{msecs} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1344 | %.br |
| 1345 | Sets the transaction timeout, in milliseconds. |
| 1346 | Returns the previous timeout. |
| 1347 | Initially, the timeout is set to 2 seconds by the {\Python} interpreter. |
| 1348 | \end{description} |
| 1349 | |
| 1350 | \subsubsection{Capability Operations} |
| 1351 | |
| 1352 | Capabilities are written in a convenient ASCII format, also used by the |
| 1353 | Amoeba utilities |
| 1354 | {\em c2a}(U) |
| 1355 | and |
| 1356 | {\em a2c}(U). |
| 1357 | For example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1358 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1359 | >>> amoeba.name_lookup('/profile/cap') |
| 1360 | aa:1c:95:52:6a:fa/14(ff)/8e:ba:5b:8:11:1a |
| 1361 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1362 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1363 | The following methods are defined for capability objects. |
| 1364 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1365 | \funcitem{dir\_list}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1366 | Returns a list of the names of the entries in an Amoeba directory. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1367 | \funcitem{b\_read}{offset, maxsize} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1368 | %.br |
| 1369 | Reads (at most) |
| 1370 | {\tt maxsize} |
| 1371 | bytes from a bullet file at offset |
| 1372 | {\tt offset.} |
| 1373 | The data is returned as a string. |
| 1374 | EOF is reported as an empty string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1375 | \funcitem{b\_size}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1376 | Returns the size of a bullet file. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1377 | \funcitem{dir\_append(), dir\_delete(), dir\_lookup(), dir\_replace}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1378 | %.br |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1379 | \itembreak |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1380 | Like the corresponding |
| 1381 | {\tt name\_*} |
| 1382 | functions, but with a path relative to the capability. |
| 1383 | (For paths beginning with a slash the capability is ignored, since this |
| 1384 | is the defined semantics for Amoeba.) |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1385 | \funcitem{std\_info}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1386 | Returns the standard info string of the object. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1387 | \funcitem{tod\_gettime}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1388 | Returns the time (in seconds since the Epoch, in UCT, as for POSIX) from |
| 1389 | a time server. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1390 | \funcitem{tod\_settime}{t} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1391 | Sets the time kept by a time server. |
| 1392 | \end{description} |
| 1393 | |
| 1394 | \subsection{Built-in Module {\tt audio}} |
| 1395 | |
| 1396 | This module provides rudimentary access to the audio I/O device |
| 1397 | {\tt /dev/audio} |
| 1398 | on the Silicon Graphics Personal IRIS; see audio(7). |
| 1399 | It supports the following operations: |
| 1400 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1401 | \funcitem{setoutgain}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1402 | Sets the output gain (0-255). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1403 | \funcitem{getoutgain}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1404 | Returns the output gain. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1405 | \funcitem{setrate}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1406 | Sets the sampling rate: 1=32K/sec, 2=16K/sec, 3=8K/sec. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1407 | \funcitem{setduration}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1408 | Sets the `sound duration' in units of 1/100 seconds. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1409 | \funcitem{read}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1410 | Reads a chunk of |
| 1411 | {\tt n} |
| 1412 | sampled bytes from the audio input (line in or microphone). |
| 1413 | The chunk is returned as a string of length n. |
| 1414 | Each byte encodes one sample as a signed 8-bit quantity using linear |
| 1415 | encoding. |
| 1416 | This string can be converted to numbers using {\tt chr2num()} described |
| 1417 | below. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1418 | \funcitem{write}{buf} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1419 | Writes a chunk of samples to the audio output (speaker). |
| 1420 | \end{description} |
| 1421 | |
| 1422 | These operations support asynchronous audio I/O: |
| 1423 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1424 | \funcitem{start\_recording}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1425 | %.br |
| 1426 | Starts a second thread (a process with shared memory) that begins reading |
| 1427 | {\tt n} |
| 1428 | bytes from the audio device. |
| 1429 | The main thread immediately continues. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1430 | \funcitem{wait\_recording}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1431 | %.br |
| 1432 | Waits for the second thread to finish and returns the data read. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1433 | \funcitem{stop\_recording}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1434 | %.br |
| 1435 | Makes the second thread stop reading as soon as possible. |
| 1436 | Returns the data read so far. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1437 | \funcitem{poll\_recording}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1438 | %.br |
| 1439 | Returns true if the second thread has finished reading (so |
| 1440 | {\tt wait\_recording()} would return the data without delay). |
| 1441 | \item[{\tt start\_playing(chunk)}, {\tt wait\_playing()}, |
| 1442 | {\tt stop\_playing()}, {\tt poll\_playing()}] |
| 1443 | %.br |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1444 | \begin{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1445 | Similar but for output. |
| 1446 | {\tt stop\_playing()} |
| 1447 | returns a lower bound for the number of bytes actually played (not very |
| 1448 | accurate). |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1449 | \end{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1450 | \end{description} |
| 1451 | |
| 1452 | The following operations do not affect the audio device but are |
| 1453 | implemented in C for efficiency: |
| 1454 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1455 | \funcitem{amplify}{buf, f1, f2} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1456 | %.br |
| 1457 | Amplifies a chunk of samples by a variable factor changing from |
| 1458 | {\tt f1}/256 to {\tt f2}/256. |
| 1459 | Negative factors are allowed. |
| 1460 | Resulting values that are to large to fit in a byte are clipped. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1461 | \funcitem{reverse}{buf} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1462 | %.br |
| 1463 | Returns a chunk of samples backwards. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1464 | \funcitem{add}{buf1, buf2} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1465 | %.br |
| 1466 | Bytewise adds two chunks of samples. |
| 1467 | Bytes that exceed the range are clipped. |
| 1468 | If one buffer shorter, it is assumed to be padded with zeros. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1469 | \funcitem{chr2num}{buf} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1470 | %.br |
| 1471 | Converts a string of sampled bytes as returned by {\tt read()} into |
| 1472 | a list containing the numeric values of the samples. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1473 | \funcitem{num2chr}{list} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1474 | %.br |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1475 | \begin{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1476 | Converts a list as returned by |
| 1477 | {\tt chr2num()} |
| 1478 | back to a buffer acceptable by |
| 1479 | {\tt write()}. |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1480 | \end{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1481 | \end{description} |
| 1482 | |
| 1483 | \subsection{Built-in Module {\tt gl}} |
| 1484 | |
| 1485 | This module provides access to the Silicon Graphics |
| 1486 | {\em Graphics Library}. |
| 1487 | It is available only on Silicon Graphics machines. |
| 1488 | |
| 1489 | {\bf Warning:} |
| 1490 | Some illegal calls to the GL library cause the {\Python} interpreter to dump |
| 1491 | core. |
| 1492 | In particular, the use of most GL calls is unsafe before the first |
| 1493 | window is opened. |
| 1494 | |
| 1495 | The module is too large to document here in its entirety, but the |
| 1496 | following should help you to get started. |
| 1497 | The parameter conventions for the C functions are translated to {\Python} as |
| 1498 | follows: |
| 1499 | |
| 1500 | \begin{itemize} |
| 1501 | \item |
| 1502 | All (short, long, unsigned) int values are represented by {\Python} |
| 1503 | integers. |
| 1504 | \item |
| 1505 | All float and double values are represented by {\Python} floating point |
| 1506 | numbers. |
| 1507 | In most cases, {\Python} integers are also allowed. |
| 1508 | \item |
| 1509 | All arrays are represented by one-dimensional {\Python} lists. |
| 1510 | In most cases, tuples are also allowed. |
| 1511 | \item |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1512 | \begin{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1513 | All string and character arguments are represented by {\Python} strings, |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1514 | for instance, |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1515 | {\tt winopen('Hi~There!')} |
| 1516 | and |
| 1517 | {\tt rotate(900,~'z')}. |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1518 | \end{sloppypar} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1519 | \item |
| 1520 | All (short, long, unsigned) integer arguments or return values that are |
| 1521 | only used to specify the length of an array argument are omitted. |
| 1522 | For example, the C call |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1523 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1524 | lmdef(deftype, index, np, props) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1525 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1526 | is translated to {\Python} as |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1527 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1528 | lmdef(deftype, index, props) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1529 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1530 | \item |
| 1531 | Output arguments are omitted from the argument list; they are |
| 1532 | transmitted as function return values instead. |
| 1533 | If more than one value must be returned, the return value is a tuple. |
| 1534 | If the C function has both a regular return value (that is not omitted |
| 1535 | because of the previous rule) and an output argument, the return value |
| 1536 | comes first in the tuple. |
| 1537 | Examples: the C call |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1538 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1539 | getmcolor(i, &red, &green, &blue) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1540 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1541 | is translated to {\Python} as |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1542 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1543 | red, green, blue = getmcolor(i) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1544 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1545 | \end{itemize} |
| 1546 | |
| 1547 | The following functions are non-standard or have special argument |
| 1548 | conventions: |
| 1549 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1550 | \funcitem{varray}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1551 | Equivalent to but faster than a number of |
| 1552 | {\tt v3d()} |
| 1553 | calls. |
| 1554 | The argument is a list (or tuple) of points. |
| 1555 | Each point must be a tuple of coordinates (x, y, z) or (x, y). |
| 1556 | The points may be 2- or 3-dimensional but must all have the |
| 1557 | same dimension. |
| 1558 | Float and int values may be mixed however. |
| 1559 | The points are always converted to 3D double precision points |
| 1560 | by assuming z=0.0 if necessary (as indicated in the man page), |
| 1561 | and for each point |
| 1562 | {\tt v3d()} |
| 1563 | is called. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1564 | \funcitem{nvarray}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1565 | Equivalent to but faster than a number of |
| 1566 | {\tt n3f} |
| 1567 | and |
| 1568 | {\tt v3f} |
| 1569 | calls. |
| 1570 | The argument is an array (list or tuple) of pairs of normals and points. |
| 1571 | Each pair is a tuple of a point and a normal for that point. |
| 1572 | Each point or normal must be a tuple of coordinates (x, y, z). |
| 1573 | Three coordinates must be given. |
| 1574 | Float and int values may be mixed. |
| 1575 | For each pair, |
| 1576 | {\tt n3f()} |
| 1577 | is called for the normal, and then |
| 1578 | {\tt v3f()} |
| 1579 | is called for the point. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1580 | \funcitem{vnarray}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1581 | Similar to |
| 1582 | {\tt nvarray()} |
| 1583 | but the pairs have the point first and the normal second. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1584 | \funcitem{nurbssurface}{s\_k[], t\_k[], ctl[][], s\_ord, t\_ord, type} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1585 | %.br |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1586 | \itembreak |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1587 | Defines a nurbs surface. |
| 1588 | The dimensions of |
| 1589 | {\tt ctl[][]} |
| 1590 | are computed as follows: |
| 1591 | {\tt [len(s\_k)~-~s\_ord]}, |
| 1592 | {\tt [len(t\_k)~-~t\_ord]}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1593 | \funcitem{nurbscurve}{knots, ctlpoints, order, type} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1594 | %.br |
| 1595 | Defines a nurbs curve. |
| 1596 | The length of ctlpoints is |
| 1597 | {\tt len(knots)~-~order}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1598 | \funcitem{pwlcurve}{points, type} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1599 | %.br |
| 1600 | Defines a piecewise-linear curve. |
| 1601 | {\tt points} |
| 1602 | is a list of points. |
| 1603 | {\tt type} |
| 1604 | must be |
| 1605 | {\tt N\_ST}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1606 | \funcitem{pick(n), select}{n} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1607 | %.br |
| 1608 | The only argument to these functions specifies the desired size of the |
| 1609 | pick or select buffer. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1610 | \funcitem{endpick(), endselect}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1611 | %.br |
| 1612 | These functions have no arguments. |
| 1613 | They return a list of integers representing the used part of the |
| 1614 | pick/select buffer. |
| 1615 | No method is provided to detect buffer overrun. |
| 1616 | \end{description} |
| 1617 | |
| 1618 | Here is a tiny but complete example GL program in {\Python}: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1619 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1620 | import gl, GL, time |
| 1621 | |
| 1622 | def main(): |
| 1623 | gl.foreground() |
| 1624 | gl.prefposition(500, 900, 500, 900) |
| 1625 | w = gl.winopen('CrissCross') |
| 1626 | gl.ortho2(0.0, 400.0, 0.0, 400.0) |
| 1627 | gl.color(GL.WHITE) |
| 1628 | gl.clear() |
| 1629 | gl.color(GL.RED) |
| 1630 | gl.bgnline() |
| 1631 | gl.v2f(0.0, 0.0) |
| 1632 | gl.v2f(400.0, 400.0) |
| 1633 | gl.endline() |
| 1634 | gl.bgnline() |
| 1635 | gl.v2f(400.0, 0.0) |
| 1636 | gl.v2f(0.0, 400.0) |
| 1637 | gl.endline() |
| 1638 | time.sleep(5) |
| 1639 | |
| 1640 | main() |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1641 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1642 | |
| 1643 | \subsection{Built-in Module {\tt pnl}} |
| 1644 | |
| 1645 | This module provides access to the |
| 1646 | {\em Panel Library} |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1647 | built by NASA Ames (to get it, send e-mail to |
| 1648 | {\tt panel-request@nas.nasa.gov}). |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1649 | All access to it should be done through the standard module |
| 1650 | {\tt panel}, |
| 1651 | which transparantly exports most functions from |
| 1652 | {\tt pnl} |
| 1653 | but redefines |
| 1654 | {\tt pnl.dopanel()}. |
| 1655 | |
| 1656 | {\bf Warning:} |
| 1657 | the {\Python} interpreter will dump core if you don't create a GL window |
| 1658 | before calling |
| 1659 | {\tt pnl.mkpanel()}. |
| 1660 | |
| 1661 | The module is too large to document here in its entirety. |
| 1662 | |
| 1663 | \section{Standard Modules} |
| 1664 | |
| 1665 | The following standard modules are defined. |
| 1666 | They are available in one of the directories in the default module |
| 1667 | search path (try printing |
| 1668 | {\tt sys.path} |
| 1669 | to find out the default search path.) |
| 1670 | |
| 1671 | \subsection{Standard Module {\tt string}} |
| 1672 | |
| 1673 | This module defines some constants useful for checking character |
| 1674 | classes, some exceptions, and some useful string functions. |
| 1675 | The constants are: |
| 1676 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1677 | \funcitem{digits} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1678 | The string |
| 1679 | {\tt '0123456789'}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1680 | \funcitem{hexdigits} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1681 | The string |
| 1682 | {\tt '0123456789abcdefABCDEF'}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1683 | \funcitem{letters} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1684 | The concatenation of the strings |
| 1685 | {\tt lowercase} |
| 1686 | and |
| 1687 | {\tt uppercase} |
| 1688 | described below. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1689 | \funcitem{lowercase} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1690 | The string |
| 1691 | {\tt 'abcdefghijklmnopqrstuvwxyz'}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1692 | \funcitem{octdigits} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1693 | The string |
| 1694 | {\tt '01234567'}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1695 | \funcitem{uppercase} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1696 | The string |
| 1697 | {\tt 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1698 | \funcitem{whitespace} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1699 | A string containing all characters that are considered whitespace, |
| 1700 | i.e., |
| 1701 | space, tab and newline. |
| 1702 | This definition is used by |
| 1703 | {\tt split()} |
| 1704 | and |
| 1705 | {\tt strip()}. |
| 1706 | \end{description} |
| 1707 | |
| 1708 | The exceptions are: |
| 1709 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1710 | \excitem{atoi\_error}{non-numeric argument to string.atoi} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1711 | %.br |
| 1712 | Exception raised by |
| 1713 | {\tt atoi} |
| 1714 | when a non-numeric string argument is detected. |
| 1715 | The exception argument is the offending string. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1716 | \excitem{index\_error}{substring not found in string.index} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1717 | %.br |
| 1718 | Exception raised by |
| 1719 | {\tt index} |
| 1720 | when |
| 1721 | {\tt sub} |
| 1722 | is not found. |
| 1723 | The argument are the offending arguments to index: {\tt (s, sub)}. |
| 1724 | \end{description} |
| 1725 | |
| 1726 | The functions are: |
| 1727 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1728 | \funcitem{atoi}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1729 | Converts a string to a number. |
| 1730 | The string must consist of one or more digits, optionally preceded by a |
| 1731 | sign ({\tt '+'} or {\tt '-'}). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1732 | \funcitem{index}{s, sub} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1733 | Returns the lowest index in |
| 1734 | {\tt s} |
| 1735 | where the substring |
| 1736 | {\tt sub} |
| 1737 | is found. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1738 | \funcitem{lower}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1739 | Convert letters to lower case. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1740 | \funcitem{split}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1741 | Returns a list of the whitespace-delimited words of the string |
| 1742 | {\tt s}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1743 | \funcitem{splitfields}{s, sep} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1744 | %.br |
| 1745 | Returns a list containing the fields of the string |
| 1746 | {\tt s}, |
| 1747 | using the string |
| 1748 | {\tt sep} |
| 1749 | as a separator. |
| 1750 | The list will have one more items than the number of non-overlapping |
| 1751 | occurrences of the separator in the string. |
| 1752 | Thus, |
| 1753 | {\tt string.splitfields(s, ' ')} |
| 1754 | is not the same as |
| 1755 | {\tt string.split(s)}, |
| 1756 | as the latter only returns non-empty words. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1757 | \funcitem{strip}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1758 | Removes leading and trailing whitespace from the string |
| 1759 | {\tt s}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1760 | \funcitem{swapcase}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1761 | Converts lower case letters to upper case and vice versa. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1762 | \funcitem{upper}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1763 | Convert letters to upper case. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1764 | \funcitem{ljust(s, width), rjust(s, width), center}{s, width} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1765 | %.br |
| 1766 | These functions respectively left-justify, right-justify and center a |
| 1767 | string in a field of given width. |
| 1768 | They return a string that is at least |
| 1769 | {\tt width} |
| 1770 | characters wide, created by padding the string |
| 1771 | {\tt s} |
| 1772 | with spaces until the given width on the right, left or both sides. |
| 1773 | The string is never truncated. |
| 1774 | \end{description} |
| 1775 | |
| 1776 | \subsection{Standard Module {\tt path}} |
| 1777 | |
| 1778 | This module implements some useful functions on POSIX pathnames. |
| 1779 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1780 | \funcitem{basename}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1781 | Returns the base name of pathname |
| 1782 | {\tt p}. |
| 1783 | This is the second half of the pair returned by |
| 1784 | {\tt path.split(p)}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1785 | \funcitem{cat}{p, q} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1786 | Performs intelligent pathname concatenation on paths |
| 1787 | {\tt p} |
| 1788 | and |
| 1789 | {\tt q}: |
| 1790 | If |
| 1791 | {\tt q} |
| 1792 | is an absolute path, the return value is |
| 1793 | {\tt q}. |
| 1794 | Otherwise, the concatenation of |
| 1795 | {\tt p} |
| 1796 | and |
| 1797 | {\tt q} |
| 1798 | is returned, with a slash ({\tt '/'}) inserted unless |
| 1799 | {\tt p} |
| 1800 | is empty or ends in a slash. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1801 | \funcitem{commonprefix}{list} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1802 | %.br |
| 1803 | Returns the longest string that is a prefix of all strings in |
| 1804 | {\tt list}. |
| 1805 | If |
| 1806 | {\tt list} |
| 1807 | is empty, the empty string ({\tt ''}) is returned. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1808 | \funcitem{exists}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1809 | Returns true if |
| 1810 | {\tt p} |
| 1811 | refers to an existing path. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1812 | \funcitem{isdir}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1813 | Returns true if |
| 1814 | {\tt p} |
| 1815 | refers to an existing directory. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1816 | \funcitem{islink}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1817 | Returns true if |
| 1818 | {\tt p} |
| 1819 | refers to a directory entry that is a symbolic link. |
| 1820 | Always false if symbolic links are not supported. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1821 | \funcitem{ismount}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1822 | Returns true if |
| 1823 | {\tt p} |
| 1824 | is an absolute path that occurs in the mount table as output by the |
| 1825 | {\tt /etc/mount} |
| 1826 | utility. |
| 1827 | This output is read once when the function is used for the first |
| 1828 | time.% |
| 1829 | \footnote{ |
| 1830 | Is there a better way to check for mount points? |
| 1831 | } |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1832 | \funcitem{split}{p} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1833 | Returns a pair |
| 1834 | {\tt (head,~tail)} |
| 1835 | such that |
| 1836 | {\tt tail} |
| 1837 | contains no slashes and |
| 1838 | {\tt path.cat(head, tail)} |
| 1839 | is equal to |
| 1840 | {\tt p}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1841 | \funcitem{walk}{p, visit, arg} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1842 | %.br |
| 1843 | Calls the function |
| 1844 | {\tt visit} |
| 1845 | with arguments |
| 1846 | {\tt (arg, dirname, names)} |
| 1847 | for each directory in the directory tree rooted at |
| 1848 | {\tt p} |
| 1849 | (including |
| 1850 | {\tt p} |
| 1851 | itself, if it is a directory). |
| 1852 | The argument |
| 1853 | {\tt dirname} |
| 1854 | specifies the visited directory, the argument |
| 1855 | {\tt names} |
| 1856 | lists the files in the directory (gotten from |
| 1857 | {\tt posix.listdir(dirname)}). |
| 1858 | The |
| 1859 | {\tt visit} |
| 1860 | function may modify |
| 1861 | {\tt names} |
| 1862 | to influence the set of directories visited below |
| 1863 | {\tt dirname}, |
| 1864 | e.g., |
| 1865 | to avoid visiting certain parts of the tree. |
| 1866 | (The object referred to by |
| 1867 | {\tt names} |
| 1868 | must be modified in place, using |
| 1869 | {\tt del} |
| 1870 | or slice assignment.) |
| 1871 | \end{description} |
| 1872 | |
| 1873 | \subsection{Standard Module {\tt getopt}} |
| 1874 | |
| 1875 | This module helps scripts to parse the command line arguments in |
| 1876 | {\tt sys.argv}. |
| 1877 | It uses the same conventions as the {\UNIX} |
| 1878 | {\tt getopt()} |
| 1879 | function. |
| 1880 | It defines the function |
| 1881 | {\tt getopt.getopt(args, options)} |
| 1882 | and the exception |
| 1883 | {\tt getopt.error}. |
| 1884 | |
| 1885 | The first argument to |
| 1886 | {\tt getopt()} |
| 1887 | is the argument list passed to the script with its first element |
| 1888 | chopped off (i.e., |
| 1889 | {\tt sys.argv[1:]}). |
| 1890 | The second argument is the string of option letters that the |
| 1891 | script wants to recognize, with options that require an argument |
| 1892 | followed by a colon (i.e., the same format that {\UNIX} |
| 1893 | {\tt getopt()} |
| 1894 | uses). |
| 1895 | The return value consists of two elements: the first is a list of |
| 1896 | option-and-value pairs; the second is the list of program arguments |
| 1897 | left after the option list was stripped (this is a trailing slice of the |
| 1898 | first argument). |
| 1899 | Each option-and-value pair returned has the option as its first element, |
| 1900 | prefixed with a hyphen (e.g., |
| 1901 | {\tt '-x'}), |
| 1902 | and the option argument as its second element, or an empty string if the |
| 1903 | option has no argument. |
| 1904 | The options occur in the list in the same order in which they were |
| 1905 | found, thus allowing multiple occurrences. |
| 1906 | Example: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1907 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1908 | >>> import getopt, string |
| 1909 | >>> args = string.split('-a -b -cfoo -d bar a1 a2') |
| 1910 | >>> args |
| 1911 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 1912 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 1913 | >>> optlist |
| 1914 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 1915 | >>> args |
| 1916 | ['a1', 'a2'] |
| 1917 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1918 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1919 | The exception |
| 1920 | {\tt getopt.error = 'getopt error'} |
| 1921 | is raised when an unrecognized option is found in the argument list or |
| 1922 | when an option requiring an argument is given none. |
| 1923 | The argument to the exception is a string indicating the cause of the |
| 1924 | error. |
| 1925 | |
| 1926 | \subsection{Standard Module {\tt rand}} |
| 1927 | |
| 1928 | This module implements a pseudo-random number generator similar to |
| 1929 | {\tt rand()} |
| 1930 | in C. |
| 1931 | It defines the following functions: |
| 1932 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1933 | \funcitem{rand}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1934 | Returns an integer random number in the range [0 ... 32768). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1935 | \funcitem{choice}{s} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1936 | Returns a random element from the sequence (string, tuple or list) |
| 1937 | {\tt s.} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1938 | \funcitem{srand}{seed} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1939 | Initializes the random number generator with the given integral seed. |
| 1940 | When the module is first imported, the random number is initialized with |
| 1941 | the current time. |
| 1942 | \end{description} |
| 1943 | |
| 1944 | \subsection{Standard Module {\tt whrandom}} |
| 1945 | |
| 1946 | This module implements a Wichmann-Hill pseudo-random number generator. |
| 1947 | It defines the following functions: |
| 1948 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1949 | \funcitem{random}{} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1950 | Returns the next random floating point number in the range [0.0 ... 1.0). |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1951 | \funcitem{seed}{x, y, z} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1952 | Initializes the random number generator from the integers |
| 1953 | {\tt x}, |
| 1954 | {\tt y} |
| 1955 | and |
| 1956 | {\tt z}. |
| 1957 | When the module is first imported, the random number is initialized |
| 1958 | using values derived from the current time. |
| 1959 | \end{description} |
| 1960 | |
| 1961 | \subsection{Standard Module {\tt stdwinevents}} |
| 1962 | |
| 1963 | This module defines constants used by STDWIN for event types |
| 1964 | ({\tt WE\_ACTIVATE} etc.), command codes ({\tt WC\_LEFT} etc.) |
| 1965 | and selection types ({\tt WS\_PRIMARY} etc.). |
| 1966 | Read the file for details. |
| 1967 | Suggested usage is |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1968 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1969 | >>> from stdwinevents import * |
| 1970 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1971 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1972 | |
| 1973 | \subsection{Standard Module {\tt rect}} |
| 1974 | |
| 1975 | This module contains useful operations on rectangles. |
| 1976 | A rectangle is defined as in module |
| 1977 | {\tt stdwin}: |
| 1978 | a pair of points, where a point is a pair of integers. |
| 1979 | For example, the rectangle |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1980 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1981 | (10, 20), (90, 80) |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 1982 | \end{verbatim}\ecode |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1983 | is a rectangle whose left, top, right and bottom edges are 10, 20, 90 |
| 1984 | and 80, respectively. |
| 1985 | Note that the positive vertical axis points down (as in |
| 1986 | {\tt stdwin}). |
| 1987 | |
| 1988 | The module defines the following objects: |
| 1989 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1990 | \excitem{error}{rect.error} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1991 | %.br |
| 1992 | The exception raised by functions in this module when they detect an |
| 1993 | error. |
| 1994 | The exception argument is a string describing the problem in more |
| 1995 | detail. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 1996 | \funcitem{empty} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 1997 | %.br |
| 1998 | The rectangle returned when some operations return an empty result. |
| 1999 | This makes it possible to quickly check whether a result is empty: |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 2000 | \bcode\begin{verbatim} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2001 | >>> import rect |
| 2002 | >>> r1 = (10, 20), (90, 80) |
| 2003 | >>> r2 = (0, 0), (10, 20) |
| 2004 | >>> r3 = rect.intersect(r1, r2) |
| 2005 | >>> if r3 is rect.empty: print 'Empty intersection' |
| 2006 | Empty intersection |
| 2007 | >>> |
Guido van Rossum | d38b764 | 1991-01-25 13:26:13 +0000 | [diff] [blame] | 2008 | \end{verbatim}\ecode |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2009 | \funcitem{is\_empty}{r} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2010 | %.br |
| 2011 | Returns true if the given rectangle is empty. |
| 2012 | A rectangle |
| 2013 | {\em (left,~top), (right,~bottom)} |
| 2014 | is empty if |
| 2015 | {\em left~$\geq$~right} |
| 2016 | or |
| 2017 | {\em top~$\leq$~bottom}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2018 | \funcitem{intersect}{list} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2019 | %.br |
| 2020 | Returns the intersection of all rectangles in the list argument. |
| 2021 | It may also be called with a tuple argument or with two or more |
| 2022 | rectangles as arguments. |
| 2023 | Raises |
| 2024 | {\tt rect.error} |
| 2025 | if the list is empty. |
| 2026 | Returns |
| 2027 | {\tt rect.empty} |
| 2028 | if the intersection of the rectangles is empty. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2029 | \funcitem{union}{list} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2030 | %.br |
| 2031 | Returns the smallest rectangle that contains all non-empty rectangles in |
| 2032 | the list argument. |
| 2033 | It may also be called with a tuple argument or with two or more |
| 2034 | rectangles as arguments. |
| 2035 | Returns |
| 2036 | {\tt rect.empty} |
| 2037 | if the list is empty or all its rectangles are empty. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2038 | \funcitem{pointinrect}{point, rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2039 | %.br |
| 2040 | Returns true if the point is inside the rectangle. |
| 2041 | By definition, a point |
| 2042 | {\em (h,~v)} |
| 2043 | is inside a rectangle |
| 2044 | {\em (left,~top),} |
| 2045 | {\em (right,~bottom)} |
| 2046 | if |
| 2047 | {\em left~$\leq$~h~$<$~right} |
| 2048 | and |
| 2049 | {\em top~$\leq$~v~$<$~bottom}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2050 | \funcitem{inset(rect, }{dh, dv)} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2051 | %.br |
| 2052 | Returns a rectangle that lies inside the |
| 2053 | {\tt rect} |
| 2054 | argument by |
| 2055 | {\tt dh} |
| 2056 | pixels horizontally |
| 2057 | and |
| 2058 | {\tt dv} |
| 2059 | pixels |
| 2060 | vertically. |
| 2061 | If |
| 2062 | {\tt dh} |
| 2063 | or |
| 2064 | {\tt dv} |
| 2065 | is negative, the result lies outside |
| 2066 | {\tt rect}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2067 | \funcitem{rect2geom}{rect} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2068 | %.br |
| 2069 | Converts a rectangle to geometry representation: |
| 2070 | {\em (left,~top),} |
| 2071 | {\em (width,~height)}. |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2072 | \funcitem{geom2rect}{geom} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2073 | %.br |
| 2074 | Converts a rectangle given in geometry representation back to the |
| 2075 | standard rectangle representation |
| 2076 | {\em (left,~top),} |
| 2077 | {\em (right,~bottom)}. |
| 2078 | \end{description} |
| 2079 | |
| 2080 | \subsection{Standard Modules {\tt GL} and {\tt DEVICE}} |
| 2081 | |
| 2082 | These modules define the constants used by the Silicon Graphics |
| 2083 | {\em Graphics Library} |
| 2084 | that C programmers find in the header files |
| 2085 | {\tt <gl/gl.h>} |
| 2086 | and |
| 2087 | {\tt <gl/device.h>}. |
| 2088 | Read the module files for details. |
| 2089 | |
| 2090 | \subsection{Standard Module {\tt panel}} |
| 2091 | |
| 2092 | This module should be used instead of the built-in module |
| 2093 | {\tt pnl} |
| 2094 | to interface with the |
| 2095 | {\em Panel Library}. |
| 2096 | |
| 2097 | The module is too large to document here in its entirety. |
| 2098 | One interesting function: |
| 2099 | \begin{description} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2100 | \funcitem{defpanellist}{filename} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2101 | %.br |
| 2102 | Parses a panel description file containing S-expressions written by the |
| 2103 | {\em Panel Editor} |
| 2104 | that accompanies the Panel Library and creates the described panels. |
| 2105 | It returns a list of panel objects. |
| 2106 | \end{description} |
| 2107 | |
| 2108 | {\bf Warning:} |
| 2109 | the {\Python} interpreter will dump core if you don't create a GL window |
| 2110 | before calling |
| 2111 | {\tt panel.mkpanel()} |
| 2112 | or |
| 2113 | {\tt panel.defpanellist()}. |
| 2114 | |
Guido van Rossum | 0ed6918 | 1991-01-24 16:54:15 +0000 | [diff] [blame] | 2115 | \subsection{Standard Module {\tt panelparser}} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2116 | |
| 2117 | This module defines a self-contained parser for S-expressions as output |
| 2118 | by the Panel Editor (which is written in Scheme so it can't help writing |
| 2119 | S-expressions). |
| 2120 | The relevant function is |
Guido van Rossum | 0ed6918 | 1991-01-24 16:54:15 +0000 | [diff] [blame] | 2121 | {\tt panelparser.parse\_file(file)} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2122 | which has a file object (not a filename!) as argument and returns a list |
| 2123 | of parsed S-expressions. |
| 2124 | Each S-expression is converted into a {\Python} list, with atoms converted |
| 2125 | to {\Python} strings and sub-expressions (recursively) to {\Python} lists. |
| 2126 | For more details, read the module file. |
| 2127 | |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2128 | \section{P.M.} |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2129 | |
| 2130 | \begin{verse} |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2131 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2132 | commands |
| 2133 | |
| 2134 | cmp? |
| 2135 | |
| 2136 | *cache? |
| 2137 | |
| 2138 | localtime? |
| 2139 | |
| 2140 | calendar? |
| 2141 | |
| 2142 | \_\_dict? |
Guido van Rossum | 8ec6350 | 1991-02-19 12:53:17 +0000 | [diff] [blame] | 2143 | |
| 2144 | mac? |
| 2145 | |
Guido van Rossum | 515834a | 1991-01-22 11:45:29 +0000 | [diff] [blame] | 2146 | \end{verse} |
| 2147 | |
| 2148 | \end{document} |