Email Re: [Fwd: Re: [Python-projects] pylint failure?]

from
to
cc
Alessandro Caproni <acaproni at eso dot org>
subject
Re: [Fwd: Re: [Python-projects] pylint failure?]
date
2005/02/10 13:00
Hello,

On Wed, Feb 09, 2005 at 11:18:14AM -0700, David Fugate wrote:

> If you can offer any suggestions, I'd love to hear them.

I was wondering if some of the code below couldn't be a bit
rewritten. I'm probably simplifying too much but this might
nevertheless give some hints on how to proceed.

> #------------------------------------------------------------------------------
> _enumClassDict = {}

> def getEnumClass(repositoryID):
>     '''
>     '''
>     global _enumClassDict

>     if _enumClassDict.has_key(repositoryID):
>         return _enumClassDict[repositoryID]
>     
>     #first convert the repos ID to the name of a Python class
>     enumName = repositoryID.split(':')[1].split('/')[1:]
>     enumName[0] = enumName[0] + "__POA"
>     modName = enumName[0]
>     shortEnumName = enumName[len(enumName) -1]
>     enumName = reduce((lambda x, y : str(x) + '.' + str(y)), enumName)

>     #next import the correct CORBA stub module
>     tGlobals = {}
>     tLocals  = {}
>     exec "import " + modName in tGlobals, tLocals

>     class tClass(eval(enumName, tGlobals, tLocals), Penum):
>         '''
>         '''
>         pass
>     
>     _enumClassDict[repositoryID] = tClass

>     return tClass

def getEnumClass(repositoryId):
    if _enumClassDict.has_key(repositoryID):
        return _enumClassDict[repositoryID]
    
    #first convert the repos ID to the name of a Python class
    enumName = repositoryID.split(':')[1].split('/')[1:]
    enumName[0] = enumName[0] + "__POA"
    modName = enumName[0]
    shortEnumName = enumName[-1]
    enumName = '.'.join(enumName) # more pythonic than the reduce(lambda ...)

    mod = __import__(modName)
    # the following line doens't work if enumName is a dotted name
    # (see comment below)
    klass = getattr(mod, enumName)
    tclass = type('tClass', (klass, Penum), {})
    # Or just do : class tClass(klass, Penum): pass
    _enumClassDict[repositoryID] = tClass
    return tClass

I didn't test this code, and as I said, I might be simplifying the
problem. For example, you might havet serveral dots in your classname. But
even with this, you could always chain several getattrs and it would
still be better (IMHO) than using exec/eval

Hope this helps.

Cheers,
Adrien.

-- 
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr   http://www.logilab.org


is a reply to