Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 14, 2010 21:17:26

Lee H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

len() vs count


This started off as a pagination question, but then I realized it was
a different issue:

Given:http://dpaste.com/hold/207217/why would len(n) be different than n.count()

In my case, the len result is correct, but Paginator tries count first
(and uses that value).

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 15, 2010 05:17:09

Harley B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

len() vs count


count() does a sql count statement, while len() loads all rows from
the database and returns the array length of the results.
count() is much faster and better to use if you can conditionally
avoid loading all results.

On Jun 15, 6:17 am, Lee Hinde <leehi...@gmail.com> wrote:
> This started off as a pagination question, but then I realized it was
> a different issue:
>
> Given:http://dpaste.com/hold/207217/>
> why would  len(n)  be different  than n.count()
>
> In my case, the len result is correct, but Paginator tries count first
> (and uses that value).

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 15, 2010 06:00:45

Lee H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

len() vs count


Sorry, I wasn't clear. If I call 'len' (line 5),  the number of
records returned by "paged_notes.paginator.count" process is correct.
If I don't call len, the number returned is off.

On Mon, Jun 14, 2010 at 9:17 PM, Harley Bussell <mod...@gmail.com> wrote:
>
> count() does a sql count statement, while len() loads all rows from
> the database and returns the array length of the results.
> count() is much faster and better to use if you can conditionally
> avoid loading all results.
>
> On Jun 15, 6:17 am, Lee Hinde <leehi...@gmail.com> wrote:
> > This started off as a pagination question, but then I realized it was
> > a different issue:
> >
> > Given:http://dpaste.com/hold/207217/> >
> > why would  len(n)  be different  than n.count()
> >
> > In my case, the len result is correct, but Paginator tries count first
> > (and uses that value).
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 15, 2010 15:56:46

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

len() vs count


On 14 juin, 22:17, Lee Hinde <leehi...@gmail.com> wrote:
> This started off as a pagination question, but then I realized it was
> a different issue:
>
> Given:http://dpaste.com/hold/207217/u = notes.filter(Q(recipient__user = id) |
Q(store_as_text="All")).distinct()
u = notes.filter(recipient__user = id)

OT but - You're aware that the second statements discards all results
from the first one ?

n = u.select_related().order_by('-target_date','-id')
agg = _aggregation(n,request)

What is '_aggregation' ?

# print(len(n))
paginator = Paginator(n,10)

try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1

try:
paged_notes = paginator.page(page)
except (EmptyPage, InvalidPage):
paged_notes = paginator.page(paginator.num_pages) #last page.

print paged_notes.paginator.count

> why would  len(n)  be different  than n.count()

cf my question above.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 15, 2010 18:39:10

Lee H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

len() vs count


On Tue, Jun 15, 2010 at 7:56 AM, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 14 juin, 22:17, Lee Hinde <leehi...@gmail.com> wrote:
>> This started off as a pagination question, but then I realized it was
>> a different issue:
>>
>> Given:http://dpaste.com/hold/207217/>
>
>    u = notes.filter(Q(recipient__user = id) |
> Q(store_as_text="All")).distinct()
>    u = notes.filter(recipient__user = id)
>
> OT but - You're aware that the second statements discards all results
> from the first one ?

I added the 2nd filter to see if the distinct() made a difference in
the outcome. I should have commented the first one out.

>    n = u.select_related().order_by('-target_date','-id')
>    agg = _aggregation(n,request)
>
> What is '_aggregation' ?

_aggregation creates a 2nd queryset

def _aggregation(notes,request):
if _is_manager_or_above(request):
agg = notes.aggregate(Sum('lunch'),Sum('dinner'),Sum('catering'))
else:
agg = {}
agg = Decimal(0)
agg = Decimal(0)
agg = Decimal(0)
return agg



> #    print(len(n))
>    paginator = Paginator(n,10)
>
>    try:
>        page = int(request.GET.get('page', '1'))
>    except ValueError:
>        page = 1
>
>    try:
>        paged_notes = paginator.page(page)
>    except (EmptyPage, InvalidPage):
>        paged_notes = paginator.page(paginator.num_pages) #last page.
>
>    print paged_notes.paginator.count
>
>> why would  len(n)  be different  than n.count()
>
> cf my question above.
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 14th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support