views:

106

answers:

5

if my views code is:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)

what is the argument that will limit the result to 50 tags?

I'm assuming this:

.... limit=50)

is incorrect.

more complete code follows:

videoarttags = Media.objects.order_by('date_added'),filter(topic__exact='art') 
audioarttags = Audio.objects.order_by('date_added'),filter(topic__exact='art') 
conarttags = Concert.objects.order_by('date_added'),filter(topic__exact='art') 
arttags = list(chain(videoarttags, audioarttags, conarttags)) 
arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)

how do incorporate –

itertools.islice(sorted(...),50)
+1  A: 

You'll probably find that a slice works for you:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)[:50]
Jarret Hardie
yeah, I thought so too, but it had no effect.
In what way did this not work? You mention itertools, if you want an iterator, try `itertools.islice(sorted(...),50)`
Markus
the code looks like this:videoarttags = Media.objects.order_by('date_added'),filter(topic__exact='art')audioarttags = Audio.objects.order_by('date_added'),filter(topic__exact='art')conarttags = Concert.objects.order_by('date_added'),filter(topic__exact='art')arttags = list(chain(videoarttags, audioarttags, conarttags))arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)how do incorporate – itertools.islice(sorted(...),50)
@kjarsenal, pls edit your question to put this code there (it's unreadable all flowed in a comment) and to fix the question's title (as itertools has nothing to do with sorted).
Alex Martelli
sorry about that. question has been edited.
A: 

The general idea of what you want is a take, I believe. From the itertools documentation:

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))
Mark Rushakoff
can't figure out how to implement it in the django view using the module.
+2  A: 

what about heapq.nlargest:
Return a list with the n largest elements from the dataset defined by iterable.key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in the iterable: key=str.lower Equivalent to: sorted(iterable, key=key, reverse=True)[:n]

>>> from heapq import nlargest
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> nlargest(3, data)
[9, 8, 7]
sunqiang
This is probably the fastest way of getting only the first few sorted elements. That's exactly what I would have suggested.
EOL
A: 

I think I was pretty much barking up the wrong tree. What I was trying to accomplish was actually very simple using a template filter (slice) which I didn't know I could do. The code was as follows:

{% for arttag in arttags|slice:":50" %}

Yes, I feel pretty stupid, but I'm glad I got it done :-)

A: 

You might also want to add [:50] to each of the objects.order_by.filter calls. Doing that will mean you only ever have to sort 150 items in-memory in Python instead of possibly many more.

Steve Losh
excellent point. thanks steve; will implement.