Skip to content

Commit 91a3185

Browse files
committed
fixes #775
1 parent da0c35d commit 91a3185

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

fastcore/_modidx.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
'syms': { 'fastcore.all': {},
99
'fastcore.ansi': {},
1010
'fastcore.basics': { 'fastcore.basics.AttrDict': ('basics.html#attrdict', 'fastcore/basics.py'),
11-
'fastcore.basics.AttrDict.__dir__': ('basics.html#attrdict.__dir__', 'fastcore/basics.py'),
12-
'fastcore.basics.AttrDict.__getattr__': ('basics.html#attrdict.__getattr__', 'fastcore/basics.py'),
13-
'fastcore.basics.AttrDict.__setattr__': ('basics.html#attrdict.__setattr__', 'fastcore/basics.py'),
1411
'fastcore.basics.AttrDict._repr_markdown_': ('basics.html#attrdict._repr_markdown_', 'fastcore/basics.py'),
1512
'fastcore.basics.AttrDict.copy': ('basics.html#attrdict.copy', 'fastcore/basics.py'),
1613
'fastcore.basics.AttrDictDefault': ('basics.html#attrdictdefault', 'fastcore/basics.py'),
@@ -110,6 +107,10 @@
110107
'fastcore.basics._typeerr': ('basics.html#_typeerr', 'fastcore/basics.py'),
111108
'fastcore.basics._using_attr': ('basics.html#_using_attr', 'fastcore/basics.py'),
112109
'fastcore.basics.add_props': ('basics.html#add_props', 'fastcore/basics.py'),
110+
'fastcore.basics.adict': ('basics.html#adict', 'fastcore/basics.py'),
111+
'fastcore.basics.adict.__dir__': ('basics.html#adict.__dir__', 'fastcore/basics.py'),
112+
'fastcore.basics.adict.__getattr__': ('basics.html#adict.__getattr__', 'fastcore/basics.py'),
113+
'fastcore.basics.adict.__setattr__': ('basics.html#adict.__setattr__', 'fastcore/basics.py'),
113114
'fastcore.basics.anno_ret': ('basics.html#anno_ret', 'fastcore/basics.py'),
114115
'fastcore.basics.annotations': ('basics.html#annotations', 'fastcore/basics.py'),
115116
'fastcore.basics.argnames': ('basics.html#argnames', 'fastcore/basics.py'),

fastcore/basics.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'type_map', 'ifnone', 'maybe_attr', 'basic_repr', 'BasicRepr', 'is_array', 'listify', 'tuplify', 'true',
88
'NullType', 'tonull', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'exec_local',
99
'risinstance', 'ver2tuple', 'Inf', 'in_', 'ret_true', 'ret_false', 'stop', 'gen', 'chunked', 'otherwise',
10-
'custom_dir', 'AttrDict', 'AttrDictDefault', 'NS', 'get_annotations_ex', 'eval_type', 'type_hints',
10+
'custom_dir', 'adict', 'AttrDict', 'AttrDictDefault', 'NS', 'get_annotations_ex', 'eval_type', 'type_hints',
1111
'annotations', 'anno_ret', 'signature_ex', 'union2tuple', 'argnames', 'with_cast', 'store_attr', 'attrdict',
1212
'properties', 'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs',
1313
'hasattrs', 'setattrs', 'try_attrs', 'DepProp', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint',
@@ -269,18 +269,22 @@ def custom_dir(c, add):
269269
"Implement custom `__dir__`, adding `add` to `cls`"
270270
return object.__dir__(c) + listify(add)
271271

272-
# %% ../nbs/01_basics.ipynb #48249e34
273-
class AttrDict(dict):
272+
# %% ../nbs/01_basics.ipynb #ea75f86b
273+
class adict(dict):
274274
"`dict` subclass that also provides access to keys as attrs"
275275
def __getattr__(self,k): return self[k] if k in self else stop(AttributeError(k))
276276
def __setattr__(self, k, v): (self.__setitem__,super().__setattr__)[k[0]=='_'](k,v)
277277
def __dir__(self): return super().__dir__() + list(self.keys())
278+
279+
# %% ../nbs/01_basics.ipynb #28bf9743
280+
class AttrDict(adict):
281+
"`dict` subclass that also provides access to keys as attrs, and has a pretty markdown repr"
278282
def _repr_markdown_(self): return f'```python\n{pprint.pformat(self, indent=2)}\n```'
279283
def copy(self): return AttrDict(**self)
280284

281285
# %% ../nbs/01_basics.ipynb #cb8a0ff4
282286
class AttrDictDefault(AttrDict):
283-
"`AttrDict` subclass that returns `None` for missing attrs"
287+
"`AttrDict` subclass that returns `default_` for missing attrs"
284288
def __init__(self, *args, default_=None, **kwargs):
285289
self.default_ = default_
286290
super().__init__(*args, **kwargs)

nbs/01_basics.ipynb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,28 +1683,26 @@
16831683
{
16841684
"cell_type": "code",
16851685
"execution_count": null,
1686-
"id": "48249e34",
1686+
"id": "ea75f86b",
16871687
"metadata": {},
16881688
"outputs": [],
16891689
"source": [
16901690
"#| export\n",
1691-
"class AttrDict(dict):\n",
1691+
"class adict(dict):\n",
16921692
" \"`dict` subclass that also provides access to keys as attrs\"\n",
16931693
" def __getattr__(self,k): return self[k] if k in self else stop(AttributeError(k))\n",
16941694
" def __setattr__(self, k, v): (self.__setitem__,super().__setattr__)[k[0]=='_'](k,v)\n",
1695-
" def __dir__(self): return super().__dir__() + list(self.keys())\n",
1696-
" def _repr_markdown_(self): return f'```python\\n{pprint.pformat(self, indent=2)}\\n```'\n",
1697-
" def copy(self): return AttrDict(**self)"
1695+
" def __dir__(self): return super().__dir__() + list(self.keys())"
16981696
]
16991697
},
17001698
{
17011699
"cell_type": "code",
17021700
"execution_count": null,
1703-
"id": "80c67bed",
1701+
"id": "3e21c1e9",
17041702
"metadata": {},
17051703
"outputs": [],
17061704
"source": [
1707-
"d = AttrDict(a=1,b=\"two\")\n",
1705+
"d = adict(a=1,b=\"two\")\n",
17081706
"test_eq(d.a, 1)\n",
17091707
"test_eq(d['b'], 'two')\n",
17101708
"test_eq(d.get('c','nope'), 'nope')\n",
@@ -1717,6 +1715,20 @@
17171715
"assert 'a' in dir(d)"
17181716
]
17191717
},
1718+
{
1719+
"cell_type": "code",
1720+
"execution_count": null,
1721+
"id": "28bf9743",
1722+
"metadata": {},
1723+
"outputs": [],
1724+
"source": [
1725+
"#| export\n",
1726+
"class AttrDict(adict):\n",
1727+
" \"`dict` subclass that also provides access to keys as attrs, and has a pretty markdown repr\"\n",
1728+
" def _repr_markdown_(self): return f'```python\\n{pprint.pformat(self, indent=2)}\\n```'\n",
1729+
" def copy(self): return AttrDict(**self)"
1730+
]
1731+
},
17201732
{
17211733
"cell_type": "markdown",
17221734
"id": "ebf2a7a5",
@@ -1728,7 +1740,7 @@
17281740
{
17291741
"cell_type": "code",
17301742
"execution_count": null,
1731-
"id": "45d3197a",
1743+
"id": "d33822c6",
17321744
"metadata": {},
17331745
"outputs": [
17341746
{
@@ -1758,6 +1770,8 @@
17581770
}
17591771
],
17601772
"source": [
1773+
"d = AttrDict(a=1,b=\"two\")\n",
1774+
"\n",
17611775
"_test_dict = {'a':1, 'b': {'c':1, 'd':2}, 'c': {'c':1, 'd':2}, 'd': {'c':1, 'd':2},\n",
17621776
" 'e': {'c':1, 'd':2}, 'f': {'c':1, 'd':2, 'e': 4, 'f':[1,2,3,4,5]}}\n",
17631777
"AttrDict(_test_dict)"
@@ -1772,7 +1786,7 @@
17721786
"source": [
17731787
"#| export\n",
17741788
"class AttrDictDefault(AttrDict):\n",
1775-
" \"`AttrDict` subclass that returns `None` for missing attrs\"\n",
1789+
" \"`AttrDict` subclass that returns `default_` for missing attrs\"\n",
17761790
" def __init__(self, *args, default_=None, **kwargs):\n",
17771791
" self.default_ = default_\n",
17781792
" super().__init__(*args, **kwargs)\n",

0 commit comments

Comments
 (0)