[Python-Dev] elementtree in stdlib

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 21:35:21 CEST 2006


Trent Mick wrote:

> That is the current state.

which reminds that maybe it's time to add an import helper to
the standard library, so you can do

    stringio = import_search("cStringIO", "StringIO")
    ET = import_search("lxml.etree", "cElementTree", "xml.etree.cElementTree")
    db = import_search("superdb", "sqlite3", "fancydb", "dumbdb")

etc. without having to type in

    for mod in ("cStringIO", "StringIO"):
        try:
            m = __import__(mod)
            for p in mod.split(".")[1:]:
                m = getattr(m, p, None)
                if m is None:
                    raise ImportError
            return m
        except ImportError:
            pass
    else:
        raise ImportError(mod)

all the time (or create those horridly nested try-except constructs).

or perhaps

    try:
        import cStringIO as stringio
    retry ImportError:
        import StringIO as stringio
    except ImportError:
        print "didn't work!"

would be a solution (sorry, wrong list).

</F>





More information about the Python-Dev mailing list