from django import template

register = template.Library()


@register.simple_tag(takes_context=True)
def get_values(context, attribute_path, set_index='*', index='*'):
    if set_index == '*' and index == '*':
        try:
            return context['values'][attribute_path]
        except KeyError:
            return []

    elif set_index == '*':
        try:
            value_sets = context['values'][attribute_path]
        except KeyError:
            return []

        values = []
        for value_set in value_sets:
            try:
                values.append(value_set[index])
            except IndexError:
                values.append('')
        values = sorted(values, reverse=False, key=lambda x: x.id)
        return values

    elif index == '*':
        try:
            return context['values'][attribute_path][set_index]
        except (KeyError, IndexError):
            return []
    else:
        try:
            return context['values'][attribute_path][set_index][index]
        except (KeyError, IndexError):
            return ''


@register.simple_tag(takes_context=True)
def get_set_values(context, set, attribute_path, index='*'):
    return get_values(context, attribute_path, set.set_index, index)


@register.simple_tag(takes_context=True)
def get_set_numbers(context, set, attribute_path, index='*'):
    numbers = []
    vals = get_values(context, attribute_path, set.set_index, index)
    for val in vals:
        i = 0
        try:
            i = val.text
        except AttributeError:
            numbers.append(i)
        else:
            numbers.append(make_number(i))
    return numbers


@register.simple_tag(takes_context=True)
def get_set_sum(context, set, attribute_path, index='*'):
    numbers = get_set_numbers(context, set, attribute_path, index='*')
    return sum(numbers)


@register.simple_tag(takes_context=True)
def get_value(context, attribute_path, set_index=0, index=0):
    return get_values(context, attribute_path, set_index, index)


@register.simple_tag(takes_context=True)
def get_number(context, attribute_path, set_index=0, index=0):
    i = 0
    val = get_values(context, attribute_path, set_index, index)
    try:
        i = val.text
    except AttributeError:
        return i
    else:
        return make_number(i)


@register.simple_tag(takes_context=True)
def get_set_value(context, set, attribute_path, index=0):
    return get_set_values(context, set, attribute_path, index)


@register.simple_tag(takes_context=True)
def get_set(context, attribute_path):
    id_path = attribute_path.rstrip('/') + '/id'
    return get_values(context, id_path, index=0)


@register.inclusion_tag('views/tags/value.html', takes_context=True)
def render_value(context, attribute_path, set_index=0, index=0):
    try:
        return {'value': get_values(context, attribute_path, set_index, index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value.html', takes_context=True)
def render_set_value(context, set, attribute_path, index=0):
    try:
        return {'value': get_values(context, attribute_path, set.set_index, index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_list.html', takes_context=True)
def render_value_list(context, attribute_path, set_index=0):
    try:
        return {'values': get_values(context, attribute_path, set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_list.html', takes_context=True)
def render_set_value_list(context, set, attribute_path):
    try:
        return {'values': get_values(context, attribute_path, set.set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_inline_list.html', takes_context=True)
def render_value_inline_list(context, attribute_path, set_index=0):
    try:
        return {'values': get_values(context, attribute_path, set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_inline_list.html', takes_context=True)
def render_set_value_inline_list(context, set, attribute_path):
    try:
        return {'values': get_values(context, attribute_path, set.set_index)}
    except KeyError:
        return None


def make_number(val):
    val = val.replace(',', '.')
    try:
        return float(val)
    except ValueError:
        pass
    try:
        return int(val)
    except ValueError:
        return 0

@@ -4271,36 +4271,113 @@
     
-val = val.replace(',', '.')%0A
+if isinstance(val, str):%0A        val = val.replace(',', '.')%0A    if isinstance(val, float) is False:%0A    
     
@@ -4373,32 +4373,36 @@
 e:%0A        try:%0A
+    
         return f
@@ -4404,19 +4404,21 @@
 urn 
-floa
+in
 t(val)%0A
+    
     
@@ -4444,16 +4444,20 @@
         
+ 
+   
 pass%0A   
@@ -4453,24 +4453,28 @@
    pass%0A    
+  
+  
 try:%0A       
@@ -4470,33 +4470,39 @@
 ry:%0A        
+  
+  
 return 
-in
+floa
 t(val)%0A    e
@@ -4496,24 +4496,28 @@
 at(val)%0A    
+ 
+   
 except Value
@@ -4527,21 +4527,25 @@
 or:%0A        
+    
 return 0%0A
@@ -4544,8 +4544,37 @@
 eturn 0%0A
+    else:%0A        return val%0A

Improve resilience