<commit_before>import model
import os
from RegelList import RegelList

class GrootBoek():

    def __init__(self, name, descr, level, parent):
        self.name = name
        self.descr = descr
        self.parent = parent #GrootBoek class
        self.level = level
        self.kostenSoorten = {} # KS die bij deze node horen (uit kostensoortgroep)
        self.children = [] # list of GrootBoek classes

        self.regels = {} #['geboekt', 'obligo', 'plan'] = RegelList

        # We keep totals per node and tree to prevent running through it multiple
        # times when we display the tree in output or html.
        self.totaalNodePerKS = {}  # [geboekt, obligo, plan][<kostensoort] = <total kosten of node of that ks>
        self.totaalTree = {} # [geboekt, obligo, plan] = <total of current node + children>

    def add_kostensoort(self, kostensoort, descr):
        self.kostenSoorten[kostensoort] = descr

    def add_child(self, child):
        self.children.append(child)

    def regel(self):
        #if (totals[0]+ totals[1]) != 0:
            #return '*' * self.level + ' ' + self.name + ' (' + self.descr + ') ' + str(self.totaalGeboektTree) + ' - ' + str(self.totaalObligosTree)
        return 'DEPRC. todo'

    def druk_af(self):
        print 'grootboek ' + self.name + ' (level '+str(self.level)+') - ' + self.descr
        print 'totaal node: ' + str(self.totaalNodePerKS)
        print 'totaal tree: ' + str(self.totaalTree)

        if self.parent != '':
            print 'belongs to parent: ' + self.parent.name

        if self.kostenSoorten:
            print 'contains the following kostensoorten:'
            print self.kostenSoorten

        if self.regels:
            print 'contains the following regels:'
            for tiepe, regellist in self.regels.iteritems():
                print tiepe
                for regel in regellist.regels:
                    print '   '+ regel.omschrijving + ' - ' + str(regel.kosten) + ' - ' + regel.tiepe

        if self.children:
            print 'and has children:'
            for child in self.children:
                print child.name

        print ''

    def lower_level_parent(self, level):
        if self.level < level:
            return self
        else:
            return self.parent.lower_level_parent(level)

    def html_tree(self, render, maxdepth, depth):
        from functions import moneyfmt
        depth += 1

        groups = []
        for child in self.children:
            groups.append(child.html_tree(render, maxdepth, depth))

        regelshtml = []

        unfolded = False # Never show the details
#regel iteritems zijn nu plannen geen ks! dus per key

        regelsPerKSPerTiepe = RegelList()
        for key, regellist in self.regels.iteritems():
            regelsPerKSPerTiepe.extend(regellist)

        regelsPerKSPerTiepe = regelsPerKSPerTiepe.split_by_regel_attributes(['kostensoort', 'tiepe'])

        totals = {}
        totals['geboekt'] = 0
        totals['obligo'] = 0
        totals['plan'] = 0
        for kostenSoort, regelsPerTiepe in regelsPerKSPerTiepe.iteritems():
            totalsKS = {}
            totalsKS['geboekt'] = 0
            totalsKS['obligo'] = 0
            totalsKS['plan'] = 0
            for tiepe, regellist in regelsPerTiepe.iteritems():
                totalsKS[tiepe] = regellist.total()
                totals[tiepe] += regellist.total()

                for regel in regellist.regels:
                    regel.kosten = moneyfmt(regel.kosten, places=2, dp='.')

                KSname = self.kostenSoorten[kostenSoort]
                KSname = str(kostenSoort) +' - ' + KSname.decode('ascii', 'replace').encode('utf-8')
                regelshtml.append(render.regels(self.name, kostenSoort, KSname, totalsKS, regellist.regels, unfolded))

        if depth <= maxdepth:
            unfolded = True
        else:
            unfolded = False

        html = render.grootboekgroep(self.name, self.descr, groups, regelshtml, unfolded, totals, depth)

        return html


    def walk_tree(self, maxdepth):
        if self.level <= maxdepth:
            self.druk_af()
            for child in self.children:
                child.walk_tree(maxdepth)


    def walk_levels(self):
        levels = {self.level}
        if self.children:
            for child in self.children:
                levels.update(child.walk_levels())

        return levels

    # returns a list of all nodes from self
    # that have requested level
    def find_level(self, level):
        levels = []
        for child in self.children:
            levels = levels + child.find_level(level)

        if self.level == level:
            levels.append(self)

        return levels

    def find(self, name):
        if self.name == name:
            return self
        elif self.children:
            for child in self.children:
                result = child.find(name)
                if result != '':
                    return result
        else:
            return ''

    def assign_regels_recursive(self,regels):
        for child in self.children:
            child.assign_regels_recursive(regels)

        for key in regels.keys():
            filteredRegels = regels[key].copy()
            filteredRegels.filter_regels_by_attribute('kostensoort', self.kostenSoorten.keys())
            self.regels[key] = filteredRegels


  # Set totals per key [obligo, plan, etc.], and per ks for each node
    def set_totals(self, periode=range(0,16)):
        self.totaalTree = {}
        self.totaalNodePerKS = {}
        self.totaalTree['geboekt'] = 0
        self.totaalTree['obligo'] = 0
        self.totaalTree['plan'] = 0

        # For each key [begroot, plan, ] split into ks and periode
        for key in self.regels.keys():
            regelsPerKS = self.regels[key].split_by_regel_attributes(['kostensoort', 'periode'])

            # Walk over all ks, make sure there is a dict item
            for ks, regelsPerPeriode in regelsPerKS.iteritems():
                if key not in self.totaalNodePerKS:
                    self.totaalNodePerKS[key] = {}

                # walk over all periodes, make sure there is a dict item only if the period matches
                for periodeList, regellist in regelsPerPeriode.iteritems():
                    if periodeList in periode:
                        if ks not in self.totaalNodePerKS[key]:
                            self.totaalNodePerKS[key][ks] = 0
                        self.totaalNodePerKS[key][ks] += regellist.total()

                # add the all the ks for every valid period to the total
                self.totaalTree[key] += self.totaalNodePerKS[key][ks]

        #add childrens totaltree's
        for child in self.children:
            totaalChildTree = child.set_totals(periode=periode)
            for key in totaalChildTree.keys():
                self.totaalTree[key] += totaalChildTree[key]

        return self.totaalTree


    # Creates a list of all levels in the tree
    # for example: [1, 5, 12, 40]
    def list_levels(self, levels):
        for child in self.children:
            levels = child.list_levels(levels)
<commit_msg>fixes bug in set_totals<commit_after>import model
import os
from RegelList import RegelList

class GrootBoek():

    def __init__(self, name, descr, level, parent):
        self.name = name
        self.descr = descr
        self.parent = parent #GrootBoek class
        self.level = level
        self.kostenSoorten = {} # KS die bij deze node horen (uit kostensoortgroep)
        self.children = [] # list of GrootBoek classes

        self.regels = {} #['geboekt', 'obligo', 'plan'] = RegelList

        # We keep totals per node and tree to prevent running through it multiple
        # times when we display the tree in output or html.
        self.totaalNodePerKS = {}  # [geboekt, obligo, plan][<kostensoort] = <total kosten of node of that ks>
        self.totaalTree = {} # [geboekt, obligo, plan] = <total of current node + children>

    def add_kostensoort(self, kostensoort, descr):
        self.kostenSoorten[kostensoort] = descr

    def add_child(self, child):
        self.children.append(child)

    def regel(self):
        #if (totals[0]+ totals[1]) != 0:
            #return '*' * self.level + ' ' + self.name + ' (' + self.descr + ') ' + str(self.totaalGeboektTree) + ' - ' + str(self.totaalObligosTree)
        return 'DEPRC. todo'

    def druk_af(self):
        print 'grootboek ' + self.name + ' (level '+str(self.level)+') - ' + self.descr
        print 'totaal node: ' + str(self.totaalNodePerKS)
        print 'totaal tree: ' + str(self.totaalTree)

        if self.parent != '':
            print 'belongs to parent: ' + self.parent.name

        if self.kostenSoorten:
            print 'contains the following kostensoorten:'
            print self.kostenSoorten

        if self.regels:
            print 'contains the following regels:'
            for tiepe, regellist in self.regels.iteritems():
                print tiepe
                for regel in regellist.regels:
                    print '   '+ regel.omschrijving + ' - ' + str(regel.kosten) + ' - ' + regel.tiepe

        if self.children:
            print 'and has children:'
            for child in self.children:
                print child.name

        print ''

    def lower_level_parent(self, level):
        if self.level < level:
            return self
        else:
            return self.parent.lower_level_parent(level)

    def html_tree(self, render, maxdepth, depth):
        from functions import moneyfmt
        depth += 1

        groups = []
        for child in self.children:
            groups.append(child.html_tree(render, maxdepth, depth))

        regelshtml = []

        unfolded = False # Never show the details
#regel iteritems zijn nu plannen geen ks! dus per key

        regelsPerKSPerTiepe = RegelList()
        for key, regellist in self.regels.iteritems():
            regelsPerKSPerTiepe.extend(regellist)

        regelsPerKSPerTiepe = regelsPerKSPerTiepe.split_by_regel_attributes(['kostensoort', 'tiepe'])

        totals = {}
        totals['geboekt'] = 0
        totals['obligo'] = 0
        totals['plan'] = 0
        for kostenSoort, regelsPerTiepe in regelsPerKSPerTiepe.iteritems():
            totalsKS = {}
            totalsKS['geboekt'] = 0
            totalsKS['obligo'] = 0
            totalsKS['plan'] = 0
            for tiepe, regellist in regelsPerTiepe.iteritems():
                totalsKS[tiepe] = regellist.total()
                totals[tiepe] += regellist.total()

                for regel in regellist.regels:
                    regel.kosten = moneyfmt(regel.kosten, places=2, dp='.')

                KSname = self.kostenSoorten[kostenSoort]
                KSname = str(kostenSoort) +' - ' + KSname.decode('ascii', 'replace').encode('utf-8')
                regelshtml.append(render.regels(self.name, kostenSoort, KSname, totalsKS, regellist.regels, unfolded))

        if depth <= maxdepth:
            unfolded = True
        else:
            unfolded = False

        html = render.grootboekgroep(self.name, self.descr, groups, regelshtml, unfolded, totals, depth)

        return html


    def walk_tree(self, maxdepth):
        if self.level <= maxdepth:
            self.druk_af()
            for child in self.children:
                child.walk_tree(maxdepth)


    def walk_levels(self):
        levels = {self.level}
        if self.children:
            for child in self.children:
                levels.update(child.walk_levels())

        return levels

    # returns a list of all nodes from self
    # that have requested level
    def find_level(self, level):
        levels = []
        for child in self.children:
            levels = levels + child.find_level(level)

        if self.level == level:
            levels.append(self)

        return levels

    def find(self, name):
        if self.name == name:
            return self
        elif self.children:
            for child in self.children:
                result = child.find(name)
                if result != '':
                    return result
        else:
            return ''

    def assign_regels_recursive(self,regels):
        for child in self.children:
            child.assign_regels_recursive(regels)

        for key in regels.keys():
            filteredRegels = regels[key].copy()
            filteredRegels.filter_regels_by_attribute('kostensoort', self.kostenSoorten.keys())
            self.regels[key] = filteredRegels


  # Set totals per key [obligo, plan, etc.], and per ks for each node
    def set_totals(self, periodeRequested=range(0,16)):
        self.totaalTree = {}
        self.totaalNodePerKS = {}
        self.totaalTree['geboekt'] = 0
        self.totaalTree['obligo'] = 0
        self.totaalTree['plan'] = 0

        # For each key [begroot, plan, ] split into ks and periode
        for key in self.regels.keys():
            regelsPerKS = self.regels[key].split_by_regel_attributes(['kostensoort', 'periode'])

            # Walk over all ks, make sure there is a dict item
            for ks, regelsPerPeriode in regelsPerKS.iteritems():
                if key not in self.totaalNodePerKS:
                    self.totaalNodePerKS[key] = {}

                # walk over all periodes, make sure there is a dict item only if the period matches
                for periode, regellist in regelsPerPeriode.iteritems():
                    if periode in periodeRequested:
                        if ks not in self.totaalNodePerKS[key]:
                            self.totaalNodePerKS[key][ks] = 0
                        self.totaalNodePerKS[key][ks] += regellist.total()

                        # add the all the ks for every valid period to the total
                        self.totaalTree[key] += self.totaalNodePerKS[key][ks]

        #add childrens totaltree's
        for child in self.children:
            totaalChildTree = child.set_totals(periodeRequested=periodeRequested)
            for key in totaalChildTree.keys():
                self.totaalTree[key] += totaalChildTree[key]

        return self.totaalTree


    # Creates a list of all levels in the tree
    # for example: [1, 5, 12, 40]
    def list_levels(self, levels):
        for child in self.children:
            levels = child.list_levels(levels)

{'fraction_non_alphanumeric': 0.09829000417072153, 'fraction_numerical': 0.0034756012790212705, 'mean_word_length': 1.9799088649544325, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5671573', 'n_tokens_mistral': 4508, 'n_tokens_neox': 4323, 'n_words': 1101}