views:

52

answers:

1

i;m trying to iterate through some values, and calculate a rank. i have a calculate_rank function where i calculate a sum of values. The problem is at the second function. I want that a user's rank to be the sum of all the user that in a follow relation with him. I am doing an iteration in the second function here where i try to add the rank of all the users that are in a follow relation with the user sent as a parameter. My problem is that the returned value is zero (0). I am sure i am mistaking in the second funtion, but i don;t see: where?

def calculate_rank(user):
 rank =  calculate_questions_vote(user) + calculate_votes(user) + calculate_replies(user)
 return rank


def calculate_followers_rank(user):
        follower = Relations.objects.filter(follow = user)
        follower_rank= 0
        for a in follower:
            follower_rank += calculate_rank(follower)
        return follower_rank

thank you!

+3  A: 

You're passing follower - ie the full list of followers - into the calculate_rank function. I think you either want a (the current follower in the iteration) or user (the original user being followed) here.

These things would be easier to spot if you gave your variables more accurate names. If you'd called the list of followers followers, in the plural, then you'd see it wouldn't make sense to pass it into calculate_rank.

Daniel Roseman
This looks like the problem. Additionally, having "Follower" objects stored in a model called "Relations" is confusing.
Paul McMillan
hmm... the problem is that if i'm passing 'a' (for calculating the sum of the ranks of the users following user 'user'), seems like my function still returns zero... i wonder why ?
dana
Possibly because the filter on Relations.objects is returning no results. But I'd hope you've already checked that. Or are you just planning on asking SO to build and now debug your entire webapp for you? </snippy>
stevejalim
nope, the query returns some results.:) i;m not planning to ask SO to build it for me, i;m just trying to learn and develop fast, and i know that finding the answers punctually is the best way to help me thinking the problem as i sholud have
dana