[Zope3-dev] Offtopic: soft imports

Florent Guillaume fg at nuxeo.com
Fri Dec 10 14:00:08 EST 2004


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 ?

Florent

-- 
Florent Guillaume, Nuxeo (Paris, France)   CTO, Director of R&D
+33 1 40 33 71 59   http://nuxeo.com   fg at nuxeo.com


More information about the Zope3-dev mailing list