예제 #1
0
 def test_cells_to_range_weird(self):
     cells = [(3, 4), (4, 4), (5, 4)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'C2:E2')
예제 #2
0
 def test_cells_to_range_skip(self):
     cells = [(3, 6), (4, 6), (5, 6), (7, 6)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'C6:E6,G6')
예제 #3
0
 def test_cells_to_range_block_extra_h(self):
     #            E       F       E       F       G
     cells = [(2, 5), (2, 6), (3, 5), (3, 6), (2, 7)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'E2:G2,E3:F3')
예제 #4
0
 def test_cells_to_range_block_extra_v(self):
     cells = [(2, 5), (2, 6), (3, 5), (3, 6), (4, 6)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'E2:F3,F4')
예제 #5
0
 def test_cells_to_range_block(self):
     cells = [(2, 5), (2, 6), (3, 5), (3, 6)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'E2:F3')
예제 #6
0
 def test_cells_to_range_disj(self):
     cells = [(1, 2), (1, 3), (1, 5)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'B1:C1,E1')
예제 #7
0
 def test_cells_to_range_v(self):
     cells = [(1, 2), (2, 2), (3, 2)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'B1:B3')
예제 #8
0
 def test_cells_to_range(self):
     cells = [(1, 2), (1, 3), (1, 4)]
     r = cells_to_range(cells)
     self.assertEqual(r, 'B1:D1')
예제 #9
0
def fill_blocks(blocks, output_blocks, assignment):
    min_x = min(assignment[block.x1] for block in output_blocks) + 0
    min_y = min(assignment[block.y1] for block in output_blocks) + 0
    max_width = max(assignment[block.x2]
                    for block in output_blocks) + 2 - min_x
    max_height = max(assignment[block.y2]
                     for block in output_blocks) + 2 - min_y
    print('max_width, max_height', max_width, max_height, min_x, min_y)
    x = {c: [None for _ in range(max_height)] for c in range(max_width)}

    from openpyxl import Workbook
    wb = Workbook()
    ws = wb.active

    for nblock, block in enumerate(output_blocks):
        (ids, x1, y1, x2, y2, width, height, original_blocks, dependent_blocks) = \
            block.id, block.x1, block.y1, block.x2, block.y2, block.width, block.height, block.original, block.dependencies
        print(nblock, '/', len(output_blocks), '  ', (width, height),
              original_blocks)
        # TODO how to disambiguoate?
        original_block = original_blocks[0]
        if isinstance(original_block, Block):
            # Fill in cells
            for cell in original_block.cells:
                di, dj = cell.original_i - original_block.x1o, cell.original_j - original_block.y1o
                # Note: other way around (row, column)! None - not for dicts
                x[assignment[x1] + di - min_x][assignment[y1] + dj -
                                               min_y] = cell.v
                new_cell = ws.cell(row=assignment[y1] + dj - min_y + 1,
                                   column=assignment[x1] + di - min_x + 1,
                                   value=cell.v)
                new_cell.border = _new_border(di, dj, width, height)
        else:  # isinstance(original_block, FormulaBlockVertical):
            # Fill in formulas
            for i in range(assignment[x1], assignment[x2] + 1):
                for j in range(assignment[y1], assignment[y2] + 1):
                    args = {}
                    di = i - assignment[x1]
                    dj = j - assignment[y1]
                    for range_name, v in original_block.dependencies.items():
                        print()
                        print('Range n', range_name, v, dependent_blocks)
                        # TODO think below case cannot happen?
                        # Note: it happens when the argument isn't used for placing
                        if isinstance(next(iter(v), None), Block):
                            dblocks = v
                            print('not placing', range_name)
                            icells = dependent_intersection(
                                original_block, dblocks, di, dj, assignment)
                            drange = ([(ii - min_x + 1, ij - min_y + 1)
                                       for (ii, ij) in icells])
                            sorted_drange = sorted(list(set(drange)))
                            print('block drange', drange)
                            range_string = cells_to_range(sorted_drange)
                            print('block range_string', range_string)
                            args[range_name] = range_string
                        if isinstance(next(iter(v), None), str):
                            drange = []
                            if not range_name in dependent_blocks:
                                print(
                                    'something went wrong with formulae - skipping this block'
                                )
                                break
                            dblocks = dependent_blocks[range_name]
                            print('rogo', original_block, range_name)
                            icells = dependent_intersection(
                                original_block, dblocks, di, dj, assignment)
                            drange = ([(ii - min_x + 1, ij - min_y + 1)
                                       for (ii, ij) in icells])
                            print('drange', drange)
                            sorted_drange = sorted(list(set(drange)))
                            print('sdr', v)
                            print('sdr', sorted_drange)
                            range_string = cells_to_range(sorted_drange)
                            print('range_string', range_string)
                            args[range_name] = range_string
                        # Else the block is probably empty?

                    print('adding', args)
                    try:
                        new_value = original_block.template.format(**args)
                    except KeyError:
                        print(
                            'something went wrong with formulae - skipping this block'
                        )
                        new_value = ''

                    x[i - min_x][j - min_y] = new_value
                    new_cell = ws.cell(row=j - min_y + 1,
                                       column=i - min_x + 1,
                                       value=new_value)
                    new_cell.border = _new_border(di, dj, width, height)

    print('converting')
    df = pandas.DataFrame(x)
    return wb, df