예제 #1
0
def test_initialisation() -> None:
    """Check that Iterwriter object is created well."""
    # object created safely
    writer = iterwrite.Iterwriter(count="count: {:4d}",
                                  delta="value += {:.2f}")
    assert isinstance(writer, iterwrite.Iterwriter)

    # internal properties correct
    assert isinstance(writer.sep, str)
    assert writer.names == ["count", "delta"]
    assert writer.messages == {"count": "count: {}", "delta": "value += {}"}
    assert writer.bases == {"count": "d", "delta": "f"}
    assert writer.pad_lengths == {"count": 4, "delta": 0}
    assert writer.decimals == {"count": None, "delta": 2}

    # empty iterwriter initialised safely
    writer = iterwrite.Iterwriter()
    assert isinstance(writer, iterwrite.Iterwriter)

    # sep value can be altered
    writer = iterwrite.Iterwriter(update="({:3d} / 100): ",
                                  val="{:6.3f}",
                                  sep="")
    assert writer.sep == ""

    # unnamed arguments fail :(
    with pytest.raises(NotImplementedError,
                       match="Unnamed arguments not yet handled"):
        iterwrite.Iterwriter("({update:3d} / 100): ", "{val:6.3f}", sep="")
예제 #2
0
def test_add() -> None:
    """Check that new values can be added to a writer easily."""
    writer = iterwrite.Iterwriter(count="count: {:4d}",
                                  delta="value += {:.2f}")
    writer.add_message("best", "(best: {6.2f})")
    assert writer.names == ["count", "delta", "best"]
    for attributes in (writer.messages, writer.bases, writer.pad_lengths,
                       writer.decimals):
        assert "best" in attributes
예제 #3
0
def test_separator() -> None:
    writer = iterwrite.Iterwriter(count="count: {:4d}",
                                  delta="value += {:.2f}")
    message_def = writer.format(count=100, delta=0.05)
    writer.sep = ",  "
    message_new = writer.format(count=100, delta=0.05)

    assert message_def == "count:  100 ‖ value += 0.05"
    assert message_new == "count:  100,  value += 0.05"
예제 #4
0
def test_writing() -> None:
    # the order of the tests matters, because writer remembers the length of jumbo
    writer = iterwrite.Iterwriter(sep="--",
                                  mumbo="long:{:10d}",
                                  jumbo="short:{:0d}")
    assert writer.format(mumbo=0, jumbo=0) == "long:         0--short:0"
    assert writer.format(mumbo=43210, jumbo=0) == "long:     43210--short:0"
    assert writer.format(mumbo=43210,
                         jumbo=6543210) == "long:     43210--short:6543210"
    assert writer.format(mumbo=43210,
                         jumbo=0) == "long:     43210--short:      0"

    # test that forgetting arguments mucks it all up
    with pytest.raises(ValueError, match="Missing messages"):
        writer.format(mumbo=500)
예제 #5
0
parser.add_argument("--iterwrite",
                    help="If set, uses the iterwrite package to print results",
                    action="store_true")
ARGUMENTS = vars(parser.parse_args())

# repeatable
random.seed(672229)  # Twitter@_primes_

# newlines and spaces act as margin to make cropping easier
print("\n  Running some code with {} printing within a loop.\n".format(
    "clean" if ARGUMENTS["iterwrite"] else "messy"))

# set up loop (very artificial)
running_value = 1.0
if ARGUMENTS["iterwrite"]:
    writer = iterwrite.Iterwriter(iter="    Iteration: {:d}",
                                  value="value: {:f}",
                                  delta="delta: {:f}")
else:
    writer = "    Iteration: {iter}, value: {value}, delta: {delta}"

# make some messy output
for iter in range(ARGUMENTS["iter_count"]):
    delta = round(random.normalvariate(0, iter), random.randint(1, 12))
    running_value = round(running_value + delta, random.randint(1, 12))
    if (iter % 10) == 0:
        print(writer.format(iter=iter + 1, value=running_value, delta=delta))

# margin
print("\n")