Skip to content

Commit 0092436

Browse files
committed
fixes #600
1 parent f05f425 commit 0092436

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@
568568
'fastcore.xml.FT.__setattr__': ('xml.html#ft.__setattr__', 'fastcore/xml.py'),
569569
'fastcore.xml.FT.attrs': ('xml.html#ft.attrs', 'fastcore/xml.py'),
570570
'fastcore.xml.FT.children': ('xml.html#ft.children', 'fastcore/xml.py'),
571+
'fastcore.xml.FT.get': ('xml.html#ft.get', 'fastcore/xml.py'),
571572
'fastcore.xml.FT.tag': ('xml.html#ft.tag', 'fastcore/xml.py'),
572573
'fastcore.xml.Html': ('xml.html#html', 'fastcore/xml.py'),
573574
'fastcore.xml.Safe': ('xml.html#safe', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def __setattr__(self, k, v):
4141

4242
def __getattr__(self, k):
4343
if k.startswith('__') or k not in self.attrs: raise AttributeError(k)
44-
return self.attrs[k.lstrip('_').replace('_', '-')]
44+
return self.get(k)
45+
46+
def get(self, k, default=None): return self[2].get(k.lstrip('_').replace('_', '-'), default)
4547

4648
# %% ../nbs/11_xml.ipynb
4749
def attrmap(o):

nbs/11_xml.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
"\n",
7878
" def __getattr__(self, k):\n",
7979
" if k.startswith('__') or k not in self.attrs: raise AttributeError(k)\n",
80-
" return self.attrs[k.lstrip('_').replace('_', '-')]"
80+
" return self.get(k)\n",
81+
"\n",
82+
" def get(self, k, default=None): return self[2].get(k.lstrip('_').replace('_', '-'), default)"
8183
]
8284
},
8385
{
@@ -266,7 +268,7 @@
266268
"name": "stdout",
267269
"output_type": "stream",
268270
"text": [
269-
"newid\n"
271+
"newid newid missing\n"
270272
]
271273
},
272274
{
@@ -282,7 +284,7 @@
282284
],
283285
"source": [
284286
"elem.id = 'newid'\n",
285-
"print(elem.id)\n",
287+
"print(elem.id, elem.get('id'), elem.get('foo', 'missing'))\n",
286288
"elem"
287289
]
288290
},

0 commit comments

Comments
 (0)