0

私は Python で大きなプロジェクトに取り組んでいますが、説明できない奇妙なエラーが発生しました。私のクラスの 1 つで、インスタンス化中に呼び出されるプライベート メソッドがあります。

def _convertIndex(self, dimInd, dimName):
    '''Private function that converts numbers to numbers and non-integers using 
    the subclass\'s convertIndex.'''
    print dimInd, ' is dimInd'
    try:
        return int(dimName)
    except:
        if dimName == '*':
            return 0
        else:
            print self.param.sets, ' is self.param.sets'
            print type(self.param.sets), ' is the type of self.param.sets'
            print self.param.sets[dimInd], ' is the param at dimind'
            return self.param.sets[dimInd].value(dimName)

それが出力しているもの:

0  is dimInd
[<coremcs.SymbolicSet.SymbolicSet object at 0x10618ad90>]  is self.param.sets
<type 'list'>  is the type of self.param.sets
<SymbolicSet BAZ=['baz1', 'baz2', 'baz3', 'baz4']>  is the param at dimind
======================================================================
ERROR: testParameterSet (GtapTest.TestGtapParameter)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/myuser/Documents/workspace/ilucmc/gtapmcs/test/GtapTest.py", line 116, in testParameterSet
pset = ParameterSet(prmFile, dstFile, GtapParameter)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/ParameterSet.py", line 103, in __init__
self.distroDict, corrDefs = AbsBaseDistro.readFile(distFile, self.paramDict)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 359, in readFile
distro = cls.parseDistro(param, target, distroType, args)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 301, in parseDistro
return cls(param, target, distro, dim_names, argDict)
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 150, in __init__
self.dim_indxs = list(starmap(self._convertIndex, enumerate(dim_names)))  # convert to numeric values and save in dim_indxs
File "/Users/myuser/Documents/workspace/ilucmc/coremcs/Distro.py", line 194, in _convertIndex
print self.param.sets[dimInd], ' is the param at dimind'
IndexError: list index out of range

明らかに、これはクラス全体のコードではありませんが、私には理解できないものを表しています。self.param.sets にインデックスを付けると、エラーが発生します。どうやら、dimInd は範囲外です。問題は、dimInd が 0 であり、self.param.sets が長さ 1 のリストであるため (print ステートメントから示されているように)、なぜインデックスを作成できないのでしょうか?

編集:価値があるため、__init__メソッドは次のようになります。

'''
Stores a definitions of a distribution to be applied to a header variable.
See the file setup/gtap/DistroDoc.txt for the details.
'''
def __init__(self, param, target, distType, dim_names, argDict):
    self.name = param.name
    self.dim_names = dim_names
    self.dim_indxs = []
    self.target = target.lower() if target else None
    self.distType = distType.lower() if distType else None
    self.rv = None
    self.argDict = {}
    self.modifier = defaultdict(lambda: None)
    self.param = param

    # Separate args into modifiers and distribution arguments
    for k, v in argDict.iteritems():
        if k[0] == '_':  # modifiers start with underscore
            self.modifier[k] = v
        else:
            self.argDict[k] = v  # distribution arguments do not have underscore

    if self.target == 'index':
        print dim_names
        self.dim_indxs = list(starmap(self._convertIndex, enumerate(dim_names)))  # convert to numeric values and save in dim_indxs

    if distType == 'discrete':
        entries = self.modifier['_entries']
        if not entries:
            raise DistributionSpecError("Not enough arguments given to discrete distribution.")
        modDict = {k[1:]: float(v) for k, v in self.modifier.iteritems() if k[1:] in getOptionalArgs(DiscreteDist.__init__)}
        self.rv = DiscreteDist(entries, **modDict)
        return
    sig = DistroGen.signature(distType, self.argDict.keys())
    gen = DistroGen.generator(sig)

    if gen is None:
        raise DistributionSpecError("Unknown distribution signature %s" % str(sig))

    self.rv = gen.makeRV(self.argDict)  # generate a frozen RV with the specified arguments
    self.isFactor = gen.isFactor
4

0 に答える 0