paul@345 | 1 | #!/usr/bin/env python |
paul@345 | 2 | |
paul@345 | 3 | """ |
paul@345 | 4 | Python value emulation. |
paul@345 | 5 | |
paul@345 | 6 | Copyright (C) 2008 Paul Boddie <paul@boddie.org.uk> |
paul@345 | 7 | |
paul@345 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@345 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paul@345 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paul@345 | 11 | later version. |
paul@345 | 12 | |
paul@345 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@345 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@345 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paul@345 | 16 | details. |
paul@345 | 17 | |
paul@345 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paul@345 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@345 | 20 | """ |
paul@345 | 21 | |
paul@345 | 22 | class ContentValue: |
paul@345 | 23 | |
paul@345 | 24 | """ |
paul@345 | 25 | Equality support for nodes having contents which should have some |
paul@345 | 26 | interoperability with normal Python objects. |
paul@345 | 27 | """ |
paul@345 | 28 | |
paul@345 | 29 | def __eq__(self, other): |
paul@345 | 30 | if hasattr(other, "contents"): |
paul@345 | 31 | return self.contents == other.contents |
paul@345 | 32 | else: |
paul@345 | 33 | return self.contents == other |
paul@345 | 34 | |
paul@345 | 35 | def __ne__(self, other): |
paul@345 | 36 | return not self.__eq__(other) |
paul@345 | 37 | |
paul@345 | 38 | class SequenceValue: |
paul@345 | 39 | |
paul@345 | 40 | """ |
paul@345 | 41 | Equality and access support for nodes having sequence-like contents. |
paul@345 | 42 | """ |
paul@345 | 43 | |
paul@345 | 44 | def __eq__(self, other): |
paul@345 | 45 | for i, j in map(None, self, other): |
paul@345 | 46 | if i != j: |
paul@345 | 47 | return False |
paul@345 | 48 | return True |
paul@345 | 49 | |
paul@345 | 50 | def __ne__(self, other): |
paul@345 | 51 | return not self.__eq__(other) |
paul@345 | 52 | |
paul@345 | 53 | def __len__(self): |
paul@345 | 54 | return len(self.values()) |
paul@345 | 55 | |
paul@345 | 56 | def __getitem__(self, i): |
paul@345 | 57 | return self.values()[i] |
paul@345 | 58 | |
paul@345 | 59 | # vim: tabstop=4 expandtab shiftwidth=4 |