def compact_dated_node_json(dated_node_json, info_ind, code = None):
    node_type = dated_node_json['@type']
    if node_type == u'Node':
        compact_node = CompactNode()
        if code is None:
            # Root node always contains a datesim.
            compact_node.datesim = datetime.date(*(int(fragment) for fragment in dated_node_json['datesim'].split('-')))
        compact_node_dict = compact_node.__dict__
        for key, value in dated_node_json['children'].iteritems():
            compact_node_dict[key] = compact_dated_node_json(value, info_ind, code = key)
        return compact_node
    if node_type == u'Parameter':
        return dated_node_json.get('value')
    elif node_type == 'Scale':
        bareme = Bareme(name = code, option = dated_node_json.get('option'))
        for dated_slice_json in dated_node_json['slices']:
            base = dated_slice_json.get('base', 1)
            rate = dated_slice_json.get('rate')
            threshold = dated_slice_json.get('threshold')
            if rate is not None and threshold is not None:
                bareme.addTranche(threshold, rate * base)
        bareme.marToMoy()
        return bareme
    else:
        assert node_type == u'Generation'
        generation = Generation(name = code, option = dated_node_json.get('option'), control = dated_node_json.get('control'))
        for dated_slice_json in dated_node_json['slices']:
            val = dated_slice_json.get('valeur')
            threshold = dated_slice_json.get('threshold')
            if val is not None and threshold is not None:
                generation.addTranche(threshold, val)
        return valbytranches(generation, info_ind)
def compact_long_dated_node_json(long_node_json, code = None):
    node_type = long_node_json['@type']
    if node_type == u'Node':
        compact_node_long = CompactNode()
        if code is None:
            # Root node always contains a datesim.
            compact_node_long.datesim = datetime.date(*(int(fragment) for fragment in long_node_json['datesim'].split('-')))
        compact_node_long_dict = compact_node_long.__dict__
        for key, value in long_node_json['children'].iteritems():
            val = compact_long_dated_node_json(value, code = key)
            if val:
                compact_node_long_dict[key] = val
        return compact_node_long
    if node_type in(u'Parameter', u'Scale', u'Generation'):
        pass
    elif node_type == u'LongitudinalParam':
        return long_node_json.get('values')
       
    elif node_type == u'LongitudinalScale':
        long = long_node_json.get('values')
        long_bareme = {}
        for date in long.keys():
            dated_node_json= long[date]
            bareme = Bareme(name = code, option = long_node_json.get('option'))
            for dated_slice_json in dated_node_json:
                base = dated_slice_json.get('base', 1)
                rate = dated_slice_json.get('rate')
                threshold = dated_slice_json.get('threshold')
                if rate is not None and threshold is not None:
                    bareme.addTranche(threshold, rate * base)
                bareme.marToMoy()
            long_bareme[date] = bareme
        return long_bareme
def compact_long_dated_node_json(long_node_json, code=None):
    node_type = long_node_json['@type']
    if node_type == u'Node':
        compact_node_long = CompactNode()
        if code is None:
            # Root node always contains a datesim.
            compact_node_long.datesim = datetime.date(
                *(int(fragment)
                  for fragment in long_node_json['datesim'].split('-')))
        compact_node_long_dict = compact_node_long.__dict__
        for key, value in long_node_json['children'].iteritems():
            val = compact_long_dated_node_json(value, code=key)
            if val:
                compact_node_long_dict[key] = val
        return compact_node_long
    if node_type in (u'Parameter', u'Scale', u'Generation'):
        pass
    elif node_type == u'LongitudinalParam':
        return long_node_json.get('values')

    elif node_type == u'LongitudinalScale':
        long = long_node_json.get('values')
        long_bareme = {}
        for date in long.keys():
            dated_node_json = long[date]
            bareme = Bareme(name=code, option=long_node_json.get('option'))
            for dated_slice_json in dated_node_json:
                base = dated_slice_json.get('base', 1)
                rate = dated_slice_json.get('rate')
                threshold = dated_slice_json.get('threshold')
                if rate is not None and threshold is not None:
                    bareme.addTranche(threshold, rate * base)
                bareme.marToMoy()
            long_bareme[date] = bareme
        return long_bareme
def compact_dated_node_json(dated_node_json, info_ind, code=None):
    node_type = dated_node_json['@type']
    if node_type == u'Node':
        compact_node = CompactNode()
        if code is None:
            # Root node always contains a datesim.
            compact_node.datesim = datetime.date(
                *(int(fragment)
                  for fragment in dated_node_json['datesim'].split('-')))
        compact_node_dict = compact_node.__dict__
        for key, value in dated_node_json['children'].iteritems():
            compact_node_dict[key] = compact_dated_node_json(value,
                                                             info_ind,
                                                             code=key)
        return compact_node
    if node_type == u'Parameter':
        return dated_node_json.get('value')
    elif node_type == 'Scale':
        bareme = Bareme(name=code, option=dated_node_json.get('option'))
        for dated_slice_json in dated_node_json['slices']:
            base = dated_slice_json.get('base', 1)
            rate = dated_slice_json.get('rate')
            threshold = dated_slice_json.get('threshold')
            if rate is not None and threshold is not None:
                bareme.addTranche(threshold, rate * base)
        bareme.marToMoy()
        return bareme
    else:
        assert node_type == u'Generation'
        generation = Generation(name=code,
                                option=dated_node_json.get('option'),
                                control=dated_node_json.get('control'))
        for dated_slice_json in dated_node_json['slices']:
            val = dated_slice_json.get('valeur')
            threshold = dated_slice_json.get('threshold')
            if val is not None and threshold is not None:
                generation.addTranche(threshold, val)
        return valbytranches(generation, info_ind)