示例#1
0
def verify(num, filename=None, exit=True):
    """Verifies the solution to a problem."""
    p = Problem(num)

    filename = filename or p.filename()

    if not os.path.isfile(filename):
        # Attempt to verify the first problem file matched by glob
        if p.glob:
            filename = str(p.file)
        else:
            click.secho('No file found for problem %i.' % p.num, fg='red')
            sys.exit(1)

    solution = p.solution
    click.echo('Checking "{}" 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 "{}".'.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:
        p.file.change_suffix('')

    # Exit here if answer was incorrect, otherwise return is_correct value
    return sys.exit(1) if exit and not is_correct else is_correct
示例#2
0
文件: euler.py 项目: vermuz/EulerPy
def verify(num, filename=None, exit=True):
    """Verifies the solution to a problem."""
    p = Problem(num)

    filename = filename or p.filename()

    if not os.path.isfile(filename):
        # Attempt to verify the first problem file matched by glob
        if p.glob:
            filename = str(p.file)
        else:
            click.secho('No file found for problem %i.' % p.num, fg='red')
            sys.exit(1)

    solution = p.solution
    click.echo('Checking "{}" 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 "{}".'.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:
        p.file.change_suffix('')

    # Exit here if answer was incorrect, otherwise return is_correct value
    return sys.exit(1) if exit and not is_correct else is_correct
示例#3
0
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
示例#4
0
文件: euler.py 项目: jperla/EulerPy
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 file matched by glob
        try:
            filename = next(glob.iglob('{0:03d}*.py'.format(p.num)))
        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')

        # 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
        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')

    # 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