<commit_before>import sys
import inspect
import functools
from django import template
from django.conf import settings
from django.utils.datastructures import SortedDict
from django.utils.encoding import smart_str
from django.utils.html import escape
from django.utils.safestring import mark_safe, SafeData

register = template.Library()

#
# {% chart %}
#

@register.tag
def chart(parser, token):
    bits = iter(token.split_contents())
    name = bits.next()
    varname = None
    saveas = None
    extends = None
    for bit in bits:
        if bit == "as":
            varname = bits.next()
        elif bit == "saveas":
            raise template.TemplateSyntaxError("Sorry, 'saveas' isn't implemented yet!")
            saveas = template.Variable(bits.next())
        elif bit == "extends":
            extends = template.Variable(bits.next())
        else:
            raise template.TemplateSyntaxError("Unknown argument to '%s': '%s'" % (name, bit))
    nodelist = parser.parse("end%s" % name)
    parser.delete_first_token()
    return ChartNode(nodelist, varname, saveas, extends)

class ChartNode(template.Node):

    def __init__(self, nodelist, varname, saveas, extends):
        self.nodelist = nodelist
        self.saveas = saveas
        self.varname = varname
        self.extends = extends

    def render(self, context):
        c = Chart()
        if self.extends:
            try:
                parent = self.extends.resolve(context)
            except template.VariableDoesNotExist:
                pass
            else:
                c = parent.clone()
                
        for node in self.nodelist:
            if isinstance(node, ChartDataNode):
                c.datasets.extend(node.resolve(context))
            elif isinstance(node, ChartOptionNode):
                node.update_chart(c, context)
            elif isinstance(node, AxisNode):
                c.axes.append(node.resolve(context))

        if self.varname:
            context[self.varname] = c
            return ""
        else:
            return c.img()

class Chart(object):

    BASE = "http://chart.apis.google.com/chart"
    defaults = {
        "chs": "200x200",
        "cht": "lc"
    }

    def __init__(self):
        # Use a SortedDict for the opeions so they are added in a
        # deterministic manner; this eases things like dealing with cache keys 
        # or writing unit tests.
        self.options = SortedDict()
        self.datasets = []
        self.axes = []
        self.datarange = None
        self.alt = None

    def clone(self):
        clone = self.__class__()
        clone.options = self.options.copy()
        clone.datasets = self.datasets[:]
        clone.axes = self.axes[:]
        return clone

    def img(self):
        url = self.url()
        width, height = self.options["chs"].split("x")
        alt = 'alt="%s" ' % escape(self.alt) if self.alt else ''
        s = mark_safe('<img src="%s" width="%s" height="%s" %s/>' % (escape(url), width, height, alt))
        return s

    def url(self):
        if self.options.get('cht', None) == 't':
            self.datasets.append(self.options.pop("_mapdata"))

        # Figure out the chart's data range
        if not self.datarange:
            maxvalue = max(max(d) for d in self.datasets if d)
            minvalue = min(min(d) for d in self.datasets if d)
            self.datarange = (minvalue, maxvalue)
        
        # Encode data
        if "chds" in self.options or self.options.get('cht', None) == 'gom': 
            # text encoding if scaling provided, or for google-o-meter type
            data = "|".join(encode_text(d) for d in self.datasets)<commit_msg>Another python2.5 removal.<commit_after>import sys
import inspect
import functools
from django import template
from django.conf import settings
from django.utils.datastructures import SortedDict
from django.utils.encoding import smart_str
from django.utils.html import escape
from django.utils.safestring import mark_safe, SafeData

register = template.Library()

#
# {% chart %}
#

@register.tag
def chart(parser, token):
    bits = iter(token.split_contents())
    name = bits.next()
    varname = None
    saveas = None
    extends = None
    for bit in bits:
        if bit == "as":
            varname = bits.next()
        elif bit == "saveas":
            raise template.TemplateSyntaxError("Sorry, 'saveas' isn't implemented yet!")
            saveas = template.Variable(bits.next())
        elif bit == "extends":
            extends = template.Variable(bits.next())
        else:
            raise template.TemplateSyntaxError("Unknown argument to '%s': '%s'" % (name, bit))
    nodelist = parser.parse("end%s" % name)
    parser.delete_first_token()
    return ChartNode(nodelist, varname, saveas, extends)

class ChartNode(template.Node):

    def __init__(self, nodelist, varname, saveas, extends):
        self.nodelist = nodelist
        self.saveas = saveas
        self.varname = varname
        self.extends = extends

    def render(self, context):
        c = Chart()
        if self.extends:
            try:
                parent = self.extends.resolve(context)
            except template.VariableDoesNotExist:
                pass
            else:
                c = parent.clone()
                
        for node in self.nodelist:
            if isinstance(node, ChartDataNode):
                c.datasets.extend(node.resolve(context))
            elif isinstance(node, ChartOptionNode):
                node.update_chart(c, context)
            elif isinstance(node, AxisNode):
                c.axes.append(node.resolve(context))

        if self.varname:
            context[self.varname] = c
            return ""
        else:
            return c.img()

class Chart(object):

    BASE = "http://chart.apis.google.com/chart"
    defaults = {
        "chs": "200x200",
        "cht": "lc"
    }

    def __init__(self):
        # Use a SortedDict for the opeions so they are added in a
        # deterministic manner; this eases things like dealing with cache keys 
        # or writing unit tests.
        self.options = SortedDict()
        self.datasets = []
        self.axes = []
        self.datarange = None
        self.alt = None

    def clone(self):
        clone = self.__class__()
        clone.options = self.options.copy()
        clone.datasets = self.datasets[:]
        clone.axes = self.axes[:]
        return clone

    def img(self):
        url = self.url()
        width, height = self.options["chs"].split("x")
        if self.alt:
            alt = 'alt="%s" ' % escape(self.alt)
        else:
            alt = ''
        s = mark_safe('<img src="%s" width="%s" height="%s" %s/>' % (escape(url), width, height, alt))
        return s

    def url(self):
        if self.options.get('cht', None) == 't':
            self.datasets.append(self.options.pop("_mapdata"))

        # Figure out the chart's data range
        if not self.datarange:
            maxvalue = max(max(d) for d in self.datasets if d)
            minvalue = min(min(d) for d in self.datasets if d)
            self.datarange = (minvalue, maxvalue)
        
        # Encode data
        if "chds" in self.options or self.options.get('cht', None) == 'gom': 
            # text encoding if scaling provided, or for google-o-meter type
            data = "|".join(encode_text(d) for d in self.datasets)
{'fraction_non_alphanumeric': 0.11384199864956111, 'fraction_numerical': 0.0018906144496961513, 'mean_word_length': 2.054020618556701, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 5, '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': '4494407', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2114, 'n_words': 582}