boodebr A little library from boodebr.org


files/feed-icon-14x14.png Recently Edited
boodebr.pyconfig edit
frank, 04 August 2008 (created 03 October 2007)
Tags: pyconfig config
This module essentially performs introspection on the Python interpreter itself, to discover which language features are available.

Usage notes:


The recommended way to use this module is:
from boodebr.pyconfig import pyconfig  # or 'import *'

if pyconfig.Have_Iterators():
     ...


Using the 'pyconfig' object like this auto-caches results so you can use the function calls inline without a speed penalty of rerunning the test each time.

Background/rationale:


At first glance, this module seems odd — why not just check sys.version_info? Well, for example:

  • You could be running under a nonstandard/alternative Python implementation (e.g. Jython, or maybe a cut-down embedded version that eliminated some features) and you want to dynamically figure out what is available.

  • It seems more self-documenting to write something like:
        
    if Have_Iterators():
         ... do something with iterators ...
    Than to write:
        
    if sys.version_info[0] >= 2 and sys.version_info[1] >= 2:
         ... do something with iterators ...
    In other words, your code now says "here is the capability I need", making it more maintainable down the road than a hardcoded version match. Plus, it is more robust in the presence of a nonstandard interpreter.

  • You're writing an installer, and want to pick different modules to install, based on platform.