From 92efe05fed96d83381ad5630221a11908bd716c4 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 10 Oct 2011 21:55:44 +0200 Subject: [PATCH] client-barcode: Implement (in)equality test for class Item --- client-barcode/freitagslib/item.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client-barcode/freitagslib/item.py b/client-barcode/freitagslib/item.py index 7c3ef35..197927f 100644 --- a/client-barcode/freitagslib/item.py +++ b/client-barcode/freitagslib/item.py @@ -12,3 +12,22 @@ class Item: self.id = int(d['id']) self.name = d['name'] self.price = Decimal(d['price']) + + def __eq__(self, other): + for key in ('deposit', 'id', 'name', 'price'): + if getattr(self, key) != getattr(other, key): + return False + return True + + def __ne__(self, other): + return not self.__eq__(other) + + +if __name__ == '__main__': + import copy + d1 = {'deposit':'0.20', 'id':'15', 'name':'foo bar', 'price':'1.40'} + d2 = copy.copy(d1) + a = Item(d1) + b = Item(d2) + print(a == b) + print(a != b)