类的python字典,值消失

时间:2013-02-08 09:06:03

标签: python class dictionary

我正在尝试将类的字典分配为值,将字符串分配为键。 在主循环内打印时,我有正确的值, 但是一旦我离开主循环,我的“cliStat”变量就会初始化 “O”值..

这是我执行任务和打印的代码部分。 我的问题是,为什么cliStat保持“0”值?

cliStat = {}
for org in client:

        cliStat[ org ] = StatEntry( org, "0", "0", "0", "0", "0", "0", "0" ) 


self.getFilteredStat( client, date_debut, date_fin )

for statEntry in self.tabStatEntry.values():

        cliStat[ statEntry.client ].nb_users += statEntry.nb_users
        cliStat[ statEntry.client ].nb_pages = ( statEntry.nb_pages + cliStat[ statEntry.client ].nb_pages ) / 2
        cliStat[ statEntry.client ].perf_rate = ( statEntry.perf_rate + cliStat[ statEntry.client ].perf_rate ) / 2
        cliStat[ statEntry.client ].response_time = ( statEntry.response_time + cliStat[ statEntry.client ].response_time ) / 2
        cliStat[ statEntry.client ].nb_errors = ( statEntry.nb_errors + cliStat[ statEntry.client ].nb_errors ) / 2
        cliStat[ statEntry.client ].perf_globale = ( statEntry.perf_globale + cliStat[ statEntry.client ].perf_globale ) / 2
        cliStat[ statEntry.client ].perf_server = ( statEntry.perf_server + cliStat[ statEntry.client ].perf_server ) / 2
        cliStat[ statEntry.client ].perf_network = ( statEntry.perf_network + cliStat[ statEntry.client ].perf_network ) / 2
        cliStat[ statEntry.client ].perf_redirect = ( statEntry.perf_redirect + cliStat[ statEntry.client ].perf_redirect ) / 2


for cle,val in cliStat.items():

        print val

如果需要,这是完整的代码; http://dpaste.com/914508/

如果您需要运行代码,请输入数据文件http://rapidshare.com/files/2185155450/meteo.7z

只需使用您的文件夹更改logDir变量。

谢谢你看看!

1 个答案:

答案 0 :(得分:0)

并非所有对象都用零填充。

我将最后一个循环更改为

    for cle,val in cliStat.items():
        if val.nb_users:
            print val

并获得了六个客户端的输出,所有客户端都填充了非零数据。

最好先用少量数据测试代码。