DL Zip
In [1]:
import urllib2, zipfile
In [7]:
url = 'http://ipython.rossant.net/'
filename = 'facebook.zip'
In [8]:
downloaded = urllib2.urlopen(url + filename)
In [9]:
folder = 'data'
In [10]:
mkdir $folder
In [11]:
cd $folder
In [12]:
cd ..
In [13]:
with open(filename, 'wb') as f:
f.write(downloaded.read())
In [14]:
with zipfile.ZipFile(filename) as zip:
zip.extractall('.')
In [15]:
cd $folder
In [18]:
ls
In [19]:
cd facebook/
In [20]:
ls
In [21]:
%bookmark fbdata
In [23]:
pwd
Out[23]:
In [24]:
cd ..
In [25]:
cd facebook/
In [26]:
with open('0.edges', 'r') as f:
print f.readline()
System shell:
In [32]:
cd fbdata
In [39]:
files = !ls -1 -S | grep edges
In [40]:
files
Out[40]:
In [41]:
!head -n5 {files[0]}
In [54]:
%alias largest ls -1shS | grep %s
In [55]:
largest circles
In [56]:
%store largest
History:
In [57]:
2 + 3
Out[57]:
In [58]:
_ * 2
Out[58]:
In [59]:
%hist -nop 1-2
In [62]:
cd ..
In [63]:
%run egos.py facebook
In [64]:
ids
Out[64]:
In [65]:
run egos.py
In [69]:
run -i egos.py
In [70]:
ids
Out[70]:
NetworkX
In [2]:
import networkx as nx
In [9]:
g = nx.read_edgelist('0.edges')
In [10]:
len(g.nodes()), len(g.edges())
Out[10]:
In [77]:
nx.radius(g)
In [11]:
sg = nx.connected_component_subgraphs(g)
In [12]:
[len(s) for s in sg]
Out[12]:
In [13]:
sgfirst = sg[0]
In [14]:
nx.radius(sgfirst), nx.diameter(sgfirst)
Out[14]:
In [85]:
class MyClass(object):
def __dir__(self):
return ['attr1', 'attr2']
In [86]:
obj = MyClass()
In [ ]:
obj.
In [89]:
??nx
In [94]:
%pfile egos.py
In [96]:
%timeit nx.center(sgfirst)
In [97]:
nx.center(sgfirst)
Out[97]:
Profiling a script:
In [104]:
%run -t center.py
In [105]:
%run -p center.py
In [15]:
run -t center2.py
In [108]:
%%file text.txt
Hello world!
In [110]:
%%!
more text.txt
Out[110]:
In [122]:
code = """
import networkx as nx
g = nx.read_edgelist('0.edges')
sg = nx.connected_component_subgraphs(g)[0]
ecc = nx.eccentricity(sg)
r = nx.radius(sg)
center = [node for node in sg.nodes() if ecc[node] == r]
print center
"""
f = open('code2.py', 'w')
f.write(code)
f.close()
In [123]:
run code2.py
In this example we'll learn how to:
In [17]:
nx.draw_networkx(sgfirst, node_size=15, edge_color='b', with_labels=False, alpha=.4, linewidths=0)
In [ ]: