[newbie] conditional imports

V

vincent Salaun

hi all,

here is my pb :

- 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 :( -

Only one generic parser would have been the best solution because it
will be used by a Java application on one side and by a C++ application
on the other side but i've failed to find a python xml lib working both
in python and jython ....

So, now i'd like to put these 2 modules together as much as possible, to
finally have only one, if possible...
To do that, i thought using python shortcuts (actually, the content of
these 2 modules is quite similar, only method names change)

Here is my idea:

#############################################################
#module myBothJavaAndPyParser :

#if the module is used by the Java app ::
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory
from org.w3c.dom import *

factory=DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
parseString=self.builder.parse

#else:
from xml.dom import minidom,Node
parseString=minidom.parseString

class myParser:
def __init__(self):
#...

def parse(self,toParse):
return parseString(toParse)

.....
#############################################################

But when i instanciate this class, i don't know how to do to know which
imports and aliases must be done ... i tried to put the imports in the
__init__ (and using a boolean) but then i can't access to aliases
Any suggestions ?

And i have another problem concerning shortcuts : how to make a shortcut
on an attribute ?
For example :
Using java, for a Node, we have the getParentNode() method :
myNode.getParentNode()
In python, it's an attribute : myNode.parentNode

How to do to make a shortcut in this specific case ?

And if a make the shortcut in another way :
#if used by Java :
# imports ...
parentNode=Node.getParentNode

But now, i will uses this syntax : myNode.parentNode() .... and it won't
work anymore using python imports ....
i'm new to python, please help !!

thx in advance ;)
vince
 
P

Peter Maas

vincent said:
Here is my idea:

#############################################################
#module myBothJavaAndPyParser :

#if the module is used by the Java app ::
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory
from org.w3c.dom import *

factory=DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
parseString=self.builder.parse

#else:
from xml.dom import minidom,Node
parseString=minidom.parseString

My suggestion (untested):

import sys

if sys.platform[:4] == 'java':
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory
from org.w3c.dom import *
factory=DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
parseString=self.builder.parse
else:
from xml.dom import minidom,Node
parseString=minidom.parseString

Mit freundlichen Gruessen,

Peter Maas
 
P

Peter Otten

vincent said:
- 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 :( -

Only one generic parser would have been the best solution because it
will be used by a Java application on one side and by a C++ application
on the other side but i've failed to find a python xml lib working both
in python and jython ....

So, now i'd like to put these 2 modules together as much as possible, to
finally have only one, if possible...
To do that, i thought using python shortcuts (actually, the content of
these 2 modules is quite similar, only method names change)

Here is my idea:

#############################################################
#module myBothJavaAndPyParser :

#if the module is used by the Java app ::
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory
from org.w3c.dom import *

factory=DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
parseString=self.builder.parse

#else:
from xml.dom import minidom,Node
parseString=minidom.parseString

class myParser:
def __init__(self):
#...

def parse(self,toParse):
return parseString(toParse)

.....
#############################################################

But when i instanciate this class, i don't know how to do to know which
imports and aliases must be done ... i tried to put the imports in the
__init__ (and using a boolean) but then i can't access to aliases
Any suggestions ?

Make a wrapper module that tries to import the Jython/CPython-specific
stuff:

try:
from preferred_implementation import factory, builder, parseString
except ImportError:
from fallback_implementation import factory, builder, parseString

# code shared by both implementations
And i have another problem concerning shortcuts : how to make a shortcut
on an attribute ?
For example :
Using java, for a Node, we have the getParentNode() method :
myNode.getParentNode()
In python, it's an attribute : myNode.parentNode

How to do to make a shortcut in this specific case ?

And if a make the shortcut in another way :
#if used by Java :
# imports ...
parentNode=Node.getParentNode

But now, i will uses this syntax : myNode.parentNode() .... and it won't
work anymore using python imports ....

In the CPython implementation code (untested):

import xml.dom.minidom

def getParentNode(self):
return self.parentNode

xml.dom.minidom.Node.getParentNode = getParentNode

Then you should always be able to use myNode.getParentNode() in your client
code. This is a hack, of course. The clean way would be to subclass Node,
but that is tedious...

Peter
 
M

Matteo Dell'Amico

vincent said:
- 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.
 
V

vincent Salaun

waouh, i wasn't expected so much answers, thanks a lot everyone !

this will help me, for sure ! I think i will try Peter Otten' solution,
which is what i beginned to do. I didn't think about using import
exceptions, i think it's a good & simple way (in this specific case) to
know from where the module is used ...

thx again !
vince
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top