Home > Articles > Programming > Python

Python Libraries

  • Print
  • + Share This

Optional Operational System

The next set of modules implements interfaces to optional operational system features. Keep in mind that these features are not available for all platforms.

signal

The signal module provides mechanisms to access POSIX signals in order to let the programmer set her own signal handlers for asynchronous events.

A good example is the case when it is necessary to monitor the users, checking whether they press CTRL+C to stop the execution of a program. Although Python provides default handlers, you can overwrite them by creating your own.

import signal, sys
def signal_handler(signal, frame):
 print "You have pressed CTRL+C"
 signal.signal(signal.SIGINT, signal.SIG_IGN)
 print "Now, you can\'t stop the script with CTRL+C " 
 "for the next 10 seconds!"
 signal.signal(signal.SIGALRM, alarm_handler)
 signal.alarm(10)
 while 1:
  print "I am looping"

def alarm_handler(signal, frame):
 print "Now you can leave the program"
 sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print "Press CTRL+C"
while 1:
 continue

Some of the available signals you can use are as follows:

SIGALRM Alarm
SIGCONT Continue
SIGING Terminal interrupt character
SIGQUIT Terminal Quit character
SIGTERM Termination
SIG_IGN Signal handler that ignores a signal

socket

The socket module provides access to a low-level BSD socket-style network interface.

See Chapter 10, "Basic Network Background," for details.

select

The select module is used to implement polling and to multiplex processing across multiple I/O streams without using threads or subprocesses. It provides access to the BSD select() function interface, available in most operating systems.

On windows it only works for sockets. On UNIX, it is used for pipes, sockets, files, and so on.

See Chapter 10 for details.

thread

The thread module supports lightweight process threads. It offers a low-level interface for working with multiple threads.

See Chapter 9 for details.

threading

The threading module provides high-level threading interfaces on top of the thread module.

See Chapter 9 for details.

Queue

The Queue module is a synchronized queue class that is used in thread programming to move Python objects between multiple threads.

See Chapter 9 for details.

anydbm

The anydbm module is a generic dbm-style interface to access variants of the dbm database.

See Chapter 8 for details.

dumbdbm

The dumbdbm module is a simple, portable, and slow database implemented entirely in Python.

See Chapter 8 for details.

dbhash

The dbhash module provides a function that offers a dbm-style interface to access the BSD database library.

See Chapter 8 for details.

whichdb

The whichdb module provides a function that guesses which dbm module (dbm, gdbm, or dbhash) should be used to open a specific database.

See Chapter 8 for details.

bsddb

The bsddb module provides an interface to access routines from the Berkeley db library.

See Chapter 8 for details.

zlib

The zlib module provides functions that allow compression and decompression using the zlib library. The compression that is provided by this module is compatible with gzip.

For more details check out the zlib library home page at http://www.cdrom.com/pub/infozip/lib.

gzip

The gzip module offers support for gzip files. This module provides functions that allow compression and decompression using the GNU compression program gzip.

This module has a class named GzipFile that can be used to read and write files compatible with the GNU gzip program. The objects that are generated by this class behave just like file objects. The only exception is that the seek and tell methods aren't part of the standard implementation.

>>> import gzip
>>> gzipfile = gzip.GzipFile("backup.gz")
>>> contents = gzipfile.read()
>>> print contents

rlcompleter

The rlcompleter module provides a completion function for the readline module.

The readline module is a UNIX module that is automatically imported by rlcompleter. It uses a compatible GNU readline library to activate input editing on UNIX.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.