|
688 | 688 | {
|
689 | 689 | "data": {
|
690 | 690 | "text/plain": [
|
691 |
| - "'pip 25.1.1 from /Users/jhoward/aai-ws/.venv/lib/python3.12/site-packages/pip (python 3.12)'" |
| 691 | + "'pip 25.2 from /Users/jhoward/aai-ws/.venv/lib/python3.12/site-packages/pip (python 3.12)'" |
692 | 692 | ]
|
693 | 693 | },
|
694 | 694 | "execution_count": null,
|
|
1441 | 1441 | "text/markdown": [
|
1442 | 1442 | "---\n",
|
1443 | 1443 | "\n",
|
1444 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L389){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1444 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1445 | 1445 | "\n",
|
1446 | 1446 | "#### ReindexCollection\n",
|
1447 | 1447 | "\n",
|
|
1452 | 1452 | "text/plain": [
|
1453 | 1453 | "---\n",
|
1454 | 1454 | "\n",
|
1455 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L389){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1455 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L391){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1456 | 1456 | "\n",
|
1457 | 1457 | "#### ReindexCollection\n",
|
1458 | 1458 | "\n",
|
|
1524 | 1524 | "text/markdown": [
|
1525 | 1525 | "---\n",
|
1526 | 1526 | "\n",
|
1527 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L400){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1527 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L402){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1528 | 1528 | "\n",
|
1529 | 1529 | "###### ReindexCollection.reindex\n",
|
1530 | 1530 | "\n",
|
|
1535 | 1535 | "text/plain": [
|
1536 | 1536 | "---\n",
|
1537 | 1537 | "\n",
|
1538 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L400){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1538 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L402){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1539 | 1539 | "\n",
|
1540 | 1540 | "###### ReindexCollection.reindex\n",
|
1541 | 1541 | "\n",
|
|
1624 | 1624 | "text/markdown": [
|
1625 | 1625 | "---\n",
|
1626 | 1626 | "\n",
|
1627 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L404){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1627 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L406){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1628 | 1628 | "\n",
|
1629 | 1629 | "##### ReindexCollection.cache_clear\n",
|
1630 | 1630 | "\n",
|
|
1635 | 1635 | "text/plain": [
|
1636 | 1636 | "---\n",
|
1637 | 1637 | "\n",
|
1638 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L404){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1638 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L406){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1639 | 1639 | "\n",
|
1640 | 1640 | "##### ReindexCollection.cache_clear\n",
|
1641 | 1641 | "\n",
|
|
1689 | 1689 | "text/markdown": [
|
1690 | 1690 | "---\n",
|
1691 | 1691 | "\n",
|
1692 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L401){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1692 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L403){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1693 | 1693 | "\n",
|
1694 | 1694 | "##### ReindexCollection.shuffle\n",
|
1695 | 1695 | "\n",
|
|
1700 | 1700 | "text/plain": [
|
1701 | 1701 | "---\n",
|
1702 | 1702 | "\n",
|
1703 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L401){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 1703 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L403){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
1704 | 1704 | "\n",
|
1705 | 1705 | "##### ReindexCollection.shuffle\n",
|
1706 | 1706 | "\n",
|
|
1733 | 1733 | {
|
1734 | 1734 | "data": {
|
1735 | 1735 | "text/plain": [
|
1736 |
| - "['g', 'c', 'f', 'd', 'a', 'b', 'e', 'h']" |
| 1736 | + "['f', 'b', 'd', 'e', 'g', 'a', 'c', 'h']" |
1737 | 1737 | ]
|
1738 | 1738 | },
|
1739 | 1739 | "execution_count": null,
|
|
2274 | 2274 | {
|
2275 | 2275 | "data": {
|
2276 | 2276 | "text/plain": [
|
2277 |
| - "'https://github.com/fastai/fastcore/tree/master/fastcore/test.py#L37'" |
| 2277 | + "'https://github.com/fastai/fastcore/tree/master/fastcore/test.py#L38'" |
2278 | 2278 | ]
|
2279 | 2279 | },
|
2280 | 2280 | "execution_count": null,
|
|
2577 | 2577 | "text/markdown": [
|
2578 | 2578 | "---\n",
|
2579 | 2579 | "\n",
|
2580 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L532){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2580 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L573){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2581 | 2581 | "\n",
|
2582 | 2582 | "#### EventTimer\n",
|
2583 | 2583 | "\n",
|
|
2588 | 2588 | "text/plain": [
|
2589 | 2589 | "---\n",
|
2590 | 2590 | "\n",
|
2591 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L532){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2591 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L573){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2592 | 2592 | "\n",
|
2593 | 2593 | "#### EventTimer\n",
|
2594 | 2594 | "\n",
|
|
2622 | 2622 | "name": "stdout",
|
2623 | 2623 | "output_type": "stream",
|
2624 | 2624 | "text": [
|
2625 |
| - "Num Events: 13, Freq/sec: 451.7\n", |
2626 |
| - "Most recent: ▁▁▁▂▇ 264.5 257.0 278.7 293.7 363.0\n" |
| 2625 | + "Num Events: 1, Freq/sec: 161.2\n", |
| 2626 | + "Most recent: ▁▇▆▁▃ 59.3 132.1 120.0 58.9 92.0\n" |
2627 | 2627 | ]
|
2628 | 2628 | }
|
2629 | 2629 | ],
|
|
2702 | 2702 | "text/markdown": [
|
2703 | 2703 | "---\n",
|
2704 | 2704 | "\n",
|
2705 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L564){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2705 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L605){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2706 | 2706 | "\n",
|
2707 | 2707 | "#### PartialFormatter\n",
|
2708 | 2708 | "\n",
|
|
2713 | 2713 | "text/plain": [
|
2714 | 2714 | "---\n",
|
2715 | 2715 | "\n",
|
2716 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L564){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2716 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L605){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2717 | 2717 | "\n",
|
2718 | 2718 | "#### PartialFormatter\n",
|
2719 | 2719 | "\n",
|
|
2918 | 2918 | "text/markdown": [
|
2919 | 2919 | "---\n",
|
2920 | 2920 | "\n",
|
2921 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L621){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2921 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L662){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2922 | 2922 | "\n",
|
2923 | 2923 | "#### ContextManagers\n",
|
2924 | 2924 | "\n",
|
|
2929 | 2929 | "text/plain": [
|
2930 | 2930 | "---\n",
|
2931 | 2931 | "\n",
|
2932 |
| - "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L621){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
| 2932 | + "[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/xtras.py#L662){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", |
2933 | 2933 | "\n",
|
2934 | 2934 | "#### ContextManagers\n",
|
2935 | 2935 | "\n",
|
|
3362 | 3362 | " elif hasattr(o, '__iter__'):\n",
|
3363 | 3363 | " try: r = dict(o)\n",
|
3364 | 3364 | " except TypeError: pass\n",
|
3365 |
| - " elif hasattr(o, '__dict__'): r = o.__dict__\n", |
3366 |
| - " else: raise TypeError(f'Can not convert {o} to a dict')\n", |
3367 |
| - " return {k:v for k,v in r.items() if v not in (UNSET,MISSING)}" |
| 3365 | + " elif hasattr(o, '__flds__'): r = {x:getattr(o,x) for x in o.__flds__}\n", |
| 3366 | + " else: r = vars(o)\n", |
| 3367 | + " skip = set(getattr(o, '__skip__', []))\n", |
| 3368 | + " return {k:v for k,v in r.items() if v is not UNSET and v is not MISSING and k not in skip}" |
3368 | 3369 | ]
|
3369 | 3370 | },
|
3370 | 3371 | {
|
|
3394 | 3395 | "asdict(bob)"
|
3395 | 3396 | ]
|
3396 | 3397 | },
|
| 3398 | + { |
| 3399 | + "cell_type": "markdown", |
| 3400 | + "metadata": {}, |
| 3401 | + "source": [ |
| 3402 | + "Set the optional `__flds__` parameter to customise the field list, and the optional `__skip__` parameter to skip some names." |
| 3403 | + ] |
| 3404 | + }, |
| 3405 | + { |
| 3406 | + "cell_type": "code", |
| 3407 | + "execution_count": null, |
| 3408 | + "metadata": {}, |
| 3409 | + "outputs": [], |
| 3410 | + "source": [ |
| 3411 | + "class CustomObj:\n", |
| 3412 | + " def __init__(self): self.a,self.b,self.c,self.d = 1,2,3,4\n", |
| 3413 | + " __flds__ = ['a','b','c','d']\n", |
| 3414 | + " __skip__ = ['b']\n", |
| 3415 | + "\n", |
| 3416 | + "obj = CustomObj()\n", |
| 3417 | + "test_eq(asdict(obj), {'a': 1, 'c': 3, 'd': 4})" |
| 3418 | + ] |
| 3419 | + }, |
3397 | 3420 | {
|
3398 | 3421 | "cell_type": "markdown",
|
3399 | 3422 | "metadata": {},
|
3400 | 3423 | "source": [
|
3401 | 3424 | "To customise dict conversion behavior for a class, implement the `_asdict` method (this is used in the Python stdlib for named tuples)."
|
3402 | 3425 | ]
|
3403 | 3426 | },
|
| 3427 | + { |
| 3428 | + "cell_type": "code", |
| 3429 | + "execution_count": null, |
| 3430 | + "metadata": {}, |
| 3431 | + "outputs": [], |
| 3432 | + "source": [ |
| 3433 | + "#| export\n", |
| 3434 | + "def vars_pub(x):\n", |
| 3435 | + " \"Get public non-skipped vars\"\n", |
| 3436 | + " skip = set(getattr(x, '__skip__', []))\n", |
| 3437 | + " return [o for o in vars(x) if o[0]!='_' and o not in skip]" |
| 3438 | + ] |
| 3439 | + }, |
| 3440 | + { |
| 3441 | + "cell_type": "markdown", |
| 3442 | + "metadata": {}, |
| 3443 | + "source": [ |
| 3444 | + "The `vars_pub` function returns a list of public (non-underscore-prefixed) variable names from an object, excluding any names listed in the object's optional `__skip__` attribute." |
| 3445 | + ] |
| 3446 | + }, |
| 3447 | + { |
| 3448 | + "cell_type": "code", |
| 3449 | + "execution_count": null, |
| 3450 | + "metadata": {}, |
| 3451 | + "outputs": [], |
| 3452 | + "source": [ |
| 3453 | + "class TestObj:\n", |
| 3454 | + " def __init__(self): self.pub_attr,self._priv_attr,self.another_pub,self.skip_me = 1,2,3,4\n", |
| 3455 | + " __skip__ = ['skip_me']\n", |
| 3456 | + "\n", |
| 3457 | + "obj = TestObj()\n", |
| 3458 | + "test_eq(vars_pub(obj), ['pub_attr', 'another_pub'])" |
| 3459 | + ] |
| 3460 | + }, |
| 3461 | + { |
| 3462 | + "cell_type": "markdown", |
| 3463 | + "metadata": {}, |
| 3464 | + "source": [ |
| 3465 | + "Without `__skip__`, all pub vars are returned" |
| 3466 | + ] |
| 3467 | + }, |
| 3468 | + { |
| 3469 | + "cell_type": "code", |
| 3470 | + "execution_count": null, |
| 3471 | + "metadata": {}, |
| 3472 | + "outputs": [], |
| 3473 | + "source": [ |
| 3474 | + "class SimpleObj:\n", |
| 3475 | + " def __init__(self): self.a,self._b,self.c = 1,2,3\n", |
| 3476 | + "\n", |
| 3477 | + "simple = SimpleObj()\n", |
| 3478 | + "test_eq(vars_pub(simple), ['a', 'c'])" |
| 3479 | + ] |
| 3480 | + }, |
3404 | 3481 | {
|
3405 | 3482 | "cell_type": "code",
|
3406 | 3483 | "execution_count": null,
|
|
0 commit comments