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