字典没有选择正确的密钥

时间:2017-06-04 21:50:49

标签: python

我的一本词典有一个奇怪的问题。

下面是一个方法,它将根据收到的时间帧执行另一种方法。时间范围为m1,m5,m15,m30,H1,H4,D1,W1,M1。

基本上从1分钟到1个月。

def get_trading_range(self, init_date, time_frame):
    """
    """
    key = time_frame[:1]
    tr_dict = {
        'D' : self._daily_range(init_date),
        'W' : self._weekly_range(init_date),
        'M' : self._monthly_range(init_date),
        'H' : self._hourly_range(init_date, time_frame),
        'm' : self._minutely_range(init_date, time_frame)
    }
    return tr_dict[key]

使用以下

调用该方法
DateRange().get_trading_range(datetime(some_date), 'D1')

似乎在选择'm'方法。我尝试删除'm'键和值,然后再次执行时选择'H'方法。只有当我删除'm'和amp; 'H'键和值将执行'D'方法。

这是一个奇怪的错误吗?

这是追溯

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "engine.py", line 25, in _historical
    HistoricalCollector().historical_prices(hist_queue, live_queue, event)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 90, in historical_prices
    self._historical_iter(hist_queue, live_queue, fxoffer)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 73, in _historical_iter
    self._collect_data(fxc, offer, time_frame, init_date)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 28, in _collect_data
    dtx = DateRange().get_trading_range(init_date, time_frame)
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 84, in get_trading_range
    'm' : self._minutely_range(init_date, time_frame),
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 103, in _minutely_range
    freq = self._data_frequency(time_frame)[0]
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 23, in _data_frequency
    return freq_keys[time_frame]
KeyError: 'D1'

还尝试使用key = time_frame[0]

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "engine.py", line 25, in _historical
    HistoricalCollector().historical_prices(hist_queue, live_queue, event)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 90, in historical_prices
    self._historical_iter(hist_queue, live_queue, fxoffer)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 73, in _historical_iter
    self._collect_data(fxc, offer, time_frame, init_date)
  File "/home/nonroot/fxcmminer_1.1/fxcm/historical.py", line 28, in _collect_data
    dtx = DateRange().get_trading_range(init_date, str(time_frame))
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 84, in get_trading_range
    'm' : self._minutely_range(init_date, time_frame),
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 103, in _minutely_range
    freq = self._data_frequency(time_frame)[0]
  File "/home/nonroot/fxcmminer_1.1/fxcm/tradinghours.py", line 23, in _data_frequency
    return freq_keys[time_frame]
KeyError: 'D1'

2 个答案:

答案 0 :(得分:2)

问题是,在创建tr_dict时会调用这些方法,而不是在您访问时调用这些方法,这就是_minutely_range调用time_frame = "D1"的原因。

尝试类似:

tr_dict = {
    'D' : lambda: self._daily_range(init_date),
    'W' : lambda: self._weekly_range(init_date),
    'M' : lambda: self._monthly_range(init_date),
    'H' : lambda: self._hourly_range(init_date, time_frame),
    'm' : lambda: self._minutely_range(init_date, time_frame)
}
return tr_dict[key]()

这样,只有在必要时才会调用这些方法。

答案 1 :(得分:0)

看起来这个功能并没有回归' m'或者' H'但是当它遇到回溯时正在评估它们。是key你想要传递给他们的函数而不是time_frame吗?