blob: db69449d7e02b166c1431b6d5a0cc408ef4b2b46 [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3=========================
4Library and Extension FAQ
5=========================
6
7.. contents::
8
9General Library Questions
10=========================
11
12How do I find a module or application to perform task X?
13--------------------------------------------------------
14
15Check :ref:`the Library Reference <library-index>` to see if there's a relevant
16standard library module. (Eventually you'll learn what's in the standard
17library and will able to skip this step.)
18
19Search the `Python Package Index <http://pypi.python.org/pypi>`_.
20
21Next, check the `Vaults of Parnassus <http://www.vex.net/parnassus/>`_, an older
22index of packages.
23
24Finally, try `Google <http://www.google.com>`_ or other Web search engine.
25Searching for "Python" plus a keyword or two for your topic of interest will
26usually find something helpful.
27
28
29Where is the math.py (socket.py, regex.py, etc.) source file?
30-------------------------------------------------------------
31
32If you can't find a source file for a module it may be a builtin or dynamically
33loaded module implemented in C, C++ or other compiled language. In this case
34you may not have the source file or it may be something like mathmodule.c,
35somewhere in a C source directory (not on the Python Path).
36
37There are (at least) three kinds of modules in Python:
38
391) modules written in Python (.py);
402) modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);
413) modules written in C and linked with the interpreter; to get a list of these,
42 type::
43
44 import sys
45 print sys.builtin_module_names
46
47
48How do I make a Python script executable on Unix?
49-------------------------------------------------
50
51You need to do two things: the script file's mode must be executable and the
52first line must begin with ``#!`` followed by the path of the Python
53interpreter.
54
55The first is done by executing ``chmod +x scriptfile`` or perhaps ``chmod 755
56scriptfile``.
57
58The second can be done in a number of ways. The most straightforward way is to
59write ::
60
61 #!/usr/local/bin/python
62
63as the very first line of your file, using the pathname for where the Python
64interpreter is installed on your platform.
65
66If you would like the script to be independent of where the Python interpreter
67lives, you can use the "env" program. Almost all Unix variants support the
68following, assuming the python interpreter is in a directory on the user's
69$PATH::
70
71 #!/usr/bin/env python
72
73*Don't* do this for CGI scripts. The $PATH variable for CGI scripts is often
74very minimal, so you need to use the actual absolute pathname of the
75interpreter.
76
77Occasionally, a user's environment is so full that the /usr/bin/env program
78fails; or there's no env program at all. In that case, you can try the
79following hack (due to Alex Rezinsky)::
80
81 #! /bin/sh
82 """:"
83 exec python $0 ${1+"$@"}
84 """
85
86The minor disadvantage is that this defines the script's __doc__ string.
87However, you can fix that by adding ::
88
89 __doc__ = """...Whatever..."""
90
91
92
93Is there a curses/termcap package for Python?
94---------------------------------------------
95
96.. XXX curses *is* built by default, isn't it?
97
98For Unix variants: The standard Python source distribution comes with a curses
99module in the ``Modules/`` subdirectory, though it's not compiled by default
100(note that this is not available in the Windows distribution -- there is no
101curses module for Windows).
102
103The curses module supports basic curses features as well as many additional
104functions from ncurses and SYSV curses such as colour, alternative character set
105support, pads, and mouse support. This means the module isn't compatible with
106operating systems that only have BSD curses, but there don't seem to be any
107currently maintained OSes that fall into this category.
108
109For Windows: use `the consolelib module
110<http://effbot.org/zone/console-index.htm>`_.
111
112
113Is there an equivalent to C's onexit() in Python?
114-------------------------------------------------
115
116The :mod:`atexit` module provides a register function that is similar to C's
117onexit.
118
119
120Why don't my signal handlers work?
121----------------------------------
122
123The most common problem is that the signal handler is declared with the wrong
124argument list. It is called as ::
125
126 handler(signum, frame)
127
128so it should be declared with two arguments::
129
130 def handler(signum, frame):
131 ...
132
133
134Common tasks
135============
136
137How do I test a Python program or component?
138--------------------------------------------
139
140Python comes with two testing frameworks. The :mod:`doctest` module finds
141examples in the docstrings for a module and runs them, comparing the output with
142the expected output given in the docstring.
143
144The :mod:`unittest` module is a fancier testing framework modelled on Java and
145Smalltalk testing frameworks.
146
147For testing, it helps to write the program so that it may be easily tested by
148using good modular design. Your program should have almost all functionality
149encapsulated in either functions or class methods -- and this sometimes has the
150surprising and delightful effect of making the program run faster (because local
151variable accesses are faster than global accesses). Furthermore the program
152should avoid depending on mutating global variables, since this makes testing
153much more difficult to do.
154
155The "global main logic" of your program may be as simple as ::
156
157 if __name__ == "__main__":
158 main_logic()
159
160at the bottom of the main module of your program.
161
162Once your program is organized as a tractable collection of functions and class
163behaviours you should write test functions that exercise the behaviours. A test
164suite can be associated with each module which automates a sequence of tests.
165This sounds like a lot of work, but since Python is so terse and flexible it's
166surprisingly easy. You can make coding much more pleasant and fun by writing
167your test functions in parallel with the "production code", since this makes it
168easy to find bugs and even design flaws earlier.
169
170"Support modules" that are not intended to be the main module of a program may
171include a self-test of the module. ::
172
173 if __name__ == "__main__":
174 self_test()
175
176Even programs that interact with complex external interfaces may be tested when
177the external interfaces are unavailable by using "fake" interfaces implemented
178in Python.
179
180
181How do I create documentation from doc strings?
182-----------------------------------------------
183
184.. XXX mention Sphinx/epydoc
185
186The :mod:`pydoc` module can create HTML from the doc strings in your Python
187source code. An alternative is `pythondoc
188<http://starship.python.net/crew/danilo/pythondoc/>`_.
189
190
191How do I get a single keypress at a time?
192-----------------------------------------
193
194For Unix variants: There are several solutions. It's straightforward to do this
195using curses, but curses is a fairly large module to learn. Here's a solution
196without curses::
197
198 import termios, fcntl, sys, os
199 fd = sys.stdin.fileno()
200
201 oldterm = termios.tcgetattr(fd)
202 newattr = termios.tcgetattr(fd)
203 newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
204 termios.tcsetattr(fd, termios.TCSANOW, newattr)
205
206 oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
207 fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
208
209 try:
210 while 1:
211 try:
212 c = sys.stdin.read(1)
213 print "Got character", `c`
214 except IOError: pass
215 finally:
216 termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
217 fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
218
219You need the :mod:`termios` and the :mod:`fcntl` module for any of this to work,
220and I've only tried it on Linux, though it should work elsewhere. In this code,
221characters are read and printed one at a time.
222
223:func:`termios.tcsetattr` turns off stdin's echoing and disables canonical mode.
224:func:`fcntl.fnctl` is used to obtain stdin's file descriptor flags and modify
225them for non-blocking mode. Since reading stdin when it is empty results in an
226:exc:`IOError`, this error is caught and ignored.
227
228
229Threads
230=======
231
232How do I program using threads?
233-------------------------------
234
Georg Brandld404fa62009-10-13 16:55:12 +0000235Be sure to use the :mod:`threading` module and not the :mod:`_thread` module.
Georg Brandld7413152009-10-11 21:25:26 +0000236The :mod:`threading` module builds convenient abstractions on top of the
Georg Brandld404fa62009-10-13 16:55:12 +0000237low-level primitives provided by the :mod:`_thread` module.
Georg Brandld7413152009-10-11 21:25:26 +0000238
239Aahz has a set of slides from his threading tutorial that are helpful; see
240http://starship.python.net/crew/aahz/OSCON2001/.
241
242
243None of my threads seem to run: why?
244------------------------------------
245
246As soon as the main thread exits, all threads are killed. Your main thread is
247running too quickly, giving the threads no time to do any work.
248
249A simple fix is to add a sleep to the end of the program that's long enough for
250all the threads to finish::
251
252 import threading, time
253
254 def thread_task(name, n):
255 for i in range(n): print name, i
256
257 for i in range(10):
258 T = threading.Thread(target=thread_task, args=(str(i), i))
259 T.start()
260
261 time.sleep(10) # <----------------------------!
262
263But now (on many platforms) the threads don't run in parallel, but appear to run
264sequentially, one at a time! The reason is that the OS thread scheduler doesn't
265start a new thread until the previous thread is blocked.
266
267A simple fix is to add a tiny sleep to the start of the run function::
268
269 def thread_task(name, n):
270 time.sleep(0.001) # <---------------------!
271 for i in range(n): print name, i
272
273 for i in range(10):
274 T = threading.Thread(target=thread_task, args=(str(i), i))
275 T.start()
276
277 time.sleep(10)
278
279Instead of trying to guess how long a :func:`time.sleep` delay will be enough,
280it's better to use some kind of semaphore mechanism. One idea is to use the
Georg Brandld404fa62009-10-13 16:55:12 +0000281:mod:`queue` module to create a queue object, let each thread append a token to
Georg Brandld7413152009-10-11 21:25:26 +0000282the queue when it finishes, and let the main thread read as many tokens from the
283queue as there are threads.
284
285
286How do I parcel out work among a bunch of worker threads?
287---------------------------------------------------------
288
Georg Brandld404fa62009-10-13 16:55:12 +0000289Use the :mod:`queue` module to create a queue containing a list of jobs. The
290:class:`~queue.Queue` class maintains a list of objects with ``.put(obj)`` to
Georg Brandld7413152009-10-11 21:25:26 +0000291add an item to the queue and ``.get()`` to return an item. The class will take
292care of the locking necessary to ensure that each job is handed out exactly
293once.
294
295Here's a trivial example::
296
297 import threading, Queue, time
298
299 # The worker thread gets jobs off the queue. When the queue is empty, it
300 # assumes there will be no more work and exits.
301 # (Realistically workers will run until terminated.)
302 def worker ():
303 print 'Running worker'
304 time.sleep(0.1)
305 while True:
306 try:
307 arg = q.get(block=False)
308 except Queue.Empty:
309 print 'Worker', threading.currentThread(),
310 print 'queue empty'
311 break
312 else:
313 print 'Worker', threading.currentThread(),
314 print 'running with argument', arg
315 time.sleep(0.5)
316
317 # Create queue
318 q = Queue.Queue()
319
320 # Start a pool of 5 workers
321 for i in range(5):
322 t = threading.Thread(target=worker, name='worker %i' % (i+1))
323 t.start()
324
325 # Begin adding work to the queue
326 for i in range(50):
327 q.put(i)
328
329 # Give threads time to run
330 print 'Main thread sleeping'
331 time.sleep(5)
332
333When run, this will produce the following output:
334
335 Running worker
336 Running worker
337 Running worker
338 Running worker
339 Running worker
340 Main thread sleeping
341 Worker <Thread(worker 1, started)> running with argument 0
342 Worker <Thread(worker 2, started)> running with argument 1
343 Worker <Thread(worker 3, started)> running with argument 2
344 Worker <Thread(worker 4, started)> running with argument 3
345 Worker <Thread(worker 5, started)> running with argument 4
346 Worker <Thread(worker 1, started)> running with argument 5
347 ...
348
349Consult the module's documentation for more details; the ``Queue`` class
350provides a featureful interface.
351
352
353What kinds of global value mutation are thread-safe?
354----------------------------------------------------
355
356A global interpreter lock (GIL) is used internally to ensure that only one
357thread runs in the Python VM at a time. In general, Python offers to switch
358among threads only between bytecode instructions; how frequently it switches can
359be set via :func:`sys.setcheckinterval`. Each bytecode instruction and
360therefore all the C implementation code reached from each instruction is
361therefore atomic from the point of view of a Python program.
362
363In theory, this means an exact accounting requires an exact understanding of the
364PVM bytecode implementation. In practice, it means that operations on shared
365variables of builtin data types (ints, lists, dicts, etc) that "look atomic"
366really are.
367
368For example, the following operations are all atomic (L, L1, L2 are lists, D,
369D1, D2 are dicts, x, y are objects, i, j are ints)::
370
371 L.append(x)
372 L1.extend(L2)
373 x = L[i]
374 x = L.pop()
375 L1[i:j] = L2
376 L.sort()
377 x = y
378 x.field = y
379 D[x] = y
380 D1.update(D2)
381 D.keys()
382
383These aren't::
384
385 i = i+1
386 L.append(L[-1])
387 L[i] = L[j]
388 D[x] = D[x] + 1
389
390Operations that replace other objects may invoke those other objects'
391:meth:`__del__` method when their reference count reaches zero, and that can
392affect things. This is especially true for the mass updates to dictionaries and
393lists. When in doubt, use a mutex!
394
395
396Can't we get rid of the Global Interpreter Lock?
397------------------------------------------------
398
399.. XXX mention multiprocessing
400
401The Global Interpreter Lock (GIL) is often seen as a hindrance to Python's
402deployment on high-end multiprocessor server machines, because a multi-threaded
403Python program effectively only uses one CPU, due to the insistence that
404(almost) all Python code can only run while the GIL is held.
405
406Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive
407patch set (the "free threading" patches) that removed the GIL and replaced it
408with fine-grained locking. Unfortunately, even on Windows (where locks are very
409efficient) this ran ordinary Python code about twice as slow as the interpreter
410using the GIL. On Linux the performance loss was even worse because pthread
411locks aren't as efficient.
412
413Since then, the idea of getting rid of the GIL has occasionally come up but
414nobody has found a way to deal with the expected slowdown, and users who don't
415use threads would not be happy if their code ran at half at the speed. Greg's
416free threading patch set has not been kept up-to-date for later Python versions.
417
418This doesn't mean that you can't make good use of Python on multi-CPU machines!
419You just have to be creative with dividing the work up between multiple
420*processes* rather than multiple *threads*. Judicious use of C extensions will
421also help; if you use a C extension to perform a time-consuming task, the
422extension can release the GIL while the thread of execution is in the C code and
423allow other threads to get some work done.
424
425It has been suggested that the GIL should be a per-interpreter-state lock rather
426than truly global; interpreters then wouldn't be able to share objects.
427Unfortunately, this isn't likely to happen either. It would be a tremendous
428amount of work, because many object implementations currently have global state.
429For example, small integers and short strings are cached; these caches would
430have to be moved to the interpreter state. Other object types have their own
431free list; these free lists would have to be moved to the interpreter state.
432And so on.
433
434And I doubt that it can even be done in finite time, because the same problem
435exists for 3rd party extensions. It is likely that 3rd party extensions are
436being written at a faster rate than you can convert them to store all their
437global state in the interpreter state.
438
439And finally, once you have multiple interpreters not sharing any state, what
440have you gained over running each interpreter in a separate process?
441
442
443Input and Output
444================
445
446How do I delete a file? (And other file questions...)
447-----------------------------------------------------
448
449Use ``os.remove(filename)`` or ``os.unlink(filename)``; for documentation, see
450the :mod:`os` module. The two functions are identical; :func:`unlink` is simply
451the name of the Unix system call for this function.
452
453To remove a directory, use :func:`os.rmdir`; use :func:`os.mkdir` to create one.
454``os.makedirs(path)`` will create any intermediate directories in ``path`` that
455don't exist. ``os.removedirs(path)`` will remove intermediate directories as
456long as they're empty; if you want to delete an entire directory tree and its
457contents, use :func:`shutil.rmtree`.
458
459To rename a file, use ``os.rename(old_path, new_path)``.
460
461To truncate a file, open it using ``f = open(filename, "r+")``, and use
462``f.truncate(offset)``; offset defaults to the current seek position. There's
463also ```os.ftruncate(fd, offset)`` for files opened with :func:`os.open`, where
464``fd`` is the file descriptor (a small integer).
465
466The :mod:`shutil` module also contains a number of functions to work on files
467including :func:`~shutil.copyfile`, :func:`~shutil.copytree`, and
468:func:`~shutil.rmtree`.
469
470
471How do I copy a file?
472---------------------
473
474The :mod:`shutil` module contains a :func:`~shutil.copyfile` function. Note
475that on MacOS 9 it doesn't copy the resource fork and Finder info.
476
477
478How do I read (or write) binary data?
479-------------------------------------
480
481To read or write complex binary data formats, it's best to use the :mod:`struct`
482module. It allows you to take a string containing binary data (usually numbers)
483and convert it to Python objects; and vice versa.
484
485For example, the following code reads two 2-byte integers and one 4-byte integer
486in big-endian format from a file::
487
488 import struct
489
490 f = open(filename, "rb") # Open in binary mode for portability
491 s = f.read(8)
492 x, y, z = struct.unpack(">hhl", s)
493
494The '>' in the format string forces big-endian data; the letter 'h' reads one
495"short integer" (2 bytes), and 'l' reads one "long integer" (4 bytes) from the
496string.
497
498For data that is more regular (e.g. a homogeneous list of ints or thefloats),
499you can also use the :mod:`array` module.
500
501
502I can't seem to use os.read() on a pipe created with os.popen(); why?
503---------------------------------------------------------------------
504
505:func:`os.read` is a low-level function which takes a file descriptor, a small
506integer representing the opened file. :func:`os.popen` creates a high-level
507file object, the same type returned by the builtin :func:`open` function. Thus,
508to read n bytes from a pipe p created with :func:`os.popen`, you need to use
509``p.read(n)``.
510
511
512How do I run a subprocess with pipes connected to both input and output?
513------------------------------------------------------------------------
514
515.. XXX update to use subprocess
516
517Use the :mod:`popen2` module. For example::
518
519 import popen2
520 fromchild, tochild = popen2.popen2("command")
521 tochild.write("input\n")
522 tochild.flush()
523 output = fromchild.readline()
524
525Warning: in general it is unwise to do this because you can easily cause a
526deadlock where your process is blocked waiting for output from the child while
527the child is blocked waiting for input from you. This can be caused because the
528parent expects the child to output more text than it does, or it can be caused
529by data being stuck in stdio buffers due to lack of flushing. The Python parent
530can of course explicitly flush the data it sends to the child before it reads
531any output, but if the child is a naive C program it may have been written to
532never explicitly flush its output, even if it is interactive, since flushing is
533normally automatic.
534
535Note that a deadlock is also possible if you use :func:`popen3` to read stdout
536and stderr. If one of the two is too large for the internal buffer (increasing
537the buffer size does not help) and you ``read()`` the other one first, there is
538a deadlock, too.
539
540Note on a bug in popen2: unless your program calls ``wait()`` or ``waitpid()``,
541finished child processes are never removed, and eventually calls to popen2 will
542fail because of a limit on the number of child processes. Calling
543:func:`os.waitpid` with the :data:`os.WNOHANG` option can prevent this; a good
544place to insert such a call would be before calling ``popen2`` again.
545
546In many cases, all you really need is to run some data through a command and get
547the result back. Unless the amount of data is very large, the easiest way to do
548this is to write it to a temporary file and run the command with that temporary
549file as input. The standard module :mod:`tempfile` exports a ``mktemp()``
550function to generate unique temporary file names. ::
551
552 import tempfile
553 import os
554
555 class Popen3:
556 """
557 This is a deadlock-safe version of popen that returns
558 an object with errorlevel, out (a string) and err (a string).
559 (capturestderr may not work under windows.)
560 Example: print Popen3('grep spam','\n\nhere spam\n\n').out
561 """
562 def __init__(self,command,input=None,capturestderr=None):
563 outfile=tempfile.mktemp()
564 command="( %s ) > %s" % (command,outfile)
565 if input:
566 infile=tempfile.mktemp()
567 open(infile,"w").write(input)
568 command=command+" <"+infile
569 if capturestderr:
570 errfile=tempfile.mktemp()
571 command=command+" 2>"+errfile
572 self.errorlevel=os.system(command) >> 8
573 self.out=open(outfile,"r").read()
574 os.remove(outfile)
575 if input:
576 os.remove(infile)
577 if capturestderr:
578 self.err=open(errfile,"r").read()
579 os.remove(errfile)
580
581Note that many interactive programs (e.g. vi) don't work well with pipes
582substituted for standard input and output. You will have to use pseudo ttys
583("ptys") instead of pipes. Or you can use a Python interface to Don Libes'
584"expect" library. A Python extension that interfaces to expect is called "expy"
585and available from http://expectpy.sourceforge.net. A pure Python solution that
586works like expect is ` pexpect <http://pexpect.sourceforge.net>`_.
587
588
589How do I access the serial (RS232) port?
590----------------------------------------
591
592For Win32, POSIX (Linux, BSD, etc.), Jython:
593
594 http://pyserial.sourceforge.net
595
596For Unix, see a Usenet post by Mitch Chapman:
597
598 http://groups.google.com/groups?selm=34A04430.CF9@ohioee.com
599
600
601Why doesn't closing sys.stdout (stdin, stderr) really close it?
602---------------------------------------------------------------
603
604Python file objects are a high-level layer of abstraction on top of C streams,
605which in turn are a medium-level layer of abstraction on top of (among other
606things) low-level C file descriptors.
607
608For most file objects you create in Python via the builtin ``file`` constructor,
609``f.close()`` marks the Python file object as being closed from Python's point
610of view, and also arranges to close the underlying C stream. This also happens
611automatically in f's destructor, when f becomes garbage.
612
613But stdin, stdout and stderr are treated specially by Python, because of the
614special status also given to them by C. Running ``sys.stdout.close()`` marks
615the Python-level file object as being closed, but does *not* close the
616associated C stream.
617
618To close the underlying C stream for one of these three, you should first be
619sure that's what you really want to do (e.g., you may confuse extension modules
620trying to do I/O). If it is, use os.close::
621
622 os.close(0) # close C's stdin stream
623 os.close(1) # close C's stdout stream
624 os.close(2) # close C's stderr stream
625
626
627Network/Internet Programming
628============================
629
630What WWW tools are there for Python?
631------------------------------------
632
633See the chapters titled :ref:`internet` and :ref:`netdata` in the Library
634Reference Manual. Python has many modules that will help you build server-side
635and client-side web systems.
636
637.. XXX check if wiki page is still up to date
638
639A summary of available frameworks is maintained by Paul Boddie at
640http://wiki.python.org/moin/WebProgramming .
641
642Cameron Laird maintains a useful set of pages about Python web technologies at
643http://phaseit.net/claird/comp.lang.python/web_python.
644
645
646How can I mimic CGI form submission (METHOD=POST)?
647--------------------------------------------------
648
649I would like to retrieve web pages that are the result of POSTing a form. Is
650there existing code that would let me do this easily?
651
652Yes. Here's a simple example that uses httplib::
653
654 #!/usr/local/bin/python
655
656 import httplib, sys, time
657
658 ### build the query string
659 qs = "First=Josephine&MI=Q&Last=Public"
660
661 ### connect and send the server a path
662 httpobj = httplib.HTTP('www.some-server.out-there', 80)
663 httpobj.putrequest('POST', '/cgi-bin/some-cgi-script')
664 ### now generate the rest of the HTTP headers...
665 httpobj.putheader('Accept', '*/*')
666 httpobj.putheader('Connection', 'Keep-Alive')
667 httpobj.putheader('Content-type', 'application/x-www-form-urlencoded')
668 httpobj.putheader('Content-length', '%d' % len(qs))
669 httpobj.endheaders()
670 httpobj.send(qs)
671 ### find out what the server said in response...
672 reply, msg, hdrs = httpobj.getreply()
673 if reply != 200:
674 sys.stdout.write(httpobj.getfile().read())
675
676Note that in general for URL-encoded POST operations, query strings must be
677quoted by using :func:`urllib.quote`. For example to send name="Guy Steele,
678Jr."::
679
680 >>> from urllib import quote
681 >>> x = quote("Guy Steele, Jr.")
682 >>> x
683 'Guy%20Steele,%20Jr.'
684 >>> query_string = "name="+x
685 >>> query_string
686 'name=Guy%20Steele,%20Jr.'
687
688
689What module should I use to help with generating HTML?
690------------------------------------------------------
691
692.. XXX add modern template languages
693
694There are many different modules available:
695
696* HTMLgen is a class library of objects corresponding to all the HTML 3.2 markup
697 tags. It's used when you are writing in Python and wish to synthesize HTML
698 pages for generating a web or for CGI forms, etc.
699
700* DocumentTemplate and Zope Page Templates are two different systems that are
701 part of Zope.
702
703* Quixote's PTL uses Python syntax to assemble strings of text.
704
705Consult the `Web Programming wiki pages
706<http://wiki.python.org/moin/WebProgramming>`_ for more links.
707
708
709How do I send mail from a Python script?
710----------------------------------------
711
712Use the standard library module :mod:`smtplib`.
713
714Here's a very simple interactive mail sender that uses it. This method will
715work on any host that supports an SMTP listener. ::
716
717 import sys, smtplib
718
719 fromaddr = raw_input("From: ")
720 toaddrs = raw_input("To: ").split(',')
721 print "Enter message, end with ^D:"
722 msg = ''
723 while True:
724 line = sys.stdin.readline()
725 if not line:
726 break
727 msg += line
728
729 # The actual mail send
730 server = smtplib.SMTP('localhost')
731 server.sendmail(fromaddr, toaddrs, msg)
732 server.quit()
733
734A Unix-only alternative uses sendmail. The location of the sendmail program
735varies between systems; sometimes it is ``/usr/lib/sendmail``, sometime
736``/usr/sbin/sendmail``. The sendmail manual page will help you out. Here's
737some sample code::
738
739 SENDMAIL = "/usr/sbin/sendmail" # sendmail location
740 import os
741 p = os.popen("%s -t -i" % SENDMAIL, "w")
742 p.write("To: receiver@example.com\n")
743 p.write("Subject: test\n")
744 p.write("\n") # blank line separating headers from body
745 p.write("Some text\n")
746 p.write("some more text\n")
747 sts = p.close()
748 if sts != 0:
749 print "Sendmail exit status", sts
750
751
752How do I avoid blocking in the connect() method of a socket?
753------------------------------------------------------------
754
755The select module is commonly used to help with asynchronous I/O on sockets.
756
757To prevent the TCP connect from blocking, you can set the socket to non-blocking
758mode. Then when you do the ``connect()``, you will either connect immediately
759(unlikely) or get an exception that contains the error number as ``.errno``.
760``errno.EINPROGRESS`` indicates that the connection is in progress, but hasn't
761finished yet. Different OSes will return different values, so you're going to
762have to check what's returned on your system.
763
764You can use the ``connect_ex()`` method to avoid creating an exception. It will
765just return the errno value. To poll, you can call ``connect_ex()`` again later
766-- 0 or ``errno.EISCONN`` indicate that you're connected -- or you can pass this
767socket to select to check if it's writable.
768
769
770Databases
771=========
772
773Are there any interfaces to database packages in Python?
774--------------------------------------------------------
775
776Yes.
777
Georg Brandld404fa62009-10-13 16:55:12 +0000778Interfaces to disk-based hashes such as :mod:`DBM <dbm.ndbm>` and :mod:`GDBM
779<dbm.gnu>` are also included with standard Python. There is also the
780:mod:`sqlite3` module, which provides a lightweight disk-based relational
781database.
Georg Brandld7413152009-10-11 21:25:26 +0000782
783Support for most relational databases is available. See the
784`DatabaseProgramming wiki page
785<http://wiki.python.org/moin/DatabaseProgramming>`_ for details.
786
787
788How do you implement persistent objects in Python?
789--------------------------------------------------
790
791The :mod:`pickle` library module solves this in a very general way (though you
792still can't store things like open files, sockets or windows), and the
793:mod:`shelve` library module uses pickle and (g)dbm to create persistent
Georg Brandld404fa62009-10-13 16:55:12 +0000794mappings containing arbitrary Python objects.
Georg Brandld7413152009-10-11 21:25:26 +0000795
796A more awkward way of doing things is to use pickle's little sister, marshal.
797The :mod:`marshal` module provides very fast ways to store noncircular basic
798Python types to files and strings, and back again. Although marshal does not do
799fancy things like store instances or handle shared references properly, it does
800run extremely fast. For example loading a half megabyte of data may take less
801than a third of a second. This often beats doing something more complex and
802general such as using gdbm with pickle/shelve.
803
804
805Why is cPickle so slow?
806-----------------------
807
808.. XXX update this, default protocol is 2/3
809
810The default format used by the pickle module is a slow one that results in
811readable pickles. Making it the default, but it would break backward
812compatibility::
813
814 largeString = 'z' * (100 * 1024)
815 myPickle = cPickle.dumps(largeString, protocol=1)
816
817
818If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?
819------------------------------------------------------------------------------------------
820
821Databases opened for write access with the bsddb module (and often by the anydbm
822module, since it will preferentially use bsddb) must explicitly be closed using
823the ``.close()`` method of the database. The underlying library caches database
824contents which need to be converted to on-disk form and written.
825
826If you have initialized a new bsddb database but not written anything to it
827before the program crashes, you will often wind up with a zero-length file and
828encounter an exception the next time the file is opened.
829
830
831I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, 'Invalid argument'). Help! How can I restore my data?
832----------------------------------------------------------------------------------------------------------------------------
833
834Don't panic! Your data is probably intact. The most frequent cause for the error
835is that you tried to open an earlier Berkeley DB file with a later version of
836the Berkeley DB library.
837
838Many Linux systems now have all three versions of Berkeley DB available. If you
839are migrating from version 1 to a newer version use db_dump185 to dump a plain
840text version of the database. If you are migrating from version 2 to version 3
841use db2_dump to create a plain text version of the database. In either case,
842use db_load to create a new native database for the latest version installed on
843your computer. If you have version 3 of Berkeley DB installed, you should be
844able to use db2_load to create a native version 2 database.
845
846You should move away from Berkeley DB version 1 files because the hash file code
847contains known bugs that can corrupt your data.
848
849
850Mathematics and Numerics
851========================
852
853How do I generate random numbers in Python?
854-------------------------------------------
855
856The standard module :mod:`random` implements a random number generator. Usage
857is simple::
858
859 import random
860 random.random()
861
862This returns a random floating point number in the range [0, 1).
863
864There are also many other specialized generators in this module, such as:
865
866* ``randrange(a, b)`` chooses an integer in the range [a, b).
867* ``uniform(a, b)`` chooses a floating point number in the range [a, b).
868* ``normalvariate(mean, sdev)`` samples the normal (Gaussian) distribution.
869
870Some higher-level functions operate on sequences directly, such as:
871
872* ``choice(S)`` chooses random element from a given sequence
873* ``shuffle(L)`` shuffles a list in-place, i.e. permutes it randomly
874
875There's also a ``Random`` class you can instantiate to create independent
876multiple random number generators.