<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import template


register = template.Library()

@register.filter()
def getattribute(model, item):
    """Equivalent to getattr(model, item)

    If item has dots (eg: foo.bar.baz), recursively call getattribute():
    e = getattr(model, 'foo')
    e = getattr(e, 'bar')
    e = getattr(e, 'baz')
    Then return e, or e() if it's a callable

    """
    elements = item.split('.')
    element = elements.pop(0)
    attr = getattr(model, element, None)
    if attr is None: # end of recursion
        return attr
    if not elements: # no elements left (end of recursion)
        if callable(attr):
            try:
                return attr()
            except: # couldn't call this method without params after all
                return ''
        return attr
    return getattribute(attr, '.'.join(elements))

@register.filter()
def getvalue(dictionary, item):
    """Equivalent to dict.get(item), provided for convenience"""
    return dictionary.get(item)

@register.filter()
def nice_display(item):
    """Display a comma-separated list of models for M2M fields"""
    if hasattr(item, 'all'): # RelatedManager: display a list
        return ', '.join(map(unicode, item.all()))
    return item<commit_msg>slightly refactored 'getattribute' filter<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import template


register = template.Library()

@register.filter()
def getattribute(model, item):
    """Chained lookup of item on model

    If item has dots (eg: 'foo.bar.baz'), recursively call getattribute():
    e = getattr(model, 'foo')
    e = getattr(e, 'bar')
    e = getattr(e, 'baz')
    At each step, check if e is a callable, and if so, use e()

    """
    elements = item.split('.')
    element = elements.pop(0)
    attr = getattr(model, element, None)
    if attr is None: # end of recursion
        return
    if callable(attr):
        try:
            attr = attr()
        except: # couldn't call this method without params
            return
    if elements:
        return getattribute(attr, '.'.join(elements))
    return attr # no elements left (end of recursion)

@register.filter()
def getvalue(dictionary, item):
    """Equivalent to dict.get(item), provided for convenience"""
    return dictionary.get(item)

@register.filter()
def nice_display(item):
    """Display a comma-separated list of models for M2M fields"""
    if hasattr(item, 'all'): # RelatedManager: display a list
        return ', '.join(map(unicode, item.all()))
    return item
{'fraction_non_alphanumeric': 0.12911981388134935, 'fraction_numerical': 0.0023264831329972858, 'mean_word_length': 2.9269406392694064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7575956', 'n_tokens_mistral': 780, 'n_tokens_neox': 749, 'n_words': 259}