def verify(p, filename=None, exit=True): """Verifies the solution to a problem.""" filename = filename or p.filename if not os.path.isfile(filename): # Attempt to verify the first problem file matched by glob try: filename = next(p.iglob) except StopIteration: click.secho('No file found for problem %i.' % p.num, fg='red') sys.exit(1) solution = p.solution click.echo('Checking "{0}" against solution: '.format(filename), nl=False) cmd = [sys.executable or 'python', filename] start = clock() proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) stdout = proc.communicate()[0] end = clock() time_info = format_time(start, end) # Return value of anything other than 0 indicates an error if proc.poll() != 0: click.secho('Error calling "{0}".'.format(filename), fg='red') click.secho(time_info, fg='cyan') # Return None if option is not --verify-all, otherwise exit return sys.exit(1) if exit else None # Decode output if returned as bytes (Python 3) if isinstance(stdout, bytes): output = stdout.decode('ascii') # Split output lines into array; make empty output more readable output_lines = output.splitlines() if output else ['[no output]'] # If output is multi-lined, print the first line of the output on a # separate line from the "checking against solution" message, and # skip the solution check (multi-line solution won't be correct) if len(output_lines) > 1: is_correct = False click.echo() # force output to start on next line click.secho('\n'.join(output_lines), bold=True, fg='red') else: is_correct = output_lines[0] == solution fg_colour = 'green' if is_correct else 'red' click.secho(output_lines[0], bold=True, fg=fg_colour) click.secho(time_info, fg='cyan') # Remove any suffix from the filename if its solution is correct if is_correct and filename != p.filename: rename_file(filename, p.filename) # Exit here if answer was incorrect, otherwise return is_correct value return sys.exit(1) if exit and not is_correct else is_correct
def verify(p, filename=None, exit=True): """Verifies the solution to a problem.""" filename = filename or p.filename if not os.path.isfile(filename): # Attempt to verify the first problem file matched by glob try: filename = next(p.iglob) except StopIteration: click.secho('No file found for problem %i.' % p.num, fg='red') sys.exit(1) solution = p.solution click.echo('Checking "{0}" against solution: '.format(filename), nl=False) cmd = (sys.executable or 'python', filename) start = clock() proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) stdout = proc.communicate()[0] end = clock() time_info = format_time(start, end) # Return value of anything other than 0 indicates an error if proc.poll() != 0: click.secho('Error calling "{0}".'.format(filename), fg='red') click.secho(time_info, fg='cyan') # Return None if option is not --verify-all, otherwise exit return sys.exit(1) if exit else None # Decode output if returned as bytes (Python 3) if isinstance(stdout, bytes): output = stdout.decode('ascii') # Split output lines into array; make empty output more readable output_lines = output.splitlines() if output else ['[no output]'] # If output is multi-lined, print the first line of the output on a # separate line from the "checking against solution" message, and # skip the solution check (multi-line solution won't be correct) if len(output_lines) > 1: is_correct = False click.echo() # force output to start on next line click.secho('\n'.join(output_lines), bold=True, fg='red') else: is_correct = output_lines[0] == solution fg_colour = 'green' if is_correct else 'red' click.secho(output_lines[0], bold=True, fg=fg_colour) click.secho(time_info, fg='cyan') # Remove any suffix from the filename if its solution is correct if is_correct and filename != p.filename: rename_file(filename, p.filename) # Exit here if answer was incorrect, otherwise return is_correct value return sys.exit(1) if exit and not is_correct else is_correct
def skip(p): """Generates Python file for the next problem.""" click.echo("Current problem is problem %i." % p.num) next_p = Problem(p.num + 1) generate(next_p, prompt_default=False) rename_file(p.filename, p.suf_name('skipped'))
def verify_all(current_p): """ Verifies all problem files in the current directory and prints an overview of the status of each problem. """ # Define various problem statuses statuses = ( ('correct', 'C', 'green'), ('incorrect', 'I', 'red'), ('error', 'E', 'yellow'), ('skipped', 'S', 'cyan'), ('missing', '.', 'white'), ) status = OrderedDict( (key, click.style(symbol, fg=colour, bold=True)) for key, symbol, colour in statuses ) overview = {} # Search through problem files using glob module for filename in glob.glob('[0-9][0-9][0-9]*.py'): p = Problem(int(filename[:3])) # Catch KeyboardInterrupt during verification to allow the user # to skip the verification of a problem if it takes too long try: is_correct = verify(p, filename=filename, exit=False) except KeyboardInterrupt: overview[p.num] = status['skipped'] else: if is_correct is None: # error was returned by problem file overview[p.num] = status['error'] elif is_correct: overview[p.num] = status['correct'] elif not is_correct: overview[p.num] = status['incorrect'] # Attempt to add "skipped" suffix to the filename if the # problem file is not the current problem. This is useful # when the --verify-all is used in a directory containing # files generated pre-v1.1 (before files with suffixes) if p.num != current_p.num: rename_file(filename, p.suf_name('skipped')) # Separate each verification with a newline click.echo() # No Project Euler files in the current directory if not overview: click.echo("No Project Euler files found in the current directory.") sys.exit(1) # Print overview of the status of each problem legend = ', '.join('{0} = {1}'.format(v, k) for k, v in status.items()) click.echo('-' * 63) click.echo(legend + '\n') # Rows needed for overview is based on the current problem number num_of_rows = (current_p.num + 19) // 20 for row in range(1, num_of_rows + 1): low, high = (row * 20) - 19, (row * 20) click.echo("Problems {0:03d}-{1:03d}: ".format(low, high), nl=False) for problem in range(low, high + 1): # Add missing status to problems with no corresponding file status = overview[problem] if problem in overview else '.' # Separate problem indicators into groups of 5 spacer = ' ' if (problem % 5 == 0) else ' ' # Start a new line at the end of each row click.secho(status + spacer, nl=(problem % 20 == 0)) click.echo()
def verify(p, filename=None, exit=True): """Verifies the solution to a problem.""" filename = filename or p.filename if not os.path.isfile(filename): # Attempt a fuzzy search for problem files using the glob module for fuzzy_file in glob.glob("{0:03d}*.py".format(p.num)): if os.path.isfile(fuzzy_file): filename = fuzzy_file break else: click.secho("No file found for problem %i." % p.num, fg="red") sys.exit(1) solution = p.solution click.echo('Checking "{0}" against solution: '.format(filename), nl=False) cmd = [sys.executable or "python", filename] start = clock() proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) stdout = proc.communicate()[0] end = clock() time_info = format_time(start, end) # Return value of anything other than 0 indicates an error if proc.poll() != 0: click.secho('Error calling "{0}".'.format(filename), fg="red") click.secho(time_info, fg="cyan") # Exit here if appropriate, otherwise return None (for --verify-all) if exit: sys.exit(1) else: return None # Python 3 returns bytes; use a valid encoding like ASCII if isinstance(stdout, bytes): output = stdout.decode("ascii") # Split output lines into array; make empty output more readable output_lines = output.splitlines() if output else ["[no output]"] # If output is multi-lined, print the first line of the output on a # separate line from the "checking against solution" message, and # skip the solution check (multi-line solution won't be correct) if len(output_lines) > 1: is_correct = False click.echo() # force output to start on next line for line in output_lines: click.secho(line, bold=True, fg="red") else: is_correct = output_lines[0] == solution fg_colour = "green" if is_correct else "red" click.secho(output_lines[0], bold=True, fg=fg_colour) click.secho(time_info, fg="cyan") # Exit here if answer was incorrect if exit and not is_correct: sys.exit(1) else: # Remove any suffix from the filename if its solution is correct if is_correct and filename != p.filename: rename_file(filename, p.filename) return is_correct
def verify_all(current_p): """ Verifies all problem files in the current directory and prints an overview of the status of each problem. """ overview = {} # Search through problem files using glob module for filename in glob.glob("[0-9][0-9][0-9]*.py"): p = Problem(int(filename[:3])) # Catch KeyboardInterrupt during verification to allow the user # to skip the verification of a problem if it takes too long try: is_correct = verify(p, filename=filename, exit=False) except KeyboardInterrupt: overview[p.num] = click.style("S", fg="cyan") else: if is_correct is None: # error was returned by problem file overview[p.num] = click.style("E", fg="yellow") elif is_correct: overview[p.num] = click.style("C", fg="green") elif not is_correct: overview[p.num] = click.style("I", fg="red") # Attempt to add "skipped" suffix to the filename if the # problem file is not the current problem. This is useful # when the --verify-all is used in a directory containing # files generated pre-v1.1 (before files with suffixes) if p.num != current_p.num: skipped_name = p.suf_name("skipped") rename_file(filename, skipped_name) # Separate each verification with a newline click.echo() # No Project Euler files in the current directory if not overview: click.echo("No Project Euler files found in the current directory.") sys.exit(1) # Print overview of the status of each problem click.echo("-" * 63) legend = ", ".join( "{0} = {1}".format(click.style(symbol, bold=True, fg=colour), name) for symbol, name, colour in ( ("C", "correct", "green"), ("I", "incorrect", "red"), ("E", "error", "yellow"), ("S", "skipped", "cyan"), (".", "missing", "white"), ) ) click.echo(legend + "\n") # Rows needed for overview is based on the current problem number num_of_rows = (current_p.num + 19) // 20 for row in range(1, num_of_rows + 1): low, high = (row * 20) - 19, (row * 20) click.echo("Problems {0:03d}-{1:03d}: ".format(low, high), nl=False) for problem in range(low, high + 1): # Add missing status to problems with no problem file status = overview[problem] if problem in overview else "." click.secho(status, bold=True, nl=False) # Separate problem indicators into groups of 5 click.echo(" " if problem % 5 == 0 else " ", nl=False) # Start a new line at the end of each row click.echo() click.echo()
def verify_all(current_p): """ Verifies all problem files in the current directory and prints an overview of the status of each problem. """ # Define various problem statuses keys = ('correct', 'incorrect', 'error', 'skipped', 'missing') symbols = ('C', 'I', 'E', 'S', '.') colours = ('green', 'red', 'yellow', 'cyan', 'white') status = OrderedDict( (key, click.style(symbol, fg=colour, bold=True)) for key, symbol, colour in zip(keys, symbols, colours)) overview = {} # Search through problem files using glob module files = problem_glob() # No Project Euler files in the current directory if not files: click.echo("No Project Euler files found in the current directory.") sys.exit(1) for file in files: p = Problem(int(file[:3])) # Catch KeyboardInterrupt during verification to allow the user # to skip the verification of a problem if it takes too long try: is_correct = verify(p, filename=file, exit=False) except KeyboardInterrupt: overview[p.num] = status['skipped'] else: if is_correct is None: # error was returned by problem file overview[p.num] = status['error'] elif is_correct: overview[p.num] = status['correct'] elif not is_correct: overview[p.num] = status['incorrect'] # Attempt to add "skipped" suffix to the filename if the # problem file is not the current problem. This is useful # when the --verify-all is used in a directory containing # files generated pre-v1.1 (before files with suffixes) if p.num != current_p.num: rename_file(file, p.suf_name('skipped')) # Separate each verification with a newline click.echo() # Print overview of the status of each problem legend = ', '.join('{0} = {1}'.format(v, k) for k, v in status.items()) click.echo('-' * 63) click.echo(legend + '\n') # Rows needed for overview is based on the current problem number num_of_rows = (current_p.num + 19) // 20 for row in range(1, num_of_rows + 1): low, high = (row * 20) - 19, (row * 20) click.echo("Problems {0:03d}-{1:03d}: ".format(low, high), nl=False) for problem in range(low, high + 1): # Add missing status to problems with no corresponding file status = overview[problem] if problem in overview else '.' # Separate problem indicators into groups of 5 spacer = ' ' if (problem % 5 == 0) else ' ' # Start a new line at the end of each row click.secho(status + spacer, nl=(problem % 20 == 0)) click.echo()