[newbie] conditional imports

Matteo Dell'Amico della at toglimi.linux.it
Thu May 27 11:34:40 EDT 2004


vincent Salaun wrote:
> - i've written a python xml parser using minidom
> - i've written a Jython xml parser using java dom classes - because 
> minidom doesn't work with jython :( -

I'd make a jython module to extend the java DOM API, making them work 
like minidom (or the subset of minidom that you really need). After 
that, you could contact the jython maintainers: I'd think they'd be 
happy to know there's a functional minidom module for Jython :-)

Then, the conditional import would become to

try:
     from xml.dom import minidom
except ImportError:
     # We couldn't import xml.dom.minidom, so we are probably using
     #Jython
     from my.jython.package import minidom

Then, you can go on happily using the minidom without caring whether 
it's the module you wrote or it's the CPython one.

-- 
Ciao,
Matteo



More information about the Python-list mailing list