File: unicode.py

package info (click to toggle)
python-networkx 0.32-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,332 kB
  • ctags: 1,020
  • sloc: python: 21,197; makefile: 67; sh: 11
file content (69 lines) | stat: -rw-r--r-- 1,577 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Example using unicode strings as graph labels.  

Also shows creative use of the Heavy Metal Umlaut:
http://en.wikipedia.org/wiki/Heavy_metal_umlaut

"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
__date__ = ""
__credits__ = """"""
__revision__ = ""
#    Copyright (C) 2006 by 
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    Distributed under the terms of the GNU Lesser General Public License
#    http://www.gnu.org/copyleft/lesser.html

import codecs
import networkx as NX
import pylab as P


hd=u'H\u00fcsker D\u00fc'
mh=u'Mot\u00F6rhead'
mc=u'M\u00F6tley Cr\u00fce'
st=u'Sp\u0131n\u0308al Tap'
q=u'Queensr\u00ffche'
boc=u'Blue \u00d6yster Cult'
dt=u'Deatht\u00F6ngue'

G=NX.Graph()
G.add_edge(hd,mh)
G.add_edge(mc,st)
G.add_edge(boc,mc)
G.add_edge(boc,dt)
G.add_edge(st,dt)
G.add_edge(q,st)
G.add_edge(dt,mh)
G.add_edge(st,mh)

# write in UTF-8 encoding
fh=codecs.open('edgelist.utf-8','w',encoding='utf-8')
fh.write('# -*- coding: %s -*-\n'%fh.encoding) # encoding hint for emacs
NX.write_multiline_adjlist(G,fh,delimiter='\t')

# read and store in UTF-8
fh=codecs.open('edgelist.utf-8','r',encoding='utf-8')
H=NX.read_multiline_adjlist(fh,delimiter='\t')

for n in G.nodes():
    if n not in H:
        print False

print G.nodes()

try:
    pos=NX.spring_layout(G)
    NX.draw(G,pos,font_size=16,with_labels=False)
    for p in pos: # raise text positions
        pos[p][1]+=0.07
    NX.draw_networkx_labels(G,pos)
    P.show()
except:
    pass