Write a context manager class Timer(clock, sink, name):
clock() (a function returning seconds).sink(name, elapsed_ms, ok) where elapsed_ms is rounded to
one decimal and ok is False if the block raised.with Timer(...) as t: binds the Timer itself; after the block, t.elapsed_ms holds the result.The harness supplies a fake clock so results are deterministic.
Implement Timer. Input: clock: () -> float, sink: (name, ms, ok) -> None, name: str. Returns: a context manager.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
def __harness__(Timer, ticks, fail):
times = iter(ticks)
records = []
clock = lambda: next(times)
try:
with Timer(clock, lambda n, ms, ok: records.append([n, ms, ok]), "job") as t:
if fail:
raise RuntimeError("boom")
propagated = False
except RuntimeError:
propagated = True
return {"records": records, "elapsed": getattr(t, "elapsed_ms", None), "propagated": propagated}
class Timer:
def __init__(self, clock, sink, name):
self.clock, self.sink, self.name = clock, sink, name
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return True
Run executes the public tests. Submit grades against public and hidden tests.
Write a context manager class Timer(clock, sink, name):
clock() (a function returning seconds).sink(name, elapsed_ms, ok) where elapsed_ms is rounded to
one decimal and ok is False if the block raised.with Timer(...) as t: binds the Timer itself; after the block, t.elapsed_ms holds the result.The harness supplies a fake clock so results are deterministic.
Implement Timer. Input: clock: () -> float, sink: (name, ms, ok) -> None, name: str. Returns: a context manager.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
def __harness__(Timer, ticks, fail):
times = iter(ticks)
records = []
clock = lambda: next(times)
try:
with Timer(clock, lambda n, ms, ok: records.append([n, ms, ok]), "job") as t:
if fail:
raise RuntimeError("boom")
propagated = False
except RuntimeError:
propagated = True
return {"records": records, "elapsed": getattr(t, "elapsed_ms", None), "propagated": propagated}
class Timer:
def __init__(self, clock, sink, name):
self.clock, self.sink, self.name = clock, sink, name
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return True
Run executes the public tests. Submit grades against public and hidden tests.