Organizing python code for handling statistic information
<p>I'm going to create statistics based on information what builds were success or not and how much per project. </p>

<p>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?</p>

<p>Here is the code:</p>

<pre><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()
</code></pre>
