Created
March 27, 2016 10:19
-
-
Save chribsen/081cf6244fedf5fa8e91 to your computer and use it in GitHub Desktop.
Gets the friend pairs from table derived_friend_list and finds their mutual friends. The mutual friend counts are found by taking the size of the intersection of their friend set.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import psycopg2 | |
| import psycopg2.extras | |
| from collections import defaultdict | |
| conn_dtu = psycopg2.connect(<connstring>) | |
| cur_dtu = conn_dtu.cursor(cursor_factory=psycopg2.extras.DictCursor) | |
| cur_dtu.execute("""SELECT user_a, user_b FROM derived_friend_list""") | |
| friend_pairs = tuple(cur_dtu.fetchall()) | |
| # Start by finding the friend set of each user | |
| friend_lookup = defaultdict(lambda: set()) | |
| for (user_a, user_b) in friend_pairs: | |
| friend_lookup[user_a].add(user_b) | |
| friend_lookup[user_b].add(user_a) | |
| for i, (user_a, user_b) in enumerate(friend_pairs): | |
| # Calculate mutual friend count | |
| mutual_friends = len(friend_lookup[user_a].intersection(friend_lookup[user_b])) | |
| cur_dtu.execute('update derived_friend_features set mutual_friend_count=%s WHERE user_a=%s AND user_b=%s', | |
| (mutual_friends, user_a, user_b,)) | |
| if i % 500 == 0: | |
| conn_dtu.commit() | |
| print('Reached count: {0} '.format(str(i))) | |
| print('{0} finished'.format(str((i+1) / len(friend_pairs)))) | |
| conn_dtu.commit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment