Python使用现有密钥给出密钥错误

时间:2015-03-03 09:18:05

标签: python dictionary web2py

我有一个功能:

def test():
    options = dict(
    l1 = "sector",
    l2 = "industry",
    sd = "2013-01-13 00:00:00",
    ed = "2013-12-31 00:00:00",
    pf = "usequity",
    bm = "SP500")

    f = formp()

    elements = ["formstring", "form1", "fc", "fp", "fb",  "fsd", "fed", "fl1",  "fl2",  "fm"]
    formstring, form, client, portfolio, benchmark, startd, endd, lev1, lev2, methodology = [f[i] for i in elements]

    if all(f[i] for i in ["fp", "fb",  "fsd", "fed", "fl1",  "fl2"]):
        options = dict((key,value) for key,value in zip(['pf','bm','sd','ed','l1','l2'],
                                                        ["fp", "fb",  "fsd", "fed", "fl1",  "fl2"]))
    holdings = attrib.db_results(options,bd)
    dr = attrib.dailyReturn(holdings, options, bd)
    analytics = ratios.ratios(dr, options, 30)

    trackingerror = analytics['trackingErrorAnn'].dropna(axis=0)
    volatility = pd.concat([analytics["stdPF"].dropna(axis=0),analytics['stdBM'].dropna(axis=0)],axis=1)
    captureup = pd.concat([analytics["captureUp"].dropna(axis=0),analytics['captureDn'].dropna(axis=0)],axis=1)
    numberup = pd.concat([analytics["numberUp"].dropna(axis=0),analytics['numberDn'].dropna(axis=0)],axis=1)
    alphaup = pd.concat([analytics["alphaUp"].dropna(axis=0),analytics['alphaDn'].dropna(axis=0)],axis=1)
    covariation = analytics["variationCoefficient"].dropna(axis=0)
    sharpe = analytics["sharpe"].dropna(axis=0)
    information = analytics["informationRatio"].dropna(axis=0)
    sortino = analytics["sortino"].dropna(axis=0)
    upsidepot = analytics["upsidePotential"].dropna(axis=0)
    beta = analytics["beta"].dropna(axis=0)
    betaup = analytics["betaUp"].dropna(axis=0)
    betadown = analytics["betaDn"].dropna(axis=0)
    var = analytics["VaR"].dropna(axis=0)
    mvar = analytics["modifiedVAR"].dropna(axis=0)
    semivar = analytics["semiVariance"].dropna(axis=0)
    downsidedev = analytics["downsideDeviation"].dropna(axis=0)
    treynor = analytics["treynor"].dropna(axis=0)
    kelly = analytics["kellyRatio"].dropna(axis=0)
    drawdown = analytics["drawdown"].dropna(axis=0)
    drawdowndev = analytics["drawdownDeviation"].dropna(axis=0)

    measures = ["trackingerror", "volatility", "captureup", "numberup", "alphaup", "covariation", "sharpe", "information",
                "sortino", "upsidepot", "beta", "betaup", "betadown", "var", "mvar", "semivar", "downsidedev",
                "treynor", "kelly", "drawdown", "drawdowndev"]

    measureDict = {measure: locals()[measure].reset_index() for measure in measures}

    dual = ['volatility','captureup','numberup','alphaup']
    names = [['pf', 'bm']] + [['up', 'down']]*3
    for measure, colnames in zip(dual,names):
        measureDict[measure].columns = ['date'] + colnames

    for measure in measures:
        if not measure in dual:
            measureDict[measure].columns = ['date', measure]

    measureString = {}

    for measure in measures:
        tmp = measureDict[measure]
        tmp.date = tmp.date.apply(lambda x: x.strftime("%d-%m-%y"))
        tmp = tmp.to_dict('r')
        tmp = json.dumps(tmp)
        measureString[measure] = tmp

    return locals()

但是当我运行它时出现错误

Traceback (most recent call last):
  File "/home/tahnoon/web2py/gluon/restricted.py", line 224, in restricted
    exec ccode in environment
  File "/home/tahnoon/web2py/applications/apollo/controllers/performance.py", line 829, in <module>
  File "/home/tahnoon/web2py/gluon/globals.py", line 392, in <lambda>
    self._caller = lambda f: f()
  File "/home/tahnoon/web2py/applications/apollo/controllers/performance.py", line 70, in test
    measureDict = {measure: locals()[measure].reset_index() for measure in measures}
  File "/home/tahnoon/web2py/applications/apollo/controllers/performance.py", line 70, in <dictcomp>
    measureDict = {measure: locals()[measure].reset_index() for measure in measures}
KeyError: 'trackingerror'

这是一个明显的python错误,我正在制作或不太明显的web2py?我该如何解决?

由于

0 个答案:

没有答案