On Feb 21, 6:48 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> How about
>
> def f(self, a=[], b=[])
>
> Just a guess though. To be brutally honest I'm not sure if there even is
> a validator invoked in case of no value being present at all. IF so,
> you could of course create your own ForEach-validator that just does
> this, overloading the _to_python-method (or whichever is needed, but I
> guess that's the one) to return an empty list if the
> super-implementation doesn't.
I took a stab at making my own validator. Comments requested:
class IntegerList(validators.FancyValidator):
"Make sure that we get a list of integers back."
messages = dict()
def _to_python(self, value, state):
if value == "empty list": return []
elif isinstance(value, list): return [int(x) for x in value]
else: return [int(value)]
Then I changed my f controller to be like this:
@validate(validators={'a':IntegerList()},
'b':IntegerList()})
@expose()
def f(self, a="empty list", b="empty list"):
log.debug("a is %s." % a)
log.debug("b is %s." % b)
return dict(a=a, b=b)
Everything seems to work OK.
I don't like the idea of putting the empty lists into the method
definition. I've had bad luck with that in the past when python would
create a single list and keep it across function calls, like this:
In [1]: def f(a=[]):
...: a.append(1)
...: return a
...:
In [2]: f()
Out[2]: [1]
In [3]: f()
Out[3]: [1, 1]
I don't know enough about cherrypy and turbogears to be sure that I
woudn't face the same problem. So instead, I'm using the string
"empty list".
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---