开发者

Organizing python code for handling statistic information

开发者 https://www.devze.com 2022-12-13 23:23 出处:网络
I\'m going to create statistics based on information what builds were success or not and how much per project.

I'm going to create statistics based on information what builds were success or not and how much per project.

I create ProjectStat class per new project I see and inside handled statistics. For printing overall statistic I need to pass through all ProjectStat instances. For printing success statistics per project I need to pass through them again and so on, on any kind of statistics. My question is about simplifying the way handling the cycles, i.e not to pass the dictionary every time. Perhaps using decorators or decorator pattern would be pythonic way? How then they can be used if number of instances of ProjectStat is dynamically changed?

Here is开发者_JAVA百科 the code:

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()


I'm not sure I understand the Q, but I'll try to answer anyway :)

option1:

total = sum([project.projectTotal for project in dict.values()])
success = sum([project.projectSuccess for project in dict.values()])
failed = sum([project.projectFailed for project in dict.values()])

option2:

(total,success,failed) = reduce (lambda x,y:(x[0]+y[0],x[1]+y[1],x[2]+y[2]), [(project.projectTotal,project.projectSuccess,project.projectFailed) for project in dict.values()])
0

精彩评论

暂无评论...
验证码 换一张
取 消