Skip to content

Commit 5cc17e6

Browse files
committed
fixes #682
1 parent 3e45456 commit 5cc17e6

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,10 @@
514514
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
515515
'fastcore.xml': { 'fastcore.xml.FT': ('xml.html#ft', 'fastcore/xml.py'),
516516
'fastcore.xml.FT.__call__': ('xml.html#ft.__call__', 'fastcore/xml.py'),
517+
'fastcore.xml.FT.__eq__': ('xml.html#ft.__eq__', 'fastcore/xml.py'),
517518
'fastcore.xml.FT.__getattr__': ('xml.html#ft.__getattr__', 'fastcore/xml.py'),
518519
'fastcore.xml.FT.__getitem__': ('xml.html#ft.__getitem__', 'fastcore/xml.py'),
520+
'fastcore.xml.FT.__hash__': ('xml.html#ft.__hash__', 'fastcore/xml.py'),
519521
'fastcore.xml.FT.__html__': ('xml.html#ft.__html__', 'fastcore/xml.py'),
520522
'fastcore.xml.FT.__init__': ('xml.html#ft.__init__', 'fastcore/xml.py'),
521523
'fastcore.xml.FT.__iter__': ('xml.html#ft.__iter__', 'fastcore/xml.py'),

fastcore/xml.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ def to_xml(elm, lvl=0, indent=True, do_escape=True):
220220
def __html__(self:FT): return to_xml(self, indent=False)
221221
FT.__str__ = FT.__html__
222222

223+
# %% ../nbs/09_xml.ipynb
224+
@patch
225+
def __eq__(self:FT, other):
226+
if not isinstance(other, FT): return False
227+
return self.tag==other.tag and self.attrs==other.attrs and self.children==other.children
228+
229+
@patch
230+
def __hash__(self:FT): return hash((self.tag, tuple(sorted(self.attrs.items())), self.children))
231+
223232
# %% ../nbs/09_xml.ipynb
224233
def highlight(s, lang='html'):
225234
"Markdown to syntax-highlight `s` in language `lang`"

nbs/09_xml.ipynb

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"from IPython.display import Markdown\n",
4949
"from pprint import pprint\n",
5050
"\n",
51-
"from fastcore.test import test_eq"
51+
"from fastcore.test import test_eq, test_ne"
5252
]
5353
},
5454
{
@@ -743,6 +743,47 @@
743743
"print(Div('ho'))"
744744
]
745745
},
746+
{
747+
"cell_type": "code",
748+
"execution_count": null,
749+
"id": "cd8a6aff",
750+
"metadata": {},
751+
"outputs": [],
752+
"source": [
753+
"#| export\n",
754+
"@patch\n",
755+
"def __eq__(self:FT, other):\n",
756+
" if not isinstance(other, FT): return False\n",
757+
" return self.tag==other.tag and self.attrs==other.attrs and self.children==other.children\n",
758+
"\n",
759+
"@patch\n",
760+
"def __hash__(self:FT): return hash((self.tag, tuple(sorted(self.attrs.items())), self.children))"
761+
]
762+
},
763+
{
764+
"cell_type": "markdown",
765+
"id": "1abc3f67",
766+
"metadata": {},
767+
"source": [
768+
"`FT` object equality and hashing is based on tag, attrs, and children."
769+
]
770+
},
771+
{
772+
"cell_type": "code",
773+
"execution_count": null,
774+
"id": "8f665b3d",
775+
"metadata": {},
776+
"outputs": [],
777+
"source": [
778+
"test_eq(Div('hello', id='x'), Div('hello', id='x'))\n",
779+
"test_ne(Div('hello'), Div('goodbye'))\n",
780+
"test_ne(Div('hello', id='a'), Div('hello', id='b'))\n",
781+
"test_ne(P('hello'), Div('hello'))\n",
782+
"\n",
783+
"test_eq(hash(Div('hello', id='x')), hash(Div('hello', id='x')))\n",
784+
"assert hash(Div('hello')), hash(Div('goodbye'))"
785+
]
786+
},
746787
{
747788
"cell_type": "markdown",
748789
"id": "5ad30d7c",
@@ -869,13 +910,7 @@
869910
"source": []
870911
}
871912
],
872-
"metadata": {
873-
"kernelspec": {
874-
"display_name": "python3",
875-
"language": "python",
876-
"name": "python3"
877-
}
878-
},
913+
"metadata": {},
879914
"nbformat": 4,
880915
"nbformat_minor": 5
881916
}

0 commit comments

Comments
 (0)