Beispiel #1
0
    def setUp(self):
        titles = (
            Title("id"),
            Title("name"),
            Title("age"),
            Title("grade"),
        )
        self.table = ListTable("test_table", titles)

        self.data = [(1, "Sam", 26, "A"), (2, "Jack", 31, "B"),
                     (3, "James", 32, "C")]

        self.wrapped_table = ListTable("test_table", titles, self.data)
Beispiel #2
0
    def setUp(self):
        self.students_list = (
            (1, u"迟宏泽", u"男", 1),
            (2, "Jack", u"男", 2),
            (3, u"韩梅梅", u"女", 3),
            (5, u"李雷", u"男", 3)
        )
        self.students_table = ListTable(
            "students",
            (
                Title("id", u"编号"),
                Title("name", u"姓名"),
                Title("gender", u"性别"),
                Title("grade", u"年级")
            ),
            self.students_list
        )

        def even_row(row_index, column_index):
            return row_index != 0 and row_index % 2 == 0

        self.even_row_style = CellStyle(even_row, {"font_color": "red"})
        self.top_three_style = CellStyle(
            ((1, 3), None), {"bg_color": "yellow"})

        self.tmp_path = []
Beispiel #3
0
    def setUp(self):
        self.table = ListTable(
            "student",
            (Title("id", u"编号"), Title("name", u"姓名"), Title("grade", u"年级")),
            ((1, u"小王", 1), (2, u"小李", 1), (3, u"小明", 2)))

        def data_row_style(row_index, row):
            if row.grade == 1:
                return "style='color:red;'"

        def data_cell_style(row_index, column_index, field_name, value):
            if field_name == "id":
                return "style='color:orange;'"

        def title_row_style(table):
            return "style='background-color:gray;'"

        def title_cell_style(column_index, title):
            if title.name == "name":
                return "style='text-decoration:underline;'"

        self.properties = {
            "table": "border=1",
            "title-row": title_row_style,
            "title-cell": title_cell_style,
            "data-row": data_row_style,
            "data-cell": data_cell_style,
        }
Beispiel #4
0
    def test_print(self):
        table = ListTable(
            "test",
            (Title("id", u"编号"), Title("name", u"姓名"), Title("grade", u"年级")),
            ((1, "Sam", "A"), (2, "Tom", "A"), (3, "Jack", "B"),
             (4, "James", "C")))

        pp = PrintTablePlugin()
        pp.execute(None, table, table, table)
Beispiel #5
0
    def execute(self, context, tables, name, titles=0, variable=None):
        """
        :param context 上下文对象
        :param name    表名
        :param titles  标题,可以是Title对象列表,也可以是数字,如果是数字那么将
                       使用对应的拼接表标题
        :param tables  要拼接的表格,接受三种格式:
                       1. 具体的Table对象
                       2. Table对象的上下文变量名
                       3. 元组,第一个元素为Table对象,后面为要拼接的属性名
        """

        result = []
        for idx, table in enumerate(tables):
            fields = []  # 要拼接的字段
            if isinstance(table, types.StringTypes):
                table = context[table]
                fields = [title.name for title in table.titles]
            elif isinstance(table, AbstractTable):
                fields = [title.name for title in table.titles]
            elif isinstance(table, SequenceCollectionType):
                table, fields = table[0], table[1:]
                if isinstance(table, types.StringTypes):
                    table = context[table]
            else:
                raise InvalidTypeException

            for row in table:
                result.append(row[fields])

            if isinstance(titles, int) and titles == idx:
                titles = [
                    title for title in table.titles if title.name in fields
                ]

        if variable is None:
            return ListTable(name, titles, result)
        else:
            context[variable] = ListTable(name, titles, result)
