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