On 23/08/2010 11:17am, John Yeukhon Wong wrote:
Hi, I am confused with this piece of code. This is a code responsible
for building a tag cloud. It lives in the views.py
The part I don't understand is
# Calculate tag, min and max counts.
min_count = max_count = tags[0].bookmarks.count()
I never had used this 3 assignments in Python before. I also don't get
why it's tags[0]....
x = y = z = 3
is the same in Python as
z = 3
y = z
x = y
tags[0] is the first element of the tag list. In Python, lists and other
iterables are zero-based.
hth
According to the book, it is iterating through the tag lists.....
I really can't follow the logic there, and this also prevents me from
understanding the rest of the for loop....
Thank you for any input!
//code begins
def tag_cloud_page(request):
MAX_WEIGHT = 5
tags = Tag.objects.order_by('name')
# Calculate tag, min and max counts.
min_count = max_count = tags[0].bookmarks.count()
for tag in tags:
tag.count = tag.bookmarks.count()
if tag.count< min_count:
min_count = tag.count
if max_count< tag.count:
max_count = tag.count
# Calculate count range. Avoid dividing by zero.
range = float(max_count - min_count)
if range == 0.0:
range = 1.0
# Calculate tag weights.
for tag in tags:
tag.weight = int( MAX_WEIGHT * (tag.count - min_count) /
range)
variables = RequestContext(request, { 'tags': tags })
return render_to_response('tag_cloud_page.html', variables)
// code ends
--
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 at
http://groups.google.com/group/django-users?hl=en.