Subsections

5.3.3 mode

It is often valuable to maintain a distinction between various phases of an application's lifecycle. The mode module calls these phases modes, and identifies four of them, given here in conceptual life-cycle order:

Mode Description
debugging The application is being actively debugged; exceptions may trigger an interactive debugger.
development The application is being actively developed; however, exceptions should not trigger interactive debugging.
staging The application is deployed in a mock-production environment.
production The application is in live use by its end users.

The expectation is that various aspects of the application--logging, exception handling, data sourcing--will adapt to the current mode. The mode is set in the PYTHONMODE environment variable. This module provides API for interacting with this variable. If PYTHONMODE is unset, it will be set to development when this module is imported.

5.3.3.1 Members

The module defines the following functions:

get( )
Return the current PYTHONMODE setting as a lowercase string; will raise EnvironmentError if the (case-insensitive) setting is not one of debugging, development, staging, or production.

set( mode)
Given a mode, set the PYTHONMODE environment variable and refresh the module's boolean members. If given a bad mode, ValueError is raised.

setAPI( )
Refresh the module's boolean members. Call this if you ever change PYTHONPATH directly in the os.environ mapping.

The module also defines a number of boolean attributes reflecting the current mode setting, including abbreviations and combinations. Uppercase versions of each of the following are also defined (e.g., DEBUGGING).

debugging, deb
True if PYTHONMODE is set to debugging.
development, dev
True if PYTHONMODE is set to development.
staging, st
True if PYTHONMODE is set to staging.
production, prod
True if PYTHONMODE is set to production.
debugging_or_development, debdev, devdeb
True if PYTHONMODE is set to debugging or development.
staging_or_production, stprod
True if PYTHONMODE is set to staging or production.

5.3.3.2 Example

Example usage:

>>> import mode
>>> mode.set('development')     # can set the mode at runtime
>>> mode.get()                  # and access the current mode
'development'
>>> mode.development            # module defines boolean constants
True
>>> mode.PRODUCTION             # uppercase versions are also defined
False
>>> mode.dev                    # as are abbreviations
True
>>> mode.DEBDEV, mode.stprod    # and combinations
(True, False)

Aspen is copyright © 2006-2007 by Chad Whitacre and contributors, and is offered under the MIT license.