[bottlepy] Adding ability to specify default value for MultiDict.getall

7 views
Skip to first unread message

Roman

unread,
May 6, 2010, 3:48:19 PM5/6/10
to bottlepy
Hello.

May I ask to add ability to specify default value for MultiDict.getall
method?

Something like the following

def getall( key, default=None ):
default = default if default is not None else []
return self.dict.get(key) or default

Thanks.

--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottle.paws.de/ for news and documentation.

Marcel Hellkamp

unread,
May 7, 2010, 8:18:16 AM5/7/10
to bott...@googlegroups.com
Am Donnerstag, den 06.05.2010, 12:48 -0700 schrieb Roman:
> May I ask to add ability to specify default value for MultiDict.getall
> method?

Perhaps, but do you really need this? MultiDict.getall() returns an
empty list on a missing key. Empty lists have a negative boolean value,
so you can do the following:

default = ['Default','Values']
for value in request.POST.getall(key) or default:
print value

I find this more natural and explicit than an additional getall()
argument. And there is another benefit: The right part of an 'or'
statement is not evaluated if the left part is true. You can use an
expensive function call on the right side and don't have to worry about
wasted time if the default value is not needed.

But on a second thought, the API difference between getall() ans get()
may be unintuitive.


--
Mit freundlichen Grüßen
Marcel Hellkamp

Roman

unread,
May 7, 2010, 9:40:20 AM5/7/10
to bottlepy
On May 7, 3:18 pm, Marcel Hellkamp <m...@gsites.de> wrote:
> Am Donnerstag, den 06.05.2010, 12:48 -0700 schrieb Roman:
>
> > May I ask to add ability to specify default value for MultiDict.getall
> > method?
>
> Perhaps, but do you really need this? MultiDict.getall() returns an
> empty list on a missing key. Empty lists have a negative boolean value,
> so you can do the following:
>
> default = ['Default','Values']
> for value in request.POST.getall(key) or default:
>     print value
>
> I find this more natural and explicit than an additional getall()
> argument. And there is another benefit: The right part of an 'or'
> statement is not evaluated if the left part is true. You can use an
> expensive function call on the right side and don't have to worry about
> wasted time if the default value is not needed.

:-). I am still not comfortable with such construct. The proposed
solution is fine with me.

Thanks

> But on a second thought, the API difference between getall() ans get()
> may be unintuitive.

May be you can take a look on C++ std::multimap and std::map
containers. They have a different "get item" interfaces.

http://www.sgi.com/tech/stl/Multimap.html
http://www.sgi.com/tech/stl/Map.html
Reply all
Reply to author
Forward
0 new messages