From 1bc806dd90a721ba892ac8735cd9afa38553593b Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Mon, 20 Feb 2012 11:40:40 +0100 Subject: [PATCH] :class:`wsattr` now takes a 'default' parameter --- doc/changes.rst | 3 +++ wsme/types.py | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/changes.rst b/doc/changes.rst index 4e37bde..830eb3b 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -6,6 +6,9 @@ next * Now handle dict and UserType types as GET/POST params. +* :class:`wsattr` now takes a 'default' parameter that will be returned + instead of 'Unset' if no value has been set. + 0.3b1 ----- diff --git a/wsme/types.py b/wsme/types.py index e35c6c8..91ee70d 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -183,7 +183,7 @@ class wsattr(object): mandatoryvalue = wsattr(int, mandatory=True) """ - def __init__(self, datatype, mandatory=False, name=None): + def __init__(self, datatype, mandatory=False, name=None, default=Unset): #: The attribute name in the parent python class. #: Set by :func:`inspect_class` self.key = None # will be set by class inspection @@ -194,11 +194,14 @@ class wsattr(object): self.datatype = datatype #: True if the attribute is mandatory self.mandatory = mandatory + #: Default value. The attribute will return this instead + #: of :data:`Unset` if no value has been set. + self.default = default def __get__(self, instance, owner): if instance is None: return self - return getattr(instance, '_' + self.key, Unset) + return getattr(instance, '_' + self.key, self.default) def __set__(self, instance, value): try: