def print_tests(url, grid): print(' url={}'.format(url)) print(' rows={}'.format(grid.height)) print(' cols={}'.format(grid.width)) if any( isinstance(e, BackgroundElt) and e.color != 0xffffff for r, c, e in grid.features): print(" fill='''") print(grid_to_string(grid)) print("'''") if any(isinstance(e, BorderElt) for r, c, e in grid.features): bordered = Grid(grid.height, grid.width) bordered.features.extend(grid.features) bordered.features.extend(outside_bars(grid)) print(" bars='''") print(bars_to_string(bordered)) print("'''") if any(isinstance(e, TextElt) for r, c, e in grid.features): label_str = labels_to_string(grid) gr = Grid(grid.height, grid.width) gr.features.extend(autonumber(gr)) if label_str == labels_to_string(grid): print(" cells_with_text='auto'") else: print(" cells_with_text='''") print(label_str) print("'''")
def test_all(self): detected=(len(self.img.breaks[0])-1,len(self.img.breaks[1])-1) expected=(self.rows,self.cols) self.assertEqual(expected,detected,'wrong dimensions') if hasattr(self,'fill'): with self.subTest('fill'): grid=self.img.grid() grid.features.extend(self.img.read_background()) f=grid_to_string(grid) self.assertEqual(self.fill.strip(),f.strip()) if hasattr(self,'bars'): with self.subTest('bars'): grid=self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b=bars_to_string(grid) self.assertEqual(self.bars.strip(),b.strip()) if hasattr(self,'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text=='auto': grid=self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text=labels_to_string(grid) grid=self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t=labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(),t.strip())
def test_all(self): detected = (len(self.img.breaks[0]) - 1, len(self.img.breaks[1]) - 1) expected = (self.rows, self.cols) self.assertEqual(expected, detected, 'wrong dimensions') if hasattr(self, 'fill'): with self.subTest('fill'): grid = self.img.grid() grid.features.extend(self.img.read_background()) f = grid_to_string(grid) self.assertEqual(self.fill.strip(), f.strip()) if hasattr(self, 'bars'): with self.subTest('bars'): grid = self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b = bars_to_string(grid) self.assertEqual(self.bars.strip(), b.strip()) if hasattr(self, 'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text == 'auto': grid = self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text = labels_to_string(grid) grid = self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t = labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(), t.strip())
def process(grid,args): if args.print_testdata: from cross2sheet.test.test import print_tests print_tests(grid) if args.autonumber: grid.features.extend(autonumber(grid)) if args.outer_border: grid.features.extend(outside_bars(grid)) pad(grid,*args.padding)
def process(grid, args): if args.print_testdata: from cross2sheet.test.test import print_tests url = args.input_file_or_url url = url.replace('http://web.mit.edu/puzzle/www/', '') print_tests(url, grid) if args.autonumber: grid.features.extend(autonumber(grid)) if args.outer_border: grid.features.extend(outside_bars(grid)) pad(grid, *args.padding)
def a_puzzle_with_answer_nowhere_man(): req=urlopen('http://web.mit.edu/puzzle/www/2014/puzzle/puzzle_with_answer_nowhere_man/grid.png') data=req.read() req.close() img=ImageGrid(data) grid=img.grid() grid.features.extend(img.read_background()) grid.features.extend(img.read_bars()) grid.features.extend(outside_bars(grid)) grid.features.extend(autonumber(grid)) save_xlsx(grid,'nowhere_man.xlsx')
def __init__(self,data): self.cells = [[Cell() for x in range(data.width)] for y in range(data.height)] for r,c,e in data.back: self.cells[r][c].back=e.color for r,c,e in data.bars: self.cells[r][c].borders.extend(b.lower() for b in e.dirs) for r,c,e in data.text: self.cells[r][c].texts.append(CellText(['text'],e.text)) g=Grid(data.height,data.width) for s1,e1 in [('bar',data.bars),('nobar',[])]: for s2,e2 in [('back',data.back),('noback',[])]: g.features=e1+e2 for r,c,e in autonumber(g): self.cells[r][c].texts.append(CellText(['auto',s1,s2],e.text))
def form_data_to_excel(form): td=TableData.from_json(form['data']) g=Grid(td.height,td.width) if 'back' in form: g.features.extend(td.back) if 'bar' in form: g.features.extend(td.bars) if form['auto']=='auto': g.features.extend(autonumber(g)) elif form['auto']=='text': g.features.extend(td.text) g.features.extend(outside_bars(g)) i=BytesIO() to_openpyxl(g,text_in_cells='cells' in form,text_in_comments='comments' in form).save(i) i.seek(0) return i
def __init__(self, data): self.cells = [[Cell() for x in range(data.width)] for y in range(data.height)] for r, c, e in data.back: self.cells[r][c].back = e.color for r, c, e in data.bars: self.cells[r][c].borders.extend(b.lower() for b in e.dirs) for r, c, e in data.text: self.cells[r][c].texts.append(CellText(['text'], e.text)) g = Grid(data.height, data.width) for s1, e1 in [('bar', data.bars), ('nobar', [])]: for s2, e2 in [('back', data.back), ('noback', [])]: g.features = e1 + e2 for r, c, e in autonumber(g): self.cells[r][c].texts.append( CellText(['auto', s1, s2], e.text))
def form_data_to_excel(form): td = TableData.from_json(form['data']) g = Grid(td.height, td.width) if 'back' in form: g.features.extend(td.back) if 'bar' in form: g.features.extend(td.bars) if form['auto'] == 'text': g.features.extend(td.text) g.validate() if form['auto'] == 'auto': g.features.extend(autonumber(g)) g.features.extend(outside_bars(g)) i = BytesIO() to_openpyxl(g, text_in_cells='cells' in form, text_in_comments='comments' in form).save(i) i.seek(0) return i