Created
March 21, 2016 13:17
-
-
Save chribsen/a316304939d17c5c8096 to your computer and use it in GitHub Desktop.
This file contains 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 | |
from itertools import groupby | |
conn_dtu = psycopg2.connect("<connstring>") | |
cur_dtu = conn_dtu.cursor() | |
cur_dtu.execute("select user_a, user_b, meeting_array from temp_friend_meetings where met_consecutive_days is null") | |
for idx, each in enumerate(cur_dtu.fetchall()): | |
meeting_vector = each[2] | |
grouped_L = [(k, sum(1 for i in g)) for k,g in groupby(meeting_vector)] | |
sorted_group = sorted(grouped_L, key=lambda x: x[1], reverse=True)[0] | |
cur_dtu.execute(""" UPDATE temp_friend_meetings SET met_consecutive_days=%s | |
WHERE user_a=%s AND user_b=%s""", | |
(int(sorted_group[1]), | |
each[0], | |
each[1],) | |
) | |
print('user pair: ' + str(idx)) | |
if idx % 1000 == 0: | |
print('saving..') | |
conn_dtu.commit() | |
print('saved') | |
conn_dtu.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment