Google Groups Home
Help | Sign in
Comments regarding QuerySet.valuelist()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Michael Radziej  
View profile
 More options Oct 31 2006, 2:49 pm
From: Michael Radziej <m...@noris.de>
Date: Tue, 31 Oct 2006 20:49:19 +0100
Local: Tues, Oct 31 2006 2:49 pm
Subject: Comments regarding QuerySet.valuelist()
Hi,

Ticket 2482 by mccutchen (thanks!) adds a method 'valuelist()' to
QuerySet. It works similar to values(), but generates tuples
instead of dicts.

I use this, and I really appreciate the idea, but there's one
thing I don't like about it. If you pass only one attribute, it
yields single values, not singletons, i.e.

 >>> list(qset.valuelist("a","b"))
[(a1,b1), (a2,b2), ...]

 >>> list(qset.valuelist("a"))
[a1, a2]

This is very neat most times, especially when you retrieve ids to
pass to bulk_in, but as soon as you start to use this more
generically like

valuelist(*attnames)

you get different structures depending on the length of attnames,
and this feels so wrong. It leads to code like this:

qset_hits = data_source.query_set().filter(
     **{"%s__istartswith" % data_source.att_names[0]: query}
     ).valuelist(*data_source.att_names)
if len(self.att_names)==1:
     qset_hits = ((x,) for x in qset_hits)

I propose to split the functionality into two separate methods,
valueslist() and valuetupels(). valueslist() would only allow to
pass in a single attribute name.

valuelist(attname) --> always yields single values
   e.g., list(valuelist("a"))   --> [a1, a2, ...]
valuetuples(*attnames) --> always yields tuples
   e.g., list(valuetuples("a")) --> [(a1,), (a2,), ...]

What do you think? Am I already splitting hairs?

Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49 911 9352-0 - Fax +49 911 9352-100

http://www.noris.de - The IT-Outsourcing Company


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile
 More options Nov 4 2006, 5:32 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sat, 04 Nov 2006 22:32:58 -0000
Local: Sat, Nov 4 2006 5:32 pm
Subject: Re: Comments regarding QuerySet.valuelist()
I think you raise a valid point.  There are valid use cases for both
methods.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Bennett  
View profile
 More options Nov 4 2006, 6:57 pm
From: "James Bennett" <ubernost...@gmail.com>
Date: Sat, 4 Nov 2006 17:57:38 -0600
Local: Sat, Nov 4 2006 6:57 pm
Subject: Re: Comments regarding QuerySet.valuelist()
On 10/31/06, Michael Radziej <m...@noris.de> wrote:

> I propose to split the functionality into two separate methods,
> valueslist() and valuetupels(). valueslist() would only allow to
> pass in a single attribute name.

> valuelist(attname) --> always yields single values
>    e.g., list(valuelist("a"))   --> [a1, a2, ...]
> valuetuples(*attnames) --> always yields tuples
>    e.g., list(valuetuples("a")) --> [(a1,), (a2,), ...]

> What do you think? Am I already splitting hairs?

No, I think you're right that we should have a mechanism in place for
ensuring that methods like this always return results of a consistent
type. I'm not sure whether it'd be better to do two methods or to
return singleton tuples, though; anybody got thoughts one way or
another?

--
"May the forces of evil become confused on the way to your house."
  -- George Carlin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Wilson  
View profile
 More options Nov 4 2006, 11:34 pm
From: "Gary Wilson" <gary.wil...@gmail.com>
Date: Sun, 05 Nov 2006 04:34:42 -0000
Local: Sat, Nov 4 2006 11:34 pm
Subject: Re: Comments regarding QuerySet.valuelist()

> No, I think you're right that we should have a mechanism in place for
> ensuring that methods like this always return results of a consistent
> type. I'm not sure whether it'd be better to do two methods or to
> return singleton tuples, though; anybody got thoughts one way or
> another?

Well, a lot of times, I see myself needing just a simple list (list of
titles, list of id's to pass to in_bulk(), etc.)  If we just had a
function that returned tuples, then a list comprehension would still
have to be used to make a list from the singleton tuples when wanting a
for loop or for passing to in_bulk().

With adding both methods, we then have a method for returning each of
the main python data structures.  As for valuelist(), instead of being
able to only pass a single parameter, it might be cool to make a zip'ed
list of values, i.e.

>>> list(qset.valuelist("a","b"))

[a1, b1, a2, b2, ...]

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google