错误迭代Python 3.4中的OrderedDict

时间:2015-09-08 23:01:13

标签: python python-3.4

我正在尝试使用{Key,Value}迭代OrderedDict,但我遇到了Key Error。 iteritems()在我正在使用的python 3.4中被弃用,现在我必须使用items()来做同样的事情

 File "/home/hotdogs/mydictset.py", line 88, in __init__
 self.mainParams[k]['start'], self.mainParams[k]['step'], self.mainParams[k] ['stop'])
 KeyError: 'timed_exit_bars'


class MySet()
  def __init__(self, filePath):
    # Read diagnostics set file
    self.content = readSetFile(filePath)
    if not self.content: return
    self.sections = self.content._sections
    self.mainParams = {}
    self.optimizationArray = {}
    global paramIndexes

    #for k, v in OrderedDict(self.content.items('main')).iteritems(): --was from python 2.7 and i have removed it.

    for k, v in OrderedDict(self.content.items('main')).items():
        # Add the params names avoiding the optimization line
        if k.find(",") == -1:
            # Adding name of the param and the value
            if k not in self.mainParams:
                self.mainParams[k] = {}
            self.mainParams[k]['value'] = v
            # optimization flags
            if self.content.has_option('main', k + ',F'):
                if self.content.getboolean('main', k + ',F') == 1:
                    self.mainParams[k]['optimize'] = True
                    self.mainParams[k]['start'] = self.content.getfloat('main', k + ',1')
                    self.mainParams[k]['step'] = self.content.getfloat('main', k + ',2')
                    self.mainParams[k]['stop'] = self.content.getfloat('main', k + ',3')
                    self.optimizationArray[paramIndexes[k]] = (
                    self.mainParams[k]['start'], self.mainParams[k]['step'], self.mainParams[k]['stop'])
                else:
                    self.mainParams[k]['optimize'] = False
            else:
                self.mainParams[k]['optimize'] = False

    i = 0

当我尝试调试Key错误时。我看到mainParams只包含一个普通的未排序的dict,参数本身也是普通的dict本身来自下面的调试。样品低于

  {'section_1': {'value': '################# General Settings #################', 'optimize': False}, 
  'run_every_tick': {'value': '0', 'optimize': False}, 
  'historic_data_id': {'value': '1', 'optimize': False}, 
  'section_2': {'value': '############## Common Strategy Settings ##############','optimize': False}, 
  'use_instance_balance': {'value': '0', 'optimize': False}, 
  'comments': {'value': 'Click Modify expert to view comments inside the source code.', 'optimize': False}, 
  'operational_mode': {'value': '1', 'optimize': False}, 
  'instance_management': {'value': '0', 'optimize': False}, 
  'analysis_window_size': {'value': '0', 'optimize': False}, 
  'enable_screenshots': {'value': '0', 'optimize': False}, 
  'timed_exit_bars': {'step': 5.0, 'start': 5.0, 'value': '60', 'stop': 100.0, 'optimize': True}, 
  'max_open_orders': {'value': '1', 'optimize': False}, 
  'strategy_timeframe': {'value': '60', 'optimize': False}, 
  'ui_font_size': {'value': '12', 'optimize': False},  
  'disable_compounding': {'value': '1', 'optimize': False}}

以下是我通过Debug for ParamIndexes

获得的示例
{
'INTERNAL_STRATEGY_ID': 52, 
'RUN_EVERY_TICK': 43, 
'MAX_OPEN_ORDERS': 45, 
'OPERATIONAL_MODE': 50, 
'ANALYSIS_WINDOW_SIZE': 55, 
'TIMEFRAME': 53, 
'DISABLE_COMPOUNDING': 47,
'INSTANCE_MANAGEMENT': 44, 
'MAX_SPREAD': 59, 
'TIMED_EXIT_BARS': 48, 
'STRATEGY_INSTANCE_ID': 51, 
...
...
....
....
....
....
....
....
....
....
}

我有什么遗失的东西。我似乎无法使用items()来迭代字典。任何帮助都很受欢迎。

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

self.optimizationArray[paramIndexes[k]] = (
self.mainParams[k]['start'], self.mainParams[k]['step'], self.mainParams[k]['stop'])

由于您刚刚创建了self.mainParams[k]['start']self.mainParams[k]['step']self.mainParams[k]['stop'],因此不会引发错误。唯一的罪魁祸首是paramIndexes[k]和/或self.optimizationArray[paramIndexes[k]]

我会加倍检查 paramIndexes k self.optimizationArray 有<{1}}映射到的任何内容

此问题与OrderedDict无关。这只是一个KeyError。