##// END OF EJS Templates
simpler python2 compatibility for davinci code
stonebig <stonebig> -
Show More
@@ -17,6 +17,11 b' from IPython.parallel import Client, Reference'
17
17
18 from __future__ import division
18 from __future__ import division
19
19
20 try : #python2
21 from urllib import urlretrieve
22 except : #python3
23 from urllib.request import urlretrieve
24
20 davinci_url = "http://www.gutenberg.org/cache/epub/5000/pg5000.txt"
25 davinci_url = "http://www.gutenberg.org/cache/epub/5000/pg5000.txt"
21
26
22 def pwordfreq(view, fnames):
27 def pwordfreq(view, fnames):
@@ -47,11 +52,7 b" if __name__ == '__main__':"
47 if not os.path.exists('davinci.txt'):
52 if not os.path.exists('davinci.txt'):
48 # download from project gutenberg
53 # download from project gutenberg
49 print("Downloading Da Vinci's notebooks from Project Gutenberg")
54 print("Downloading Da Vinci's notebooks from Project Gutenberg")
50 try : #python2
55 urlretrieve(davinci_url, 'davinci.txt')
51 urllib.urlretrieve(davinci_url, 'davinci.txt')
52 except : #python3
53 import urllib.request
54 urllib.request.urlretrieve(davinci_url, 'davinci.txt')
55
56
56 # Run the serial version
57 # Run the serial version
57 print("Serial word frequency count:")
58 print("Serial word frequency count:")
@@ -60,7 +61,7 b" if __name__ == '__main__':"
60 freqs = wordfreq(text)
61 freqs = wordfreq(text)
61 toc = time.time()
62 toc = time.time()
62 print_wordfreq(freqs, 10)
63 print_wordfreq(freqs, 10)
63 print("Took %.3f s to calcluate"%(toc-tic))
64 print("Took %.3f s to calculate"%(toc-tic))
64
65
65
66
66 # The parallel version
67 # The parallel version
@@ -84,6 +85,6 b" if __name__ == '__main__':"
84 pfreqs = pwordfreq(view,fnames)
85 pfreqs = pwordfreq(view,fnames)
85 toc = time.time()
86 toc = time.time()
86 print_wordfreq(freqs)
87 print_wordfreq(freqs)
87 print("Took %.3f s to calcluate on %i engines"%(toc-tic, len(view.targets)))
88 print("Took %.3f s to calculate on %i engines"%(toc-tic, len(view.targets)))
88 # cleanup split files
89 # cleanup split files
89 map(os.remove, fnames)
90 map(os.remove, fnames)
General Comments 0
You need to be logged in to leave comments. Login now