我将根据什么构建成功与否以及每个项目有多少信息来创建统计数据。你知道吗
我为我看到的每个新项目和内部处理的统计信息创建ProjectStat类。为了打印总体统计数据,我需要遍历所有ProjectStat实例。为了打印每个项目的成功统计数据,我需要通过他们再次等等,对任何类型的统计数据。我的问题是简化处理循环的方法,即不要每次都通过字典。也许使用decorator或decorator模式将是一种python方式?如果ProjectStat的实例数是动态变化的,那么如何使用它们呢?你知道吗
代码如下:
class ProjectStat(object):
projectSuccess = 0
projectFailed = 0
projectTotal = 0
def addRecord(self, record):
if len(record) == 5: record.append(None)
try:
(datetime, projectName, branchName, number, status, componentName) = record
except ValueError:
pass
self.projectTotal += 1
if status == 'true': self.projectSuccess += 1
else: self.projectFailed += 1
def addDecorator(self, decorator):
decorator = decorator
def readBuildHistoryFile():
dict = {}
f = open("filename")
print("reading the file")
try:
for line in f.readlines():
#print(line)
items = line.split()
projectName = items[1]
projectStat = dict[projectName] = dict.get(projectName, ProjectStat())
projectStat.addRecord(items)
print(items[1])
finally:
f.close()
success = 0
failed = 0
total = 0
for k in dict.keys():
projectStat = dict[k]
success += projectStat.projectSuccess
failed += projectStat.projectFailed
total += projectStat.projectTotal
print("Total: " + str(total))
print("Success: " + str(success))
print("Failed: " + str(failed))
if __name__ == '__main__':
readBuildHistoryFile()
我不确定我是否理解这个问题,但我还是会尽量回答:)
选项1:
选项2:
相关问题 更多 >
编程相关推荐