Skip to content

Instantly share code, notes, and snippets.

@gusy
Created June 4, 2012 01:13
Show Gist options
  • Save gusy/2865734 to your computer and use it in GitHub Desktop.
Save gusy/2865734 to your computer and use it in GitHub Desktop.
Mono-core python dict based script
import sys,codecs
'''
format emiter receiver contacts
example: 5767599 242682935 4
'''
links={}
i=0
f=file("/dev/stdin")
for line in f:
i+=1
#print line
line=line.split(";");
emiter=int(line[0])
reciever=int(line[1])
contacts=int(line[2])
l=sorted([emiter,reciever])
#rel_key='-'.join(map(str,l))
rel_key=(l[0],l[1])
di=0 if caller<callee else 1
if not rel_key in links:
links[rel_key]=[0,0]
links[rel_key][di]+=contacts
if i%1000000==0:
print "read %i million contacts"%(i/1000000)
print len(links),"relationships in dict"
f.close()
f=codecs.open("network-mutual-graph","w")
f2=codecs.open("network-directed-graph","w")
for link,contacs in links.iteritems():
link=link.split('-')
if contacts[0]==0:
f2.write("%s %s %i \n"%(link[1],link[0],contacts[1]))
elif contacts[1]==0:
f2.write("%s %s %i \n"%(link[0],link[1],contacts[0]))
else:
f.write("%s %s %i %i \n"%(link[0],link[1],contacts[0],contacts[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment