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_array(self):
     test_sample = {
         "array": {
             u'result': [[u'X', u'Y', u'Z'],
                         [1.0, 2.0, 3.0],
                         [4.0, 5.0, 6.0]]},
         "dict": {
             u'result': {
                 u'Y': [2.0, 5.0],
                 u'X': [1.0, 4.0],
                 u'Z': [3.0, 6.0]
             }},
         "records": {
             u'result': [
                 {u'Y': 2.0, u'X': 1.0, u'Z': 3.0},
                 {u'Y': 5.0, u'X': 4.0, u'Z': 6.0}
             ]}
     }
     for struct_type in test_sample.keys():
         io = BytesIO()
         sheet = pe.Sheet(self.data)
         sheet.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post(
             '/respond/%s' % struct_type, buffered=True,
             data={"file": (io, "test.xls")},
             content_type="multipart/form-data")
         expected = test_sample[struct_type]
         assert json.loads(response.data.decode('utf-8')) == expected
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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
 def test_book(self):
     test_sample = ["book", "book_dict"]
     expected = {u'result': {u'Sheet1': [[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0]], u'Sheet3': [[u'X', u'Y', u'Z'], [1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]], u'Sheet2': [[4.0, 4.0, 4.0, 4.0], [5.0, 5.0, 5.0, 5.0], [6.0, 6.0, 6.0, 6.0]]}}
     for struct_type in test_sample:
         io = BytesIO()
         book = pe.Book(self.content)
         book.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post('/respond/%s' % struct_type, buffered=True,
                                  data={"file": (io, "test.xls")},
                                  content_type="multipart/form-data")
         assert json.loads(response.data.decode('utf-8')) == expected
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)
 def test_book(self):
     for struct_type in ["book", "book_dict"]:
         io = BytesIO()
         book = pe.Book(self.content)
         book.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post('/exchange/%s' % struct_type,
                                  buffered=True,
                                  data={"file": (io, "test.xls")},
                                  content_type="multipart/form-data")
         assert response.content_type == "application/vnd.ms-excel"
         book2 = pe.load_book_from_memory('xls', response.data)
         assert book2.to_dict() == self.content
Example #13
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 #14
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 #15
0
 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())
Example #16
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
 def test_array(self):
     for struct_type in ["array", "dict", "records"]:
         print("Testing %s" % struct_type)
         io = BytesIO()
         sheet = pe.Sheet(self.data)
         sheet.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post('/exchange/%s' % struct_type,
                                  buffered=True,
                                  data={"file": (io, "test.xls")},
                                  content_type="multipart/form-data")
         assert response.content_type == "application/vnd.ms-excel"
         sheet = pe.load_from_memory('xls', response.data)
         assert sheet.to_array() == self.data
Example #18
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 #19
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 ['xlsx']:#FILE_TYPE_MIME_TABLE.keys():
         file_name = 'test.%s' % upload_file_type
         for download_file_type in ['tsv']:#FILE_TYPE_MIME_TABLE.keys():
             print("Uploading %s Downloading %s" % (upload_file_type, download_file_type))
             io = BytesIO()
             sheet = pe.Sheet(self.data)
             sheet.save_to_memory(upload_file_type, io)
             io.seek(0)
             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.load_from_memory(download_file_type, response.data)
             sheet.format(int)
             array = sheet.to_array()
             assert array == self.data
 def test_array(self):
     test_sample = {
         "array": {
             u"result": [
                 [u"X", u"Y", u"Z"],
                 [1.0, 2.0, 3.0],
                 [4.0, 5.0, 6.0],
             ]
         },
         "dict": {
             u"result": {
                 u"Y": [2.0, 5.0],
                 u"X": [1.0, 4.0],
                 u"Z": [3.0, 6.0],
             }
         },
         "records": {
             u"result": [
                 {
                     u"Y": 2.0,
                     u"X": 1.0,
                     u"Z": 3.0
                 },
                 {
                     u"Y": 5.0,
                     u"X": 4.0,
                     u"Z": 6.0
                 },
             ]
         },
     }
     for struct_type in test_sample.keys():
         io = BytesIO()
         sheet = pe.Sheet(self.data)
         sheet.save_to_memory("xls", io)
         io.seek(0)
         response = self.app.post(
             "/respond/%s" % struct_type,
             buffered=True,
             data={"file": (io, "test.xls")},
             content_type="multipart/form-data",
         )
         expected = test_sample[struct_type]
         assert json.loads(response.data.decode("utf-8")) == expected
Example #22
0
 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
 def test_book(self):
     test_sample = ["book", "book_dict"]
     expected = {
         'result': {
             'Sheet1': [[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0],
                        [3.0, 3.0, 3.0, 3.0]],
             'Sheet3': [['X', 'Y', 'Z'], [1.0, 4.0, 7.0], [2.0, 5.0, 8.0],
                        [3.0, 6.0, 9.0]],
             'Sheet2': [[4.0, 4.0, 4.0, 4.0], [5.0, 5.0, 5.0, 5.0],
                        [6.0, 6.0, 6.0, 6.0]]
         }
     }
     for struct_type in test_sample:
         io = BytesIO()
         book = pe.Book(self.content)
         book.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post('/respond/%s' % struct_type,
                                  buffered=True,
                                  data={"file": (io, "test.xls")},
                                  content_type="multipart/form-data")
         assert json.loads(response.data.decode('utf-8')) == expected
Example #24
0
def create_sample_file2_in_memory(file_type):
    """
    1,2,3,4
    5,6,7,8
    9,10,11,12
    """
    io = BytesIO()
    w = pe.Writer((file_type, io))
    table = []
    for i in [0, 4, 8]:
        array = [i + 1, i + 2, i + 3, i + 4]
        table.append(array)
    w.write_array(table)
    w.close()
    return io
 def test_array(self):
     test_sample = {
         "array": {
             u'result': [[u'X', u'Y', u'Z'], [1.0, 2.0, 3.0],
                         [4.0, 5.0, 6.0]]
         },
         "dict": {
             u'result': {
                 u'Y': [2.0, 5.0],
                 u'X': [1.0, 4.0],
                 u'Z': [3.0, 6.0]
             }
         },
         "records": {
             u'result': [{
                 u'Y': 2.0,
                 u'X': 1.0,
                 u'Z': 3.0
             }, {
                 u'Y': 5.0,
                 u'X': 4.0,
                 u'Z': 6.0
             }]
         }
     }
     for struct_type in test_sample.keys():
         io = BytesIO()
         sheet = pe.Sheet(self.data)
         sheet.save_to_memory('xls', io)
         io.seek(0)
         response = self.app.post('/respond/%s' % struct_type,
                                  buffered=True,
                                  data={"file": (io, "test.xls")},
                                  content_type="multipart/form-data")
         expected = test_sample[struct_type]
         assert json.loads(response.data.decode('utf-8')) == expected
Example #26
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")