Example #1
0
 def setUp(self):
     file_type = "xlsx"
     io = BytesIO()
     self.content = [["X", "Y", "Z"], [1, 11, 12], [2, 21, 22], [3, 31, 32], [4, 41, 42], [5, 51, 52]]
     sheet = pe.Sheet(pe.transpose(self.content), name_rows_by_column=0)
     sheet.save_to_memory(file_type, io)
     self.test_tuple = (file_type, io.getvalue())
 def test_download(self):
     for upload_file_type in FILE_TYPE_MIME_TABLE.keys():
         file_name = "issue15.test.%s" % upload_file_type
         for download_file_type in FILE_TYPE_MIME_TABLE.keys():
             print("Uploading %s Downloading %s" %
                   (upload_file_type, download_file_type))
             io = pe.save_as(dest_file_type=upload_file_type,
                             array=self.data)
             if not PY2:
                 if not isinstance(io, BytesIO):
                     io = BytesIO(io.getvalue().encode("utf-8"))
             response = self.app.post(
                 "/switch/%s" % download_file_type,
                 buffered=True,
                 data={"file": (io, file_name)},
                 content_type="multipart/form-data",
             )
             eq_(
                 response.content_type,
                 FILE_TYPE_MIME_TABLE[download_file_type],
             )
             sheet = pe.get_sheet(file_type=download_file_type,
                                  file_content=response.data)
             sheet.format(int)
             array = sheet.to_array()
             eq_(array, self.data)
Example #3
0
 def setUp(self):
     file_type = "xlsx"
     io = BytesIO()
     self.content = [["X", "Y", "Z"], [1, 11, 12], [2, 21, 22], [3, 31, 32],
                     [4, 41, 42], [5, 51, 52]]
     sheet = pe.Sheet(pe.transpose(self.content), name_rows_by_column=0)
     sheet.save_to_memory(file_type, io)
     self.test_tuple = (file_type, io.getvalue())
Example #4
0
 def test_tsvz_output_stringio2(self):
     data = [[1, 2, 3], [4, 5, 6]]
     r = pe.Sheet(data)
     io = BytesIO()
     r.save_to_memory("tsvz", io)
     r = pe.load_from_memory("tsvz", io.getvalue())
     result = ['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Example #5
0
 def test_book_save_to_stringio(self):
     data = {"Sheet 1": [[1, 2, 3], [4, 5, 6]]}
     book = pe.Book(data)
     io = BytesIO()
     book.save_to_memory("xlsm", io)
     b = pe.load_book_from_memory("xlsm", io.getvalue())
     result = [1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(b[0].enumerate())
     assert result == actual
Example #6
0
 def test_csvz_output_stringio(self):
     data = [[1, 2, 3], [4, 5, 6]]
     io = BytesIO()
     w = pe.Writer(("csvz", io))
     w.write_rows(data)
     w.close()
     r = pe.Reader(("csvz", io.getvalue()))
     result = ['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Example #7
0
 def test_book_output_stringio(self):
     data = {"Sheet 1": [[1, 2, 3], [4, 5, 6]]}
     io = BytesIO()
     w = pe.BookWriter(("xlsm", io))
     w.write_book_from_dict(data)
     w.close()
     b = pe.load_book_from_memory("xlsm", io.getvalue())
     result = [1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(b[0].enumerate())
     assert result == actual
Example #8
0
 def test_xlsm_output_stringio(self):
     data = [[1, 2, 3], [4, 5, 6]]
     io = BytesIO()
     w = pe.Writer(("xlsm", io))
     w.write_rows(data)
     w.close()
     r = pe.load_from_memory("xlsm", io.getvalue())
     result = [1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(r.enumerate())
     assert result == actual
Example #9
0
 def test_tsvz_output_stringio2(self):
     data = [
         [1, 2, 3],
         [4, 5, 6]
     ]
     r = pe.Sheet(data)
     io = BytesIO()
     r.save_to_memory("tsvz", io)
     r = pe.load_from_memory("tsvz", io.getvalue())
     result=['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Example #10
0
 def test_xlsm_output_stringio(self):
     data = [
         [1, 2, 3],
         [4, 5, 6]
     ]
     io = BytesIO()
     w = pe.Writer(("xlsm",io))
     w.write_rows(data)
     w.close()
     r = pe.load_from_memory("xlsm", io.getvalue())
     result=[1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(r.enumerate())
     assert result == actual
Example #11
0
 def test_no_file_type(self):
     file_name = 'issue15'
     download_file_type = "xls"
     io = pe.save_as(dest_file_type="xls", array=self.data)
     if not PY2:
         if not isinstance(io, BytesIO):
             io = BytesIO(io.getvalue().encode('utf-8'))
     response = self.app.post('/switch/%s' % download_file_type,
                              buffered=True,
                              data={"file": (io, file_name)},
                              content_type="multipart/form-data")
     eq_(response.content_type, 'text/html')
     eq_(response.status_code, 400)
Example #12
0
 def test_csvz_output_stringio(self):
     data = [
         [1, 2, 3],
         [4, 5, 6]
     ]
     io = BytesIO()
     w = pe.Writer(("csvz",io))
     w.write_rows(data)
     w.close()
     r = pe.Reader(("csvz", io.getvalue()))
     result=['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Example #13
0
 def test_book_save_to_stringio(self):
     data = {
         "Sheet 1": [
             [1, 2, 3],
             [4, 5, 6]
         ]
     }
     book = pe.Book(data)
     io = BytesIO()
     book.save_to_memory("xlsm", io)
     b = pe.load_book_from_memory("xlsm", io.getvalue())
     result=[1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(b[0].enumerate())
     assert result == actual
Example #14
0
 def test_book_output_stringio(self):
     data = {
         "Sheet 1": [
             [1, 2, 3],
             [4, 5, 6]
         ]
     }
     io = BytesIO()
     w = pe.BookWriter(("xlsm",io))
     w.write_book_from_dict(data)
     w.close()
     b = pe.load_book_from_memory("xlsm", io.getvalue())
     result=[1, 2, 3, 4, 5, 6]
     actual = pe.utils.to_array(b[0].enumerate())
     assert result == actual
Example #15
0
 def setUp(self):
     file_type = "xlsx"
     io = BytesIO()
     self.content = [
         ["X", "Y", "Z"],
         [1, 11, 12],
         [2, 21, 22],
         [3, 31, 32],
         [4, 41, 42],
         [5, 51, 52]
     ]
     w = pe.Writer((file_type, io))
     w.write_columns(self.content)
     w.close()
     self.test_tuple = (file_type, io.getvalue())
 def test_download(self):
     for upload_file_type in FILE_TYPE_MIME_TABLE.keys():
         file_name = 'test.%s' % upload_file_type
         for download_file_type in FILE_TYPE_MIME_TABLE.keys():
             print("Uploading %s Downloading %s" % (upload_file_type, download_file_type))
             io = pe.save_as(dest_file_type=upload_file_type, array=self.data)
             if not PY2:
                 if not isinstance(io, BytesIO):
                     io = BytesIO(io.getvalue().encode('utf-8'))
             response = self.app.post('/switch/%s' % download_file_type,
                                      buffered=True,
                                      data={"file": (io, file_name)},
                                      content_type="multipart/form-data")
             assert response.content_type == FILE_TYPE_MIME_TABLE[download_file_type]
             sheet = pe.get_sheet(file_type=download_file_type,
                                  file_content=response.data)
             sheet.format(int)
             array = sheet.to_array()
             assert array == self.data
Example #17
0
class TestColumnFormatter:
    def setUp(self):
        self.data = {
            "1": [1, 2, 3, 4, 5, 6, 7, 8],
            "2": ["1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0"],
            "3": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8],
            "4": ["1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7.7", "8.8"],
            "5": [2, 3, 4, 5, 6, 7, 8, 9],
            "6": ["2", "3", "4", "5", "6", "7", "8", "9"]
        }
        self.io = BytesIO()
        w = pe.Writer(("xls", self.io))
        w.write_dict(self.data)
        w.close()
        self.test_tuple = ("xls", self.io.getvalue())

    def test_general_usage(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_column_format_general_usage(self):
        """Test column format function"""
        r = pe.Reader(self.test_tuple)
        r.column.format(
            0,
            str)
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        print(c1)
        print(c2)
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
            
    def test_column_format_general_usage2(self):
        """Test column format function on demand"""
        r = pe.Reader(self.test_tuple)
        r.column.format(
            0,
            str,
            on_demand=True)
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_column_format_specs(self):
        r = pe.Reader(self.test_tuple)
        r.column.format(format_specs=[[0, str], [[2,3,4], float]])
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        c1 = r.column_at(3)[1:]
        c2 = self.data["3"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_column_format_specs_on_demand(self):
        r = pe.Reader(self.test_tuple)
        r.column.format(format_specs=[[0, lambda v: int(v)+1], [[2,3,4], float]])
        c1 = r.column_at(0)[1:]
        c2 = self.data["5"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        c1 = r.column_at(3)[1:]
        c2 = self.data["3"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_one_formatter_for_two_columns(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            [0,5],
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        c1 = r.column_at(5)[1:]
        c2 = self.data["6"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    @raises(NotImplementedError)
    def test_invalid_input(self):
        pe.formatters.ColumnFormatter("world", str)

    @raises(IndexError)
    def test_invalid_input2(self):
        """Empty list"""
        pe.formatters.ColumnFormatter([], str)

    @raises(IndexError)
    def test_float_in_list(self):
        pe.formatters.ColumnFormatter([1, 1.1], str)

    def test_two_formatters(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            int))
        c1 = r.column_at(0)[1:]
        c2 = self.data["1"]
        for i in range(0, len(c1)):
            assert type(c1[i]) == int
            assert c1[i] == c2[i]

    def test_custom_func(self):
        r = pe.Reader(self.test_tuple)
        f = lambda x: int(x) + 1
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            f))
        c1 = r.column_at(0)[1:]
        c2 = self.data["5"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_custom_func_with_a_general_converter(self):
        r = pe.Reader(self.test_tuple)
        f = lambda x: int(x) + 1
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            f))
        c1 = r.column_at(0)[1:]
        c2 = self.data["5"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["6"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    @raises(NotImplementedError)
    def test_named_formatter(self):
        """Test wrong data type to update_index"""
        nrf = pe.formatters.NamedColumnFormatter("abc", str)
        nrf.update_index("abc")