|
618 | 618 | { |
619 | 619 | "data": { |
620 | 620 | "text/plain": [ |
621 | | - "['d', 'h', 'g', 'a', 'c', 'f', 'e', 'b']" |
| 621 | + "['g', 'a', 'b', 'c', 'e', 'f', 'd', 'h']" |
622 | 622 | ] |
623 | 623 | }, |
624 | 624 | "execution_count": null, |
|
1326 | 1326 | "outputs": [], |
1327 | 1327 | "source": [ |
1328 | 1328 | "#export\n", |
| 1329 | + "def autostart(g):\n", |
| 1330 | + " \"Decorator that automatically starts a generator\"\n", |
| 1331 | + " @functools.wraps(g)\n", |
| 1332 | + " def f():\n", |
| 1333 | + " r = g()\n", |
| 1334 | + " next(r)\n", |
| 1335 | + " return r\n", |
| 1336 | + " return f" |
| 1337 | + ] |
| 1338 | + }, |
| 1339 | + { |
| 1340 | + "cell_type": "code", |
| 1341 | + "execution_count": null, |
| 1342 | + "metadata": {}, |
| 1343 | + "outputs": [], |
| 1344 | + "source": [ |
| 1345 | + "#export\n", |
| 1346 | + "@autostart\n", |
1329 | 1347 | "def time_events():\n", |
1330 | | - " \"A simple event timer implemented as a coroutine\"\n", |
| 1348 | + " \"An event timer implemented as a coroutine\"\n", |
1331 | 1349 | " start,events = default_timer(),0\n", |
1332 | 1350 | " while True: events += (yield events,events/(default_timer()-start)) or 0" |
1333 | 1351 | ] |
|
1336 | 1354 | "cell_type": "markdown", |
1337 | 1355 | "metadata": {}, |
1338 | 1356 | "source": [ |
1339 | | - "This is convenient for tracking the frequency of events. Call `send(None)` to start the timer, and then call `send(n)` any time you want to add `n` events to the counter. Pass the object to `next()` to get a tuple of the number of events and the frequency/second." |
| 1357 | + "This is convenient for tracking the frequency of events. Call `send(n)` any time you want to add `n` events to the counter. Pass the object to `next()` to get a tuple of the number of events and the frequency/second." |
1340 | 1358 | ] |
1341 | 1359 | }, |
1342 | 1360 | { |
|
1348 | 1366 | "name": "stdout", |
1349 | 1367 | "output_type": "stream", |
1350 | 1368 | "text": [ |
1351 | | - "# Events: 10, Freq/sec: 64.50\n" |
| 1369 | + "# Events: 10, Freq/sec: 80.30\n" |
1352 | 1370 | ] |
1353 | 1371 | } |
1354 | 1372 | ], |
1355 | 1373 | "source": [ |
1356 | 1374 | "# Random wait function for testing `time_events`\n", |
1357 | 1375 | "def _randwait(): yield from (sleep(random.random()/30) for _ in range(10))\n", |
1358 | 1376 | "\n", |
1359 | | - "c = time_events()\n", |
1360 | | - "c.send(None) # Start timer by sending `None`\n", |
| 1377 | + "c = time_events() # Start timer\n", |
1361 | 1378 | "for o in _randwait(): c.send(1) # Send an event\n", |
1362 | | - "events,freq = next(c) # `next` returns counter values\n", |
| 1379 | + "events,freq = next(c) # Return counter values\n", |
| 1380 | + "c.close() # Close when done\n", |
1363 | 1381 | "print(f'# Events: {events}, Freq/sec: {freq:.02f}')" |
1364 | 1382 | ] |
1365 | 1383 | }, |
|
1403 | 1421 | "source": [ |
1404 | 1422 | "#export\n", |
1405 | 1423 | "class PartialFormatter(string.Formatter):\n", |
| 1424 | + " \"A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args\"\n", |
1406 | 1425 | " def __init__(self):\n", |
1407 | 1426 | " self.missing = set()\n", |
1408 | 1427 | " super().__init__()\n", |
|
1417 | 1436 | " self.xtra = filter_keys(kwargs, lambda o: o not in used)" |
1418 | 1437 | ] |
1419 | 1438 | }, |
| 1439 | + { |
| 1440 | + "cell_type": "code", |
| 1441 | + "execution_count": null, |
| 1442 | + "metadata": {}, |
| 1443 | + "outputs": [ |
| 1444 | + { |
| 1445 | + "data": { |
| 1446 | + "text/markdown": [ |
| 1447 | + "<h4 id=\"PartialFormatter\" class=\"doc_header\"><code>class</code> <code>PartialFormatter</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
| 1448 | + "\n", |
| 1449 | + "> <code>PartialFormatter</code>() :: `Formatter`\n", |
| 1450 | + "\n", |
| 1451 | + "A `string.Formatter` that doesn't error on missing fields, and tracks missing fields and unused args" |
| 1452 | + ], |
| 1453 | + "text/plain": [ |
| 1454 | + "<IPython.core.display.Markdown object>" |
| 1455 | + ] |
| 1456 | + }, |
| 1457 | + "metadata": {}, |
| 1458 | + "output_type": "display_data" |
| 1459 | + } |
| 1460 | + ], |
| 1461 | + "source": [ |
| 1462 | + "show_doc(PartialFormatter, title_level=4)" |
| 1463 | + ] |
| 1464 | + }, |
1420 | 1465 | { |
1421 | 1466 | "cell_type": "code", |
1422 | 1467 | "execution_count": null, |
|
0 commit comments