Beispiel #6
0
    def setUp(self):
        self.students = (
            (1, "Sam", 1),
            (2, "Jack", 2),
            (3, "James", 2),
            (4, "Peter", 1)
        )
        self.students_table = ListTable(
            "students",
            (
                Title("id", u"编号"),
                Title("name", u"姓名"),
                Title("grade", u"年级")
            ),
            self.students
        )

        self.scores = (
            (1, 100, 98, 88),
            (2, 95, 99, 99),
            (3, 96, 97, 89),
            (4, 98, 97, 99)
        )
        self.score_table = ListTable(
            "students_score",
            (
                Title("id", u"编号"),
                Title("c", u"C语言"),
                Title("java", u"Java程序设计"),
                Title("ds", u"数据结构")
            ),
            self.scores
        )

        excel_writer = ExcelWriterPlugin()
        self.path = excel_writer.execute({}, None, sheets=(
            SheetW(self.students_table),
            SheetW(self.score_table)
        ))
Beispiel #7
0
 def setUp(self):
     self.students_table = ListTable(
         "students",
         (Title("id", u"编号"), Title("name", u"姓名"), Title("grade", u"年级")),
         (
             (1, "Sam", 1),
             (2, "Jack", 1),
             (3, "James", 1),
             (4, "Betty", 2),
             (5, "Tom", 2),
             (6, "Lucy", 2),
             (7, "Jason", 3),
         ))
Beispiel #8
0
    def setUp(self):
        self.students_table = ListTable(
            "students",
            (Title("id", u"编号"), Title("name", u"姓名"), Title("grade", u"年级"),
             Title("city", u"城市"), Title("class", u"班级")), (
                 (1, "Sam", 1, 1, 2),
                 (2, "Jack", 1, 2, 3),
                 (3, "Lucy", 2, 4, 1),
                 (4, "James", 2, 3, 2),
                 (5, "Larry", 2, 10, 3),
                 (6, "Betty", 2, 10, 5),
             ))

        self.city_table = ListTable("city",
                                    (Title("id", u"编号"), Title("name", u"名称")),
                                    ((1, "Pyongyang"), (2, "Beijing"),
                                     (3, "Shanghai"), (4, "Guangzhou"),
                                     (5, "Jinan")))

        self.class_scores = ListTable("class_score", (Title(
            "grade", u"年级"), Title("class", u"班级"), Title("score", u"分数")),
                                      ((1, 1, 10), (1, 2, 20), (1, 3, 30),
                                       (2, 1, 40), (2, 2, 50), (2, 3, 60)))
Beispiel #9
0
    def execute(self,
                context,
                way,
                left,
                right,
                on,
                fields,
                name,
                titles=None,
                variable=None):
        """
        :param context 上下文对象
        :param way join方式,允许inner、left和right三种join方式
        :param on  join条件,left_column = right_column,多个条件用逗号隔开
        :param fields 结果字段
        :param name 结果表格名称
        :param titles 结果表格标题
        :param variable 用于存储的上下文变量
        """

        if way not in ("inner", "left", "right"):
            raise InvalidArgumentException(
                u"不合法的join方式'{}',只支持inner、left、right三种join方式".format(way))

        conditions = self._parse_conditions(on)

        if isinstance(left, types.StringTypes):
            left = context[left]
        if isinstance(right, types.StringTypes):
            right = context[right]

        if way == "inner":
            result = self._inner_join(left, right, fields, conditions)
        elif way == "left":
            result = self._side_join("left", left, right, fields, conditions)
        elif way == "right":
            result = self._side_join("right", right, left, fields, conditions)

        if titles is None:
            titles = tuple(Title(f.split(".")[1]) for f in fields)

        table = ListTable(name, titles, result)
        if variable:
            context[variable] = table
        else:
            return table
Beispiel #10
0
    def setUp(self):
        self.ctx = {}
        titles = (Title("id", u"编号"), Title("name", u"姓名"),
                  Title("grade", u"年级"), Title("month",
                                               u"月"), Title("score", u"得分"))

        students = ((1, "Sam", 1), (2, "James", 1), (3, "Joe", 2), (4, "Jack",
                                                                    2))

        data = []
        for i in xrange(0, len(students)):
            student = students[i]
            for m in xrange(1, 13):
                score = random.randint(0, 100)
                row = []
                row.extend(student)
                row.extend([m, score])
                data.append(row)
        random.shuffle(data)
        self.ctx["students_table"] = ListTable("students", titles, data)
Beispiel #11
0
class ListTableTestCase(GirlFriendTestCase):
    def setUp(self):
        titles = (
            Title("id"),
            Title("name"),
            Title("age"),
            Title("grade"),
        )
        self.table = ListTable("test_table", titles)

        self.data = [(1, "Sam", 26, "A"), (2, "Jack", 31, "B"),
                     (3, "James", 32, "C")]

        self.wrapped_table = ListTable("test_table", titles, self.data)

    def test_append(self):
        """测试append方法
        """
        self.table.append([1, "SamChi", 26, "A"])
        self.assertEquals((self.table[0].id, self.table[0].name,
                           self.table[0].age, self.table[0].grade),
                          (1, "SamChi", 26, "A"))

        self.failUnlessException(InvalidSizeException, ListTable.append,
                                 self.table, [2, "James", 31, "X", 0])

        self.wrapped_table.append((4, "Betty", 21, "A"))
        self.assertEquals((4, "Betty", 21, "A"), self.data[-1])
        # test multi get
        self.assertEquals(self.wrapped_table[-1]["id", "name", "age", "grade"],
                          (4, "Betty", 21, "A"))

    def test_iter(self):
        """测试迭代器
        """
        data = [(1, "Sam", 26, "A"), (2, "Jack", 31, "B"),
                (3, "James", 32, "C")]

        for rowlist in data:
            self.table.append(rowlist)

        for idx, row in enumerate(self.table):
            self.assertEquals((row.id, row.name, row.age, row.grade),
                              data[idx])
Beispiel #12
0
    def execute(self,
                context,
                from_table,
                to_table,
                title_column,
                value_column,
                title_generator=Title,
                new_title_sort=sorted,
                new_table_name=None,
                default=None,
                sum_title=None,
                avg_title=None):
        """
        :param context  上下文对象
        :param from_table  要转换的表,可以是具体的table对象,也可以是上下文变量名
        :param to_table  转换结果所保存的变量名
        :param title_column  要转换为标题的列,比如上例中的week
        :param value_column  为新列提供数据的列,比如上例中的score
        :param title_generator 新列生成器,接受一个列值作为参数,返回Title对象
                         例如: lambda week: Title("week_" + week, u"星期" + week)
        :param new_title_sort 新标题排序,接受新标题值的列表,返回排序后的结果
        :param sum_title 接受一个Title对象,如果不为None,那么将用此列保存合计
        :param avg_title 平均值
        """
        if isinstance(from_table, types.StringTypes):
            from_table = context[from_table]

        title_column_values = set()

        # 将title_column和value_column之外的列作为唯一列
        unique_fields = tuple(
            title.name for title in from_table.titles
            if title.name != title_column and title.name != value_column)

        tmp_result = defaultdict(dict)
        for row in from_table:
            unique_columns = row[unique_fields]
            title_column_value = row[title_column]
            new_column_value = row[value_column]
            tmp_result[unique_columns][title_column_value] = new_column_value
            title_column_values.add(title_column_value)

        # 生成标题列表
        title_column_values = new_title_sort(title_column_values)
        titles = [
            title for title in from_table.titles
            if title.name != title_column and title.name != value_column
        ]
        titles.extend(title_generator(v) for v in title_column_values)

        if sum_title:
            titles.append(sum_title)
        if avg_title:
            titles.append(avg_title)

        table_name = (from_table.name
                      if new_table_name is None else new_table_name)

        data = []
        for unique_columns in tmp_result:
            row = []
            row.extend(unique_columns)
            series = tuple(tmp_result[unique_columns].get(col, default)
                           for col in tmp_result[unique_columns])
            row.extend(series)
            sum_value = None
            if sum_title:
                sum_value = sum(series)
                row.append(sum_value)
            if avg_title:
                if sum_value is None:
                    sum_value = sum(series)
                avg_value = sum_value / len(series)
                row.append(avg_value)
            data.append(row)

        context[to_table] = ListTable(table_name, titles, data)