def test(problem_name): """ Run the test input for a problem and compare it to the expected output. """ t = Terminal() program = '{0}/program.py'.format(problem_name) test_input_filename = '{0}/input.txt'.format(problem_name) with open(test_input_filename, 'rU') as f: test_input = f.read() with open('{0}/output.txt'.format(problem_name), 'rU') as f: expected_output = f.read() result = subprocess.check_output(program, stdin=open(test_input_filename, 'rU')) if result.strip() != expected_output.strip(): print t.bright_red('Test run failed!') print t.cyan('<== Input ==>') print test_input print t.cyan('<== Expected output ==>') print expected_output print t.cyan('<== Actual output ==>') print result else: print t.bright_green('Test run passed!') print t.cyan('<== Input ==>') print test_input print t.cyan('<== Output ==>') print expected_output
def validate_contribute_json(): t = Terminal() schema = get_schema() passed, failed, skipped = [], [], [] for project in Bar('Validating').iter(list(get_projects())): if not project.repos: reason = 'No repos found' skipped.append((project, reason)) continue # TODO: Handle non-github projects for repo in project.repos: if repo.startswith('https://github.com'): parsed = urlparse(repo) user, repo_name = parsed.path.strip('/').split('/') url = CONTRIBUTE_JSON_URL.format(user=user, repo=repo_name) response = requests.get(url) if response.status_code != requests.codes.ok: reason = 'No contribute.json: {0}'.format( response.status_code) failed.append((project, reason)) continue contribute_json = json.loads(response.text) try: jsonschema.validate(contribute_json, schema) passed.append(project) except jsonschema.ValidationError as e: reason = 'Invalid contribute.json: {0}'.format(e.message) failed.append((project, reason)) print t.bright_green('== Passed ==') for project in passed: print ' ' + t.underline(project.name) print t.bright_red('== Failed ==') for project, reason in failed: print ' {0}\n {1}'.format(t.underline(project.name), reason) print t.bright_yellow('== Skipped ==') for project, reason in skipped: print ' {0}\n {1}'.format(t.underline(project.name), reason)
def create(problem_name): t = Terminal() problem_name = problem_name.lower() if os.path.isdir(problem_name): print t.bright_red( 'Directory for problem `{0}` already exists!'.format(problem_name)) return url = 'http://www.spoj.com/problems/{0}/'.format(problem_name.upper()) response = requests.get(url) response.raise_for_status() # TODO: Handle bad input. soup = BeautifulSoup(response.text) title = soup.find_all('h1')[1].text.strip() test_input, test_output = _find_sample_io(soup) # Create problem directory. os.makedirs(problem_name) # Write program template and chmod +x program = '{0}/program.py'.format(problem_name) with open(program, 'w') as f: f.write(problem_template.format(title=title, url=url)) st = os.stat(program) os.chmod(program, st.st_mode | stat.S_IEXEC) with open('{0}/input.txt'.format(problem_name), 'w') as f: f.write(test_input) with open('{0}/output.txt'.format(problem_name), 'w') as f: f.write(test_output) print t.bright_green( 'Directory created for problem `{0}`.'.format(problem_name))
def show_board(board, status=''): term = Terminal() for i, (word, category) in enumerate(board): jword = word[:11] if category == UNKNOWN: print(justify(jword), end='') elif category == RED: print(term.bright_red(justify(jword + ' [r]')), end='') elif category == BLUE: print(term.bright_blue(justify(jword + ' [b]')), end='') elif category == NEUTRAL: print(term.yellow(justify(jword + ' [n]')), end='') elif category == ASSASSIN: print(term.reverse(justify(jword + ' [a]')), end='') if i % 5 == 4: print('\n') print(status)
def main(): term = Terminal() board = make_board() board_indices = {word: i for (i, (word, category)) in enumerate(board)} known_board = [(word, UNKNOWN) for (word, category) in board] current_player = RED status = '' board_vocab = [tag_en(word) for (word, category) in board] simframe = VECTORS.frame.dot(VECTORS.frame.loc[board_vocab].T) with open('/tmp/codenames.log', 'w') as log: print(board, file=log) while True: real_counts = Counter(category for (word, category) in board) known_counts = Counter(category for (word, category) in known_board) diff = real_counts - known_counts if diff[RED] == 0: show_board(board, "Red team wins.") return RED if diff[BLUE] == 0: show_board(board, "Blue team wins.") return BLUE show_board(known_board, '') print("%s spymaster is thinking of a clue..." % PLAYER_NAMES[current_player]) clue_number, clue_word = get_ai_clue(simframe, board, known_board, diff, current_player, log) print("Clue: %s %d" % (clue_word, clue_number)) picked_category = current_player while picked_category == current_player: choice = get_human_guess(board, current_player) if choice == 'PASS': status = '%s passes.' % PLAYER_NAMES[current_player] print(status) break idx = board_indices[choice] word, picked_category = board[idx] known_board[idx] = board[idx] if picked_category == RED: shown_category = term.bright_red('red') elif picked_category == BLUE: shown_category = term.bright_blue('blue') elif picked_category == NEUTRAL: shown_category = term.yellow('neutral') elif picked_category == ASSASSIN: shown_category == term.reverse('the assassin') else: raise ValueError(picked_category) status = '%s is %s.' % (choice, shown_category) print(status) if picked_category == ASSASSIN: if current_player == RED: show_board(board, "Blue team wins.") return BLUE else: show_board(board, "Red team wins.") return RED current_player = OPPOSITE_PLAYER[current_player]
class testGroup(object): """TestGroup, group a number of testUnit, exec them and print the results""" def __init__(self, name="", terminal=None, prefix="", verbose=False, align=0): self._tests = [] self.name = name self.t = terminal self.prefix = prefix (self.t) if self.t == None: self.t = Terminal() self.results = [] self.status_modules = { "success": 0, "total": 0, "warning": 0, "critical": 0, "failure": 0 } self.success_text = self.t.green("success") self.failure_text = self.t.bright_red("failure") self.warning_text = self.t.bright_yellow("warning") self.critical_text = self.t.white_on_red("critical") self.verbose = verbose self.align = align def addTest(self, testUnit): self._tests.append(testUnit) return self def test(self): "Execute all tests, some options might exist at some point" module_success, module_total = 0, 0 print(self.prefix + "+ Executing test group " + self.pretty_group(self.name)) oldprefix = self.prefix self.prefix += "| " self.results = [] for test in self._tests: try: list_status, total, log_results = self.print_result( test.test()) except Exception as e: print( self.t.bright_red( "[ERROR] An unhandled error occured during the execution of the tests" )) raise (e) # for l in e: # print(l) list_status, total, log_results = self.print_result([]) print(self.pretty_subtests(test.name, list_status, total)) if list_status["success"] + list_status["warning"] == total: self.status_modules["success"] += 1 self.status_modules["total"] += 1 for log in log_results: print(log) self.results.append([self, SUCCESS_STATUS, ""]) self.prefix = oldprefix print( self.pretty_group_result(self.status_modules, self.status_modules["total"])) return self.results def get_status(self): "Get the status of every module, if no test was run, should return an empty dict" return self.status_modules def print_result(self, table): "Get the array of success/failures and print according to the options (still none yet)" total = len(table) success = 0 results_array = [] nb = 0 list_status = {"success": 0, "failure": 0, "warning": 0, "critical": 0} for item, status, infos in table: nb += 1 if status == SUCCESS_STATUS: list_status["success"] += 1 elif status == WARNING_STATUS: list_status["warning"] += 1 elif status == CRITICAL_STATUS: list_status["critical"] += 1 else: list_status["failure"] += 1 if self.verbose or status != SUCCESS_STATUS: results_array.append( self.pretty_result(status, nb, item, infos)) return list_status, total, results_array def pretty_group_result(self, module_status, total): "Prettyfying the result of the batch of tests" bloc = self.prefix + "+ Done " return bloc + self.pretty_group(self.name) + self.pretty_dots( bloc, len(self.name)) + self.pretty_successrate( module_status, total) def pretty_name(self, item): "Just a pretty way of showing the name of a test" try: return item.__name__.strip("<>") except: return str(item) def pretty_subtests(self, name, success, total): "Pretty way of showing the result of the group of tests" bloc = self.prefix + "testing " + self.pretty_test(name) return bloc + self.pretty_dots(bloc) + self.pretty_successrate( success, total) def pretty_result(self, status, nb, item, infos): "Just a pretty way of showing the result of one test" bloc = self.prefix + " * " + " [" + str(nb) + "] " + self.pretty_name( item) if status == CRITICAL_STATUS: pbloc = self.prefix + " * " + " [" + str( nb) + "] " + self.t.bold_bright_red(self.pretty_name(item)) else: pbloc = bloc return pbloc + self.pretty_dots(bloc) + self.pretty_status( status) + self.pretty_info(infos) def pretty_dots(self, bloc, padding=0): lenbloc = len(bloc) + padding if (self.align > lenbloc + 2): dots = "." * (self.align - lenbloc) else: dots = ".." return dots def pretty_info(self, infos): "Prettyfy the additional infos" if infos == "": return "" return self.t.italic(" (" + str(infos) + ")") def pretty_status(self, status): "Prettyfy the status of the test" if status == SUCCESS_STATUS: return self.success_text elif status == FAILURE_STATUS: return self.failure_text elif status == WARNING_STATUS: return self.warning_text else: return self.critical_text def pretty_group(self, name): "Prettify the name of the testGroup" return self.t.bold(name) def pretty_test(self, test): "Prettify the name of the testUnit" return test def pretty_successrate(self, success, total): warnings = "" if success["success"] == total: wrap = self.t.green txt = self.success_text elif success["success"] + success["warning"] == total: wrap = self.t.yellow txt = self.warning_text warnings = " ({} warnings)".format(str(success["warning"])) elif success["critical"] != 0: wrap = self.t.white_on_red txt = self.critical_text else: wrap = self.t.bright_red txt = self.failure_text return wrap(txt + " [" + str(success["success"] + success["warning"]) + "/" + str(total) + "]" + warnings) def __str__(self): return self.name