From c6b97b2d7f73453fc04785c8066c16cc61da8472 Mon Sep 17 00:00:00 2001 From: Julia Torgan Date: Mon, 10 Dec 2012 08:04:06 +0300 Subject: [PATCH] READY for code review now. --- torgan/AbstractGrid.py | 10 +++++++ torgan/Main.py | 68 ++++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/torgan/AbstractGrid.py b/torgan/AbstractGrid.py index c034c30..822692c 100644 --- a/torgan/AbstractGrid.py +++ b/torgan/AbstractGrid.py @@ -23,6 +23,7 @@ def __extract_words_coordinates(self, list): return words_coordinates def parse_matrix(self, matrix): + self.__check_format(matrix) for i in xrange(len(matrix)): words_coordinates = self.__extract_words_coordinates(matrix[i]) for (x1, x2) in words_coordinates: @@ -42,3 +43,12 @@ def __build_tree(self, parent, is_vertical): parent.children.append(self.__build_tree(cross, not is_vertical)) return parent + def __check_format(self, list): + if len(list) == 0: + raise TypeError + for line in list: + isRight = True + for i in line: + isRight &= i == '*' or i == '_' + if not isRight: + raise TypeError \ No newline at end of file diff --git a/torgan/Main.py b/torgan/Main.py index d1973b4..bf26ad4 100644 --- a/torgan/Main.py +++ b/torgan/Main.py @@ -1,36 +1,46 @@ from Crossword import Crossword +import sys -file = open("grid.txt") input_grid = [] -for line in file.readlines(): - input_grid.append(line.split()) -file.close() - -file = open("dict.txt") input_dict = [] -for line in file.readlines(): - input_dict.extend(line.split()) -file.close() - -# TODO: add exception handling for: working with files, validate input - grid's format - -crossword = Crossword() -crossword.parse_matrix(input_grid) -is_right = crossword.fill_with_words(input_dict) - -if is_right: - for word in crossword.horizontal_words: - if word.value is not None: - input_grid[word.item][word.start : word.end + 1] = word.value - for word in crossword.vertical_words: - if word.value is not None: - for line in input_grid: - if word.start <= input_grid.index(line) <= word.end: - line[word.item] = word.value[input_grid.index(line) - word.start] - for line in input_grid: - print line -else: - print "Error! Can not fill crossword with that dictionary!" + +try: + + file = open("grid.txt") + for line in file.readlines(): + input_grid.append(line.split()) + file.close() + + file = open("dict.txt") + for line in file.readlines(): + input_dict.extend(line.split()) + file.close() + + crossword = Crossword() + crossword.parse_matrix(input_grid) + is_right = crossword.fill_with_words(input_dict) + + if is_right: + for word in crossword.horizontal_words: + if word.value is not None: + input_grid[word.item][word.start : word.end + 1] = word.value + for word in crossword.vertical_words: + if word.value is not None: + for line in input_grid: + if word.start <= input_grid.index(line) <= word.end: + line[word.item] = word.value[input_grid.index(line) - word.start] + for line in input_grid: + print line + else: + print "Can not fill crossword with that dictionary!" + +except IOError as e: + print "Error: ", e.strerror, e.filename +except TypeError: + print "Error: wrong format of the input crossword grid - empty or contains wrong chars!" +except: + print "Unexpected error:", sys.exc_info()[0] + raise