Basic Match Making for Django and Python -
so have model education
attached userprofile
. model works 1 assumes: user
has gone several schools in different areas.
what i'm trying create "score" depending on users went. basically, if went same school, 10 points, same city, 5 points, same state, 2 points, , on.
i've made functions effort have failed. advice?
info = {} def edu_info(user1): user_1_cities = [] user_1_schools = [] user_1_state = [] first_one = education.objects.filter(owner=user1) in first_one: user_1_cities.append(str(i.city)) user_1_schools.append(str(i.school)) user_1_state.append(str(i.state)) info[str(i.owner.username)] = {} info[str(i.owner.username)]['cities'] = user_1_cities info[str(i.owner.username)]['schools'] = user_1_schools info[str(i.owner.username)]['state'] = user_1_state homecoming info def check_match(user1, user2): match_score = {} first_info = edu_info(user1) dict = edu_info(user2) item in dict: cities = dict[item]['cities'] #user2 = item #print cities city in cities: if city in first_info['jmitchel3']['cities']: match_score['user'] = 'jmitchel3' match_score['user2'] = str(user2.user.username) match_score['city'] = city print "here! " + str(city) else: print "not here! " + str(city) homecoming match_score check_match(j,t)
how this?
start function collects info single user, , returns single dictionary user:
def edu_info(user1): user_1_cities = [] user_1_schools = [] user_1_state = [] first_one = education.objects.filter(owner=user1) in first_one: user_1_cities.append(str(i.city)) user_1_schools.append(str(i.school)) user_1_state.append(str(i.state)) info = {} info['cities'] = user_1_cities info['schools'] = user_1_schools info['states'] = user_1_state homecoming info
then, have separate function calls info collecting function 1 time each of 2 users, , computing number of points information.
def check_match(user_1, user_2) info_1 = edu_info(user_1) info_2 = edu_info(user_2) school_points = similarity_points(info_1["schools"], info_2["schools"], 10) city_points = similarity_points(info_1["cities"], info_2["cities"], 5) state_points = similarity_points(info_1["states"], info_2["states"], 2) homecoming school_points + city_points + state_points
the meat of check_match
function offloaded it's own helper function. function finds intersection of 2 lists of attributes, , multiplies number of shared attributes point value. converting attribute lists sets , using set intersection operator.
so, similarity_points(["mn","or","pa", "nj"],["az","nj","pa"], 2)
find 2 matches (nj , pa) , homecoming 4.
def similarity_points(attr_1, attr_2, points) """award number of points each shared attribute. attr_1 , attr_2 should lists compare. """ number_shared = len(set(attr_1) & set(attr_2)) homecoming number_shared * points
then, can phone call above code like:
wilduck_jmitchel3_points = check_match("wilduck", "jmitchel3")
python django
No comments:
Post a Comment