From efb18d9dceb71673395467609d80060b423dd643 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Tue, 3 Apr 2012 22:40:02 +0200 Subject: [PATCH] add a method copy() --- weboob/capabilities/base.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/weboob/capabilities/base.py b/weboob/capabilities/base.py index ae42f161..0678a83b 100644 --- a/weboob/capabilities/base.py +++ b/weboob/capabilities/base.py @@ -21,7 +21,7 @@ import warnings import datetime from decimal import Decimal -from copy import deepcopy +from copy import deepcopy, copy from weboob.tools.misc import to_unicode from weboob.tools.ordereddict import OrderedDict @@ -295,6 +295,11 @@ class CapBaseObject(object): return False return True + def copy(self): + obj = copy(self) + obj._fields = copy(self._fields) + return obj + def set_empty_fields(self, value, excepts=()): """ Set the same value on all empty fields. @@ -315,7 +320,8 @@ class CapBaseObject(object): :rtype: iter[(key, value)] """ - yield 'id', self.id + if hasattr(self, 'id'): + yield 'id', self.id for name, field in self._fields.iteritems(): yield name, field.value @@ -358,3 +364,9 @@ class CapBaseObject(object): 'Value for "%s" needs to be of type %r, not %r' % ( name, attr.types, type(value))) attr.value = value + + def __delattr__(self, name): + try: + self._fields.pop(name) + except KeyError: + object.__delattr__(self, name)