def topological_sort_chains(chains): incidence = dict() G = create_graph(chains, incidence) assert len(G) == len(incidence) color = {} pred = {} disc = {} fin = {} DFS(G, color, pred, disc, fin) fin2 = dictreverse(fin) fin3 = [] for i in sorted(fin2, reverse=True): fin3.append(fin2[i]) return fin3
def get_ordered(self, new=()): if self.order is None: return order = self.order acct_position = dict((v,k) for (k,v) in enumerate(order)) from collections import defaultdict types = defaultdict(list) provider_list = self.existing_sps.values() provider_lookup = dict() account_lookup = dict() for provider in provider_list: for type_, acct in provider.accounts.items(): if acct in new: assert len(new) == 1 assert len(provider.accounts) > len(new) continue types[type_].append(acct) provider_lookup[acct.id] = provider account_lookup[acct.id] = acct for val in types.values(): val.sort(key = lambda a: acct_position.get(a.id, 1000)) loc = dict(enumerate(provider_list)) from util.primitives.mapping import dictreverse loc2 = dictreverse(loc) chains = [types['im'], types['email'], types['social']] #this adds just a little more information about the relationship between #im/email/social accounts. total_chain = oset(account_lookup[id_] for id_ in order if id_ in account_lookup) for chain in chains: total_chain.update(chain) chains.append(total_chain) chains = [oset([loc2[provider_lookup[acct.id]] for acct in type_]) for type_ in chains] #enforce that if there is a previous ordering between two nodes, #then the reverse ordering is discarded # partial = set() # chains2 = [] # for chain in chains: # chain2 = [] # blacklist = [] # for i, a in enumerate(chain): # if a in blacklist: # continue # for b in chain[i:]: # if a == b: # continue # node = (a,b) # revnode = (b,a) # if revnode in partial: # #the conflict doesn't exist until we get to b # #and it's farther down than this one, so discard b. # _none = blacklist.append(b) # else: # _none = partial.add(node) # else: # _none = chain2.append(a) # _none = chains2.append(chain2) # #_none is for pasting into the console, consumed return values aren't shown. # import util.primitives.topological_sort as tsort # order = tsort.topological_sort_chains(chains2) import util.primitives.topo_sort as tsort order = tsort.topological_sort_chains(chains) provider_accts = [loc[i] for i in order] out = [] for prov in provider_accts: [out.append(a.id) for a in [prov.accounts.get(t, None) for t in ['im', 'email', 'social']] if a is not None] self.order = out return provider_accts
FORCED_APPS = { 'News Feed': 'nf', 'Status Updates': 'app_2915120374', 'Photos': 'app_2305272732', 'Links': 'app_2309869772' } FORCED_KEYS = { '__notification__': { 'name': 'Notifications', 'icon_url': 'facebookicons.notifications_icon' } } KNOWN_APPS_LOOKUP = mapping.dictreverse(FORCED_APPS) #COMMENTS_QUERY = "SELECT fromid, text, time, post_id, id FROM comment WHERE post_id IN (SELECT post_id FROM #posts)" PROFILES_QUERY = """SELECT id, name, pic_square, url FROM profile WHERE id IN (SELECT viewer_id FROM #posts) OR id IN(SELECT actor_id FROM #posts) OR id in (SELECT target_id FROM #posts) OR id in (SELECT source_id FROM #posts) OR id IN (SELECT likes.sample FROM #posts) OR id IN (SELECT likes.friends FROM #posts) OR id IN (SELECT sender_id FROM #notifications)""" #ALL_POSTS_QUERY = 'SELECT post_id, comments, permalink, created_time, updated_time, viewer_id, actor_id, target_id, source_id, message, attachment, action_links, likes FROM stream where filter_key="nf" and is_hidden=0 LIMIT 100' BIRTHDAY_QUERY = 'select name, birthday_date, profile_url, uid from user where uid IN (select uid2 from friend where uid1=%d)' NOW_QUERY = 'select now() from user where uid=%d' EVENTS_QUERY = 'select eid from event where eid in (select eid from event_member where uid=me() and rsvp_status="not_replied") and start_time > now()' STATUS_QUERY = 'select message, status_id, time, uid from status where uid=me() limit 1' NOTIFICATIONS_QUERY = 'select notification_id,sender_id,created_time,updated_time,title_html,title_text,href,is_unread,app_id from notification where recipient_id=me()' APP_QUERY = 'SELECT app_id,icon_url FROM application WHERE app_id IN (SELECT app_id from #notifications)' POST_FILTER_KEY_QUERY = "select post_id, filter_key from stream where post_id in (select post_id from #latest_posts) and filter_key in (select filter_key from #filter_keys)" FILTER_KEY_QUERY = "select filter_key, name, icon_url from stream_filter where uid=me() and ((is_visible=1 and type='application') or filter_key in ('" + "', '".join( FORCED_APPS.values()) + "')) ORDER BY rank ASC"
import graphapi log = getLogger("Facebook2.0") POSTS_LIMIT = 100 FORCED_APPS = {'News Feed': 'nf', 'Status Updates': 'app_2915120374', 'Photos': 'app_2305272732', 'Links': 'app_2309869772'} FORCED_KEYS = { '__notification__':{'name':'Notifications', 'icon_url':'facebookicons.notifications_icon'} } KNOWN_APPS_LOOKUP = mapping.dictreverse(FORCED_APPS) #COMMENTS_QUERY = "SELECT fromid, text, time, post_id, id FROM comment WHERE post_id IN (SELECT post_id FROM #posts)" PROFILES_QUERY = """SELECT id, name, pic_square, url FROM profile WHERE id IN (SELECT viewer_id FROM #posts) OR id IN(SELECT actor_id FROM #posts) OR id in (SELECT target_id FROM #posts) OR id in (SELECT source_id FROM #posts) OR id IN (SELECT likes.sample FROM #posts) OR id IN (SELECT likes.friends FROM #posts) OR id IN (SELECT sender_id FROM #notifications)""" #ALL_POSTS_QUERY = 'SELECT post_id, comments, permalink, created_time, updated_time, viewer_id, actor_id, target_id, source_id, message, attachment, action_links, likes FROM stream where filter_key="nf" and is_hidden=0 LIMIT 100' BIRTHDAY_QUERY = 'select name, birthday_date, profile_url, uid from user where uid IN (select uid2 from friend where uid1=%d)' NOW_QUERY = 'select now() from user where uid=%d' EVENTS_QUERY = 'select eid from event where eid in (select eid from event_member where uid=me() and rsvp_status="not_replied") and start_time > now()' STATUS_QUERY = 'select message, status_id, time, uid from status where uid=me() limit 1' NOTIFICATIONS_QUERY = 'select notification_id,sender_id,created_time,updated_time,title_html,title_text,href,is_unread,app_id from notification where recipient_id=me()' APP_QUERY = 'SELECT app_id,icon_url FROM application WHERE app_id IN (SELECT app_id from #notifications)' POST_FILTER_KEY_QUERY = "select post_id, filter_key from stream where post_id in (select post_id from #latest_posts) and filter_key in (select filter_key from #filter_keys)" FILTER_KEY_QUERY = "select filter_key, name, icon_url from stream_filter where uid=me() and ((is_visible=1 and type='application') or filter_key in ('" + "', '".join(FORCED_APPS.values()) + "')) ORDER BY rank ASC" POST_QUERY = 'SELECT post_id, comments, permalink, created_time, updated_time, viewer_id, actor_id, target_id, source_id, message, attachment, action_links, likes FROM stream where post_id="%s"'
G = create_graph(chains, incidence) assert len(G) == len(incidence) color = {} pred = {} disc = {} fin = {} DFS(G, color, pred, disc, fin) fin2 = dictreverse(fin) fin3 = [] for i in sorted(fin2, reverse=True): fin3.append(fin2[i]) return fin3 if __name__ == '__main__': incidence = dict() G = create_graph([ [6, 0, 5], [5, 6, 9, 3, 8, 7, 4, 2], [1, 0], [5, 6, 1, 0, 9, 3, 8, 7, 4, 2]], incidence) assert len(G) == len(incidence) color = {} pred = {} disc = {} fin = {} DFS(G, color, pred, disc, fin) from mapping import dictreverse fin2 = dictreverse(fin) fin3 = [] for i in sorted(fin2, reverse=True): fin3.append(fin2[i]) print fin3 # print incidence