Example #1
0
def test_kwargs():
    assert PipePy(key="value")._args == ["--key=value"]
    assert PipePy(key=2)._args == ["--key=2"]
    assert PipePy(key_key="value")._args == ["--key-key=value"]
    assert PipePy(key=True)._args == ["--key"]
    assert PipePy(key=False)._args == ["--no-key"]
    assert PipePy(k="value")._args == ["-k", "value"]
Example #2
0
def test_converts_to_str():
    assert PipePy(3)._args == ["3"]
Example #3
0
def test_no_modify():
    assert PipePy('foo')._args == ["foo"]
Example #4
0
def test_kwargs_first():
    assert PipePy(key="value")('a')._args == ["--key=value", "a"]
Example #5
0
def test_glob():
    assert (sorted(PipePy('src/tests/playground/globtest*')._args) ==
            sorted(['src/tests/playground/globtest1',
                    'src/tests/playground/globtest2']))
Example #6
0
import time

import pytest

from pipepy import PipePy, TimeoutExpired, sleep

echo_messages = PipePy('python', 'src/tests/playground/echo_messages.py')


def test_wait():
    tic = time.time()
    command = echo_messages(count=3, delay=.1, message='message {}').delay()
    command.wait()
    toc = time.time()
    assert .3 < toc - tic
    assert ([line.strip() for line in str(command).splitlines()
             ] == ["message 0", "message 1", "message 2"])


def test_terminate():
    command = echo_messages(count=10, delay=.1, message='message {}').delay()
    time.sleep(.23)  # Leave enough time for 2 messages
    command.terminate()
    command.wait()
    assert len([line.strip() for line in str(command).splitlines()]) < 10


def test_kill():
    command = echo_messages(count=10, delay=.1, message='message {}').delay()
    time.sleep(.23)  # Leave enough time for 2 messages
    command.kill()
Example #7
0
import random

from rich.console import Console
from rich.syntax import Syntax

from pipepy import PipePy


def rprint(text):
    syntax = Syntax(text, "python", theme="monokai")
    console = Console()
    console.print(syntax)


demo = PipePy('python', 'demo.py')
ls = PipePy('ls')
grep = PipePy('grep')
gzip = PipePy('gzip')
interactive = PipePy('python', 'interactive.py')
interactive2 = PipePy('python', 'interactive2.py')

print("# Simple command\n")
rprint("    >>> demo()")
r = demo()
rprint(f"    <<< {r!r}")
print()

print("# Iterate over results of command\n")
rprint("    >>> [name.upper() for name in ls]")
r = [name.upper() for name in ls]
rprint(f"    <<< {r!r}")
Example #8
0
import itertools
import random
from copy import copy

from pipepy import PipePy, cat, grep

student_command = PipePy('python', 'src/tests/playground/math_quiz_student.py')
teacher_command = PipePy('python', 'src/tests/playground/math_quiz_teacher.py')


def python_student(stdin, stdout, up):
    """ Interacts with stdin and stdout as a student to a math quiz

        `up` should either be `range(X)` or `itertools.count()`
    """

    result = []
    stdout = (line.strip() for line in iter(stdout) if line.strip())
    try:
        for _ in up:
            question = next(stdout)
            a, _, b, _ = question.split()
            answer = f"{int(a) + int(b)}\n"
            stdin.write(answer)
            stdin.flush()
            verdict = next(stdout)
            result.append((question, answer, verdict))
    except StopIteration:
        pass
    return result