Skip to content

Commit 52db6ca

Browse files
committed
Fix AttrDict
1 parent 285d3c3 commit 52db6ca

File tree

5 files changed

+56
-55
lines changed

5 files changed

+56
-55
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"gen": "01_basics.ipynb",
4040
"chunked": "01_basics.ipynb",
4141
"otherwise": "01_basics.ipynb",
42+
"custom_dir": "01_basics.ipynb",
4243
"AttrDict": "01_basics.ipynb",
4344
"with_cast": "01_basics.ipynb",
4445
"store_attr": "01_basics.ipynb",
@@ -104,7 +105,6 @@
104105
"working_directory": "02_foundation.ipynb",
105106
"add_docs": "02_foundation.ipynb",
106107
"docs": "02_foundation.ipynb",
107-
"custom_dir": "02_foundation.ipynb",
108108
"coll_repr": "02_foundation.ipynb",
109109
"is_bool": "02_foundation.ipynb",
110110
"mask2idxs": "02_foundation.ipynb",

fastcore/basics.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
__all__ = ['defaults', 'ifnone', 'maybe_attr', 'basic_repr', 'is_array', 'listify', 'get_class', 'mk_class',
44
'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance', 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq',
55
'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_', 'true', 'stop', 'gen', 'chunked', 'otherwise',
6-
'AttrDict', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel', 'class2attr',
7-
'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', 'detuplify', 'replicate', 'setify',
8-
'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle',
9-
'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first',
10-
'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1',
11-
'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr',
12-
'Self', 'Self', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed']
6+
'custom_dir', 'AttrDict', 'with_cast', 'store_attr', 'attrdict', 'properties', 'camel2snake', 'snake2camel',
7+
'class2attr', 'hasattrs', 'setattrs', 'try_attrs', 'ShowPrint', 'Int', 'Str', 'Float', 'detuplify',
8+
'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys',
9+
'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', 'range_of',
10+
'renumerate', 'first', 'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods', 'inum_methods',
11+
'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler',
12+
'instantiate', 'using_attr', 'Self', 'Self', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed']
1313

1414
# Cell
1515
from .imports import *
@@ -178,6 +178,11 @@ def otherwise(x, tst, y):
178178
"`y if tst(x) else x`"
179179
return y if tst(x) else x
180180

181+
# Cell
182+
def custom_dir(c, add:list):
183+
"Implement custom `__dir__`, adding `add` to `cls`"
184+
return dir(type(c)) + list(c.__dict__.keys()) + add
185+
181186
# Cell
182187
class AttrDict(dict):
183188
"`dict` subclass that also provides access to keys as attrs"

fastcore/foundation.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/02_foundation.ipynb (unless otherwise specified).
22

3-
__all__ = ['copy_func', 'patch_to', 'patch', 'patch_property', 'working_directory', 'add_docs', 'docs', 'custom_dir',
4-
'coll_repr', 'is_bool', 'mask2idxs', 'cycle', 'zip_cycle', 'is_indexer', 'GetAttr', 'delegate_attr',
5-
'CollBase', 'L', 'save_config_file', 'read_config_file', 'Config']
3+
__all__ = ['copy_func', 'patch_to', 'patch', 'patch_property', 'working_directory', 'add_docs', 'docs', 'coll_repr',
4+
'is_bool', 'mask2idxs', 'cycle', 'zip_cycle', 'is_indexer', 'GetAttr', 'delegate_attr', 'CollBase', 'L',
5+
'save_config_file', 'read_config_file', 'Config']
66

77
# Cell
88
from .imports import *
@@ -82,11 +82,6 @@ def docs(cls):
8282
add_docs(cls, **cls._docs)
8383
return cls
8484

85-
# Cell
86-
def custom_dir(c, add:list):
87-
"Implement custom `__dir__`, adding `add` to `cls`"
88-
return dir(type(c)) + list(c.__dict__.keys()) + add
89-
9085
# Cell
9186
def coll_repr(c, max_n=10):
9287
"String repr of up to `max_n` items of (possibly lazy) collection `c`"

nbs/01_basics.ipynb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,38 @@
911911
"These functions reduce boilerplate when setting or manipulating attributes or properties of objects."
912912
]
913913
},
914+
{
915+
"cell_type": "code",
916+
"execution_count": null,
917+
"metadata": {},
918+
"outputs": [],
919+
"source": [
920+
"#export\n",
921+
"def custom_dir(c, add:list):\n",
922+
" \"Implement custom `__dir__`, adding `add` to `cls`\"\n",
923+
" return dir(type(c)) + list(c.__dict__.keys()) + add"
924+
]
925+
},
926+
{
927+
"cell_type": "markdown",
928+
"metadata": {},
929+
"source": [
930+
"`custom_dir` allows you extract the [`__dict__` property of a class](https://stackoverflow.com/questions/19907442/explain-dict-attribute) and appends the list `add` to it."
931+
]
932+
},
933+
{
934+
"cell_type": "code",
935+
"execution_count": null,
936+
"metadata": {},
937+
"outputs": [],
938+
"source": [
939+
"class _T: \n",
940+
" def f(): pass\n",
941+
"\n",
942+
"s = custom_dir(_T, add=['foo', 'bar']) # a list of everything in `__dict__` of `_T` with ['foo', 'bar'] appended.\n",
943+
"assert {'foo', 'bar', 'f'}.issubset(s)"
944+
]
945+
},
914946
{
915947
"cell_type": "code",
916948
"execution_count": null,
@@ -966,7 +998,8 @@
966998
"test_eq(d['b'], 2)\n",
967999
"d['b'] = 3\n",
9681000
"test_eq(d['b'], 3)\n",
969-
"test_eq(d.b, 3)"
1001+
"test_eq(d.b, 3)\n",
1002+
"assert 'a' in dir(d)"
9701003
]
9711004
},
9721005
{

nbs/02_foundation.ipynb

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -565,38 +565,6 @@
565565
"test_eq(_T.f.__doc__, \"The docstring for method f.\")"
566566
]
567567
},
568-
{
569-
"cell_type": "code",
570-
"execution_count": null,
571-
"metadata": {},
572-
"outputs": [],
573-
"source": [
574-
"#export\n",
575-
"def custom_dir(c, add:list):\n",
576-
" \"Implement custom `__dir__`, adding `add` to `cls`\"\n",
577-
" return dir(type(c)) + list(c.__dict__.keys()) + add"
578-
]
579-
},
580-
{
581-
"cell_type": "markdown",
582-
"metadata": {},
583-
"source": [
584-
"`custom_dir` allows you extract the [`__dict__` property of a class](https://stackoverflow.com/questions/19907442/explain-dict-attribute) and appends the list `add` to it."
585-
]
586-
},
587-
{
588-
"cell_type": "code",
589-
"execution_count": null,
590-
"metadata": {},
591-
"outputs": [],
592-
"source": [
593-
"class _T: \n",
594-
" def f(): pass\n",
595-
"\n",
596-
"s = custom_dir(_T, add=['foo', 'bar']) # a list of everything in `__dict__` of `_T` with ['foo', 'bar'] appended.\n",
597-
"assert {'foo', 'bar', 'f'}.issubset(s)"
598-
]
599-
},
600568
{
601569
"cell_type": "code",
602570
"execution_count": null,
@@ -1552,7 +1520,7 @@
15521520
{
15531521
"data": {
15541522
"text/plain": [
1555-
"[2, 'j', 1]"
1523+
"[3, 9, 'j']"
15561524
]
15571525
},
15581526
"execution_count": null,
@@ -2752,12 +2720,12 @@
27522720
"Converted 00_test.ipynb.\n",
27532721
"Converted 01_basics.ipynb.\n",
27542722
"Converted 02_foundation.ipynb.\n",
2755-
"Converted 03_dispatch.ipynb.\n",
27562723
"Converted 03_xtras.ipynb.\n",
2757-
"Converted 04_transform.ipynb.\n",
2758-
"Converted 05_logargs.ipynb.\n",
2759-
"Converted 06_meta.ipynb.\n",
2760-
"Converted 07_script.ipynb.\n",
2724+
"Converted 04_dispatch.ipynb.\n",
2725+
"Converted 05_transform.ipynb.\n",
2726+
"Converted 06_logargs.ipynb.\n",
2727+
"Converted 07_meta.ipynb.\n",
2728+
"Converted 08_script.ipynb.\n",
27612729
"Converted index.ipynb.\n"
27622730
]
27632731
}

0 commit comments

Comments
 (0)