Skip to content

Commit 39a78c3

Browse files
committed
fixes #248
1 parent ec76bb2 commit 39a78c3

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"Path.mk_write": "03_xtras.ipynb",
159159
"Path.ls": "03_xtras.ipynb",
160160
"Path.__repr__": "03_xtras.ipynb",
161+
"time_events": "03_xtras.ipynb",
161162
"stringfmt_names": "03_xtras.ipynb",
162163
"PartialFormatter": "03_xtras.ipynb",
163164
"partial_format": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
__all__ = ['dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection',
44
'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'loads', 'untar_dir', 'repo_details', 'run',
5-
'open_file', 'save_pickle', 'load_pickle', 'stringfmt_names', 'PartialFormatter', 'partial_format',
6-
'utc2local', 'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers', 'str2bool',
7-
'sort_by_run']
5+
'open_file', 'save_pickle', 'load_pickle', 'time_events', 'stringfmt_names', 'PartialFormatter',
6+
'partial_format', 'utc2local', 'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers',
7+
'str2bool', 'sort_by_run']
88

99
# Cell
1010
from .imports import *
@@ -17,6 +17,7 @@
1717
from contextlib import contextmanager,ExitStack
1818
from pdb import set_trace
1919
from datetime import datetime, timezone
20+
from timeit import default_timer
2021

2122
# Cell
2223
def dict2obj(d):
@@ -220,6 +221,12 @@ def __repr__(self:Path):
220221
except: pass
221222
return f"Path({self.as_posix()!r})"
222223

224+
# Cell
225+
def time_events():
226+
"A simple event timer implemented as a coroutine"
227+
start,events = default_timer(),0
228+
while True: events += (yield events,events/(default_timer()-start)) or 0
229+
223230
# Cell
224231
_fmt = string.Formatter()
225232

nbs/03_xtras.ipynb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"import imghdr,struct,distutils.util,tempfile,time,string\n",
2626
"from contextlib import contextmanager,ExitStack\n",
2727
"from pdb import set_trace\n",
28-
"from datetime import datetime, timezone"
28+
"from datetime import datetime, timezone\n",
29+
"from timeit import default_timer"
2930
]
3031
},
3132
{
@@ -36,7 +37,8 @@
3637
"source": [
3738
"from fastcore.test import *\n",
3839
"from nbdev.showdoc import *\n",
39-
"from fastcore.nb_imports import *"
40+
"from fastcore.nb_imports import *\n",
41+
"from time import sleep"
4042
]
4143
},
4244
{
@@ -376,7 +378,7 @@
376378
"text/markdown": [
377379
"<h4 id=\"ReindexCollection\" class=\"doc_header\"><code>class</code> <code>ReindexCollection</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
378380
"\n",
379-
"> <code>ReindexCollection</code>(**`coll`**, **`idxs`**=*`None`*, **`cache`**=*`None`*, **`tfm`**=*`noop`*) :: [`GetAttr`](/foundation.html#GetAttr)\n",
381+
"> <code>ReindexCollection</code>(**`coll`**, **`idxs`**=*`None`*, **`cache`**=*`None`*, **`tfm`**=*`noop`*) :: [`GetAttr`](/basics.html#GetAttr)\n",
380382
"\n",
381383
"Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`"
382384
],
@@ -616,7 +618,7 @@
616618
{
617619
"data": {
618620
"text/plain": [
619-
"['f', 'd', 'e', 'g', 'a', 'b', 'h', 'c']"
621+
"['d', 'h', 'g', 'a', 'c', 'f', 'e', 'b']"
620622
]
621623
},
622624
"execution_count": null,
@@ -1317,6 +1319,50 @@
13171319
"## Other Helpers"
13181320
]
13191321
},
1322+
{
1323+
"cell_type": "code",
1324+
"execution_count": null,
1325+
"metadata": {},
1326+
"outputs": [],
1327+
"source": [
1328+
"#export\n",
1329+
"def time_events():\n",
1330+
" \"A simple event timer implemented as a coroutine\"\n",
1331+
" start,events = default_timer(),0\n",
1332+
" while True: events += (yield events,events/(default_timer()-start)) or 0"
1333+
]
1334+
},
1335+
{
1336+
"cell_type": "markdown",
1337+
"metadata": {},
1338+
"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."
1340+
]
1341+
},
1342+
{
1343+
"cell_type": "code",
1344+
"execution_count": null,
1345+
"metadata": {},
1346+
"outputs": [
1347+
{
1348+
"name": "stdout",
1349+
"output_type": "stream",
1350+
"text": [
1351+
"# Events: 10, Freq/sec: 64.50\n"
1352+
]
1353+
}
1354+
],
1355+
"source": [
1356+
"# Random wait function for testing `time_events`\n",
1357+
"def _randwait(): yield from (sleep(random.random()/30) for _ in range(10))\n",
1358+
"\n",
1359+
"c = time_events()\n",
1360+
"c.send(None) # Start timer by sending `None`\n",
1361+
"for o in _randwait(): c.send(1) # Send an event\n",
1362+
"events,freq = next(c) # `next` returns counter values\n",
1363+
"print(f'# Events: {events}, Freq/sec: {freq:.02f}')"
1364+
]
1365+
},
13201366
{
13211367
"cell_type": "code",
13221368
"execution_count": null,

0 commit comments

Comments
 (0)