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