|
48 | 48 | "from IPython.display import Markdown\n", |
49 | 49 | "from pprint import pprint\n", |
50 | 50 | "\n", |
51 | | - "from fastcore.test import test_eq" |
| 51 | + "from fastcore.test import test_eq, test_ne" |
52 | 52 | ] |
53 | 53 | }, |
54 | 54 | { |
|
743 | 743 | "print(Div('ho'))" |
744 | 744 | ] |
745 | 745 | }, |
| 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 | + }, |
746 | 787 | { |
747 | 788 | "cell_type": "markdown", |
748 | 789 | "id": "5ad30d7c", |
|
869 | 910 | "source": [] |
870 | 911 | } |
871 | 912 | ], |
872 | | - "metadata": { |
873 | | - "kernelspec": { |
874 | | - "display_name": "python3", |
875 | | - "language": "python", |
876 | | - "name": "python3" |
877 | | - } |
878 | | - }, |
| 913 | + "metadata": {}, |
879 | 914 | "nbformat": 4, |
880 | 915 | "nbformat_minor": 5 |
881 | 916 | } |
0 commit comments