08.22.06
Python namespacing is the hardest thing for me to get used to so far
I keep stumbing over the same thing in different contexts: namespacing and how to address particular objects. It’s very context sensitive, and I’m not quite grokking it yet, especially on the ‘pythonic’ way to do it. The documentation is not very clear on a first reading of how to deal with it.
Here’s an example of simple module I just started:
import xml.parsers.expat
from xml.dom.minidom import parse, parseString
import string
class RFRData:
DATA = None
@classmethod
def get(cls):
if not DATA:
cls.init()
@classmethod
def init(cls, conf=None):
cls.load_data(conf)
cls.validate_data()
@classmethod
def load_data(cls, conf=None):
try:
if conf:
if conf.find('<') == 0:
DATA = parseString(conf)
else:
DATA = parse(conf)
else:
DATA = parse('./rfrconf.xml')
except xml.parsers.expat.ExpatError, err:
print "something bad happened" , err
@classmethod
def validate_data(cls):
pass
It took me about 15 minutes to find:
If I don’t trap the error, and the xml is invalid, I get this message:
ExpatError: mismatched tag: line 21, column 6
Fine, but how to trap it? My expectation, based on that message, would be that I could have this line:
except ExpatError, err:
But no, python reports:
NameError: global name 'ExpatError' is not defined
That won’t work because I only imported a couple methods from the xml.dom.minidom package. So I try…
import xml.dom.minidom [...] except xml.dom.minidom.ExpatError, err:
And no, that doesn’t work, either:
AttributeError: 'module' object has no attribute 'ExpatError'
Which, of course, is correct, because it’s just a dom module, not a parser module. I just tried those two reflexively, based on my expectatations.
So, where does ExpatError live?
In the xml.parsers.expat module, where one would expect it to live. And I have to import it to trap errors from xml.dom.minidom.parse().
My beef is that:

























