[Zope3-dev] Offtopic: soft imports
Jim Fulton
jim at zope.com
Fri Dec 10 14:06:16 EST 2004
Florent Guillaume wrote:
> Jim wrote:
>
>>Stephan Richter wrote:
>>
>>>Yes, I was about to suggest the same:
>>>
>>>try:
>>> from zope.i18nmessageid import MessageIdFactory
>>> _ = MessageIdFactory('domain')
>>>except ImportError:
>>> _ = unicode
>>>
>>>This way you only have a soft dependence of zope.i18nmessageid
>>
>>This pattern is a bug magnet. It has been employed before
>>do deal with this situation (message ids) specifically and has
>>caused me a world of pain.
>
>
> I've been bitten by that pattern a lot too, nowadays I recommend:
>
> try:
> from zope.i18nmessageid import MessageIdFactory
> except ImportError, e:
> if str(e) != "No module named zope.i18nmessageid": raise
> _ = unicode
> else:
> _ = MessageIdFactory('domain')
>
> But it's a bit more verbose... Anybody using this ?
I've used a variation that's less brittle:
if 'i18nmessageid' not in str(e): ...
It really sucks that Python so often raises exceptions
with preformatted messages. You shouldn't have to screen-scrape
an exception to get it's data. :(
Jim
--
Jim Fulton mailto:jim at zope.com Python Powered!
CTO (540) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org
More information about the Zope3-dev
mailing list