Ejemplo n.º 1
0
    def __init__(self,
                 data,
                 title=_notitle,
                 context={},
                 copy=1,
                 replacedollar=1,
                 columncallback="__column__",
                 **columns):
        # build a nice title
        if title is _notitle:
            items = list(columns.items())
            items.sort(
            )  # we want sorted items (otherwise they would be unpredictable scrambled)
            self.title = "%s: %s" % (text.escapestring(
                data.title or "unkown source"), ", ".join([
                    "%s=%s" %
                    (text.escapestring(key), text.escapestring(str(value)))
                    for key, value in items
                ]))
        else:
            self.title = title

        self.orgdata = data
        self.defaultstyles = self.orgdata.defaultstyles

        # analyse the **columns argument
        self.columns = {}
        for columnname, value in list(columns.items()):
            # search in the columns dictionary
            try:
                self.columns[columnname] = self.orgdata.columns[value]
            except KeyError:
                # search in the columndata list
                try:
                    self.columns[columnname] = self.orgdata.columndata[value]
                except (AttributeError, TypeError):
                    # value was not an valid column identifier
                    # i.e. take it as a mathematical expression
                    if replacedollar:
                        m = _columnintref.search(value)
                        while m:
                            value = "%s%s(%s)%s" % (value[:m.start(
                            )], columncallback, m.groups()[0], value[m.end():])
                            m = _columnintref.search(value)
                        value = value.replace("$", columncallback)
                    expression = compile(value.strip(), __file__, "eval")
                    context = context.copy()
                    context[columncallback] = self.columncallback
                    if self.orgdata.columns:
                        key, columndata = list(self.orgdata.columns.items())[0]
                        count = len(columndata)
                    elif self.orgdata.columndata:
                        count = len(self.orgdata.columndata[0])
                    else:
                        count = 0
                    newdata = []
                    for i in range(count):
                        self.columncallbackcount = i
                        for key, values in list(self.orgdata.columns.items()):
                            context[key] = values[i]
                        try:
                            newdata.append(
                                eval(expression, _mathglobals, context))
                        except (ArithmeticError, ValueError):
                            newdata.append(None)
                    self.columns[columnname] = newdata

        if copy:
            # copy other, non-conflicting column names
            for columnname, columndata in list(self.orgdata.columns.items()):
                if columnname not in self.columns:
                    self.columns[columnname] = columndata

        self.columnnames = list(self.columns.keys())
Ejemplo n.º 2
0
    def __init__(
        self, data, title=_notitle, context={}, copy=1, replacedollar=1, columncallback="__column__", **columns
    ):
        # build a nice title
        if title is _notitle:
            items = columns.items()
            items.sort()  # we want sorted items (otherwise they would be unpredictable scrambled)
            self.title = "%s: %s" % (
                text.escapestring(data.title or "unkown source"),
                ", ".join(["%s=%s" % (text.escapestring(key), text.escapestring(str(value))) for key, value in items]),
            )
        else:
            self.title = title

        self.orgdata = data
        self.defaultstyles = self.orgdata.defaultstyles

        # analyse the **columns argument
        self.columns = {}
        for columnname, value in columns.items():
            # search in the columns dictionary
            try:
                self.columns[columnname] = self.orgdata.columns[value]
            except KeyError:
                # search in the columndata list
                try:
                    self.columns[columnname] = self.orgdata.columndata[value]
                except (AttributeError, TypeError):
                    # value was not an valid column identifier
                    # i.e. take it as a mathematical expression
                    if replacedollar:
                        m = _columnintref.search(value)
                        while m:
                            value = "%s%s(%s)%s" % (value[: m.start()], columncallback, m.groups()[0], value[m.end() :])
                            m = _columnintref.search(value)
                        value = value.replace("$", columncallback)
                    expression = compile(value.strip(), __file__, "eval")
                    context = context.copy()
                    context[columncallback] = self.columncallback
                    if self.orgdata.columns:
                        key, columndata = self.orgdata.columns.items()[0]
                        count = len(columndata)
                    elif self.orgdata.columndata:
                        count = len(self.orgdata.columndata[0])
                    else:
                        count = 0
                    newdata = []
                    for i in xrange(count):
                        self.columncallbackcount = i
                        for key, values in self.orgdata.columns.items():
                            context[key] = values[i]
                        try:
                            newdata.append(eval(expression, _mathglobals, context))
                        except (ArithmeticError, ValueError):
                            newdata.append(None)
                    self.columns[columnname] = newdata

        if copy:
            # copy other, non-conflicting column names
            for columnname, columndata in self.orgdata.columns.items():
                if not self.columns.has_key(columnname):
                    self.columns[columnname] = columndata

        self.columnnames = self.columns.keys()