Skip to content

Commit 8d89cf8

Browse files
committed
Code Files Added
1 parent d8ccaa9 commit 8d89cf8

File tree

174 files changed

+7429
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+7429
-0
lines changed

Chapter02/Listings.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1. loop1.py
2+
2. loop2.py
3+
3. codecomments.py
4+
4. codecomments2.py
5+
5. codecomments3.py
6+
6. codecomments4.py
7+
7. codecomments5.py
8+
8. codecomments6.py
9+
9. codecomments7.py
10+
10. a1.py
11+
11. b1.py
12+
12. a2.py
13+
13. a_text.py
14+
14. b_text.py
15+
15. a_text2.py
16+
16. b_text2.py
17+
17. textrank.py
18+
18. urlrank.py
19+
19. rankbase.py
20+
20. textrank2.py
21+
21. urlrank2.py
22+
22. power.py
23+
23. factorial.py
24+
24. metrictest1.py
25+
25. metrictest2.py
26+
26. metrictest_fix1.py
27+
27. metrictest_fix2.py
28+
28. metrictest.py
29+
30+

Chapter02/Listings.txt~

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Listing # Filename
2+
3+
1 - loop1.py
4+
2 - loop2.py
5+
3 - codecomments.py
6+
4 - codecomments2.py
7+
5 - codecomments3.py
8+
6 - codecomments4.py
9+
7 - codecomments5.py
10+
8 - codecomments6.py
11+
9 - codecomments7.py
12+
10 - a1.py
13+
11 - b1.py
14+
12 - a2.py
15+
13 - a_text.py
16+
14 - b_text.py
17+
15 - a_text2.py
18+
16 - b_text2.py
19+
17 - textrank.py
20+
18 - urlrank.py
21+
19 - rankbase.py
22+
20 - textrank2.py
23+
21 - urlrank2.py
24+
22 - power.py
25+
23 - factorial.py
26+
24 - metrictest1.py
27+
25 - metrictest2.py
28+
26 - metrictest_fix1.py
29+
27 - metrictest_fix2.py
30+
28 - metrictest.py
31+
32+

Chapter02/a1.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Code listing #10
2+
3+
""" Module A (a.py) - Implement functions that operate on series of numbers """
4+
5+
# Note this is version 1 of a.py so called a1.py
6+
7+
def squares(narray):
8+
""" Return array of squares of numbers """
9+
return pow_n(array, 2)
10+
11+
def cubes(narray):
12+
""" Return array of cubes of numbers """
13+
return pow_n(narray, 3)
14+
15+
def pow_n(narray, n):
16+
""" Return array of numbers raised to arbitrary power n each """
17+
return [pow(x, n) for x in narray]
18+
19+
def frequency(string, word):
20+
""" Find the frequency of occurrences of word in string
21+
as percentage """
22+
23+
word_l = word.lower()
24+
string_l = string.lower()
25+
26+
# Words in string
27+
words = string_l.split()
28+
count = w.count(word_l)
29+
30+
# Return frequency as percentage
31+
return 100.0*count/len(words)
32+
33+

Chapter02/a2.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Code listing #12
2+
3+
""" Module A (a.py) - Implement functions that operate on series of numbers """
4+
5+
# Note - this is rewritten version of a1, so called a2.py
6+
7+
def squares(narray):
8+
""" Return array of squares of numbers """
9+
return pow_n(array, 2)
10+
11+
def cubes(narray):
12+
""" Return array of cubes of numbers """
13+
return pow_n(narray, 3)
14+
15+
def pow_n(narray, n):
16+
""" Return array of numbers raised to arbitrary power n each """
17+
return [pow(x, n) for x in narray]

Chapter02/a_text.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Code listing #13
2+
3+
""" Module A (a.py) - Provides string processing functions """
4+
5+
# Note - this is the text processing version of a, so called a_text.py
6+
7+
import b_text as b
8+
9+
def ntimes(string, char):
10+
""" Return number of times character 'char'
11+
occurs in string """
12+
13+
return string.count(char)
14+
15+
def common_words(text1, text2):
16+
""" Return common words across text1 and text2 """
17+
18+
# A text is a collection of strings split using newlines
19+
strings1 = text1.split(“\n”)
20+
strings2 = text2.split(“\n”)
21+
22+
common = []
23+
for string1 in strings1:
24+
for string2 in strings2:
25+
common += b.common(string1, string2)
26+
27+
# Drop duplicates
28+
return list(set(common))

Chapter02/a_text2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Code listing #15
2+
3+
""" Module A (a.py) - Provides string processing functions """
4+
5+
# Note - This is the rewritten version of a_text.py so called a_text2.py
6+
7+
def ntimes(string, char):
8+
""" Return number of times character 'char'
9+
occurs in string """
10+
11+
return string.count(char)
12+
13+
def common(string1, string2):
14+
""" Return common words across strings1 1 & 2 """
15+
16+
s1 = set(string1.lower().split())
17+
s2 = set(string2.lower().split())
18+
return s1.intersection(s2)
19+
20+
def common_words(text1, text2):
21+
""" Return common words across text1 and text2 """
22+
23+
# A text is a collection of strings split using newlines
24+
strings1 = text1.split("\n")
25+
strings2 = text2.split("\n")
26+
27+
common_w = []
28+
for string1 in strings1:
29+
for string2 in strings2:
30+
common_w += common(string1, string2)
31+
32+
return list(set(common_w))

Chapter02/b1.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Code listing #11
2+
3+
""" Module B (b.py) - Implement functions provide some statistical methods """
4+
5+
# Note - this is version 1 of b, so called b1.py
6+
7+
import a1 as a
8+
9+
def rms(narray):
10+
""" Return root mean square of array of numbers"""
11+
12+
return pow(sum(a.squares(narray)), 0.5)
13+
14+
def mean(array):
15+
""" Return mean of an array of numbers """
16+
17+
return 1.0*sum(array)/len(array)
18+
19+
def variance(array):
20+
""" Return variance of an array of numbers """
21+
22+
# Square of variation from mean
23+
avg = mean(array)
24+
array_d = [(x - avg) for x in array]
25+
variance = sum(a.squares(array_d))
26+
return variance
27+
28+
def standard_deviation(array):
29+
""" Return standard deviation of an array of numbers """
30+
31+
# S.D is square root of variance
32+
return pow(variance(array), 0.5)

Chapter02/b_text.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Code listing #14
2+
3+
""" Module B (b.py) - Provides text processing functions to user """
4+
5+
# Note: This is the text processing version of b so called b_text.py
6+
7+
import a_text as a
8+
9+
def common(string1, string2):
10+
""" Return common words across strings1 1 & 2 """
11+
12+
s1 = set(string1.lower().split())
13+
s2 = set(string2.lower().split())
14+
return s1.intersection(s2)
15+
16+
def common_words(filename1, filename2):
17+
""" Return common words across two input files """
18+
19+
lines1 = open(filename1).read()
20+
lines2 = open(filename2).read()
21+
22+
return a.common_words(lines1, lines2)

Chapter02/b_text2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Code listing #16
2+
3+
""" Module B (b.py) - Provides text processing functions to user """
4+
5+
# Note: This is the rewritten version of b_text.py so called b_text2.py
6+
7+
import a_text2 as a
8+
9+
def common_words(filename1, filename2):
10+
""" Return common words across two input files """
11+
12+
lines1 = open(filename1).read()
13+
lines2 = open(filename2).read()
14+
15+
return a.common_words(lines1, lines2)

Chapter02/codecomments.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Code listing #3
2+
3+
# This loop performs a network fetch of the URL, retrying upto 3
4+
# times in case of errors. In case the URL cant be fetched,
5+
# an error is returned.
6+
7+
# Initialize all state
8+
count, ntries, result, error = 0, 3, None, None
9+
while count < ntries:
10+
try:
11+
# NOTE: We are using an explicit timeout of 30s here
12+
result = requests.get(url, timeout=30)
13+
except Exception as error:
14+
print('Caught exception', error, 'trying again after a while')
15+
# increment count
16+
count += 1
17+
# sleep 1 second every time
18+
time.sleep(1)
19+
20+
if result == None:
21+
print("Error, could not fetch URL",url)
22+
# Return a tuple of (<return code>, <lasterror>)
23+
return (2, error)
24+
25+
# Return data of URL
26+
return result.content

Chapter02/codecomments2.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Code listing #4
2+
3+
def fetch_url(url, ntries=3, timeout=30):
4+
" Fetch a given url and return its contents "
5+
6+
# This loop performs a network fetch of the URL, retrying upto
7+
# 3 times in case of errors. In case the URL cant be fetched,
8+
# an error is returned.
9+
10+
# Initialize all state
11+
count, result, error = 0, None, None
12+
while count < ntries:
13+
try:
14+
result = requests.get(url, timeout=timeout)
15+
except Exception as error:
16+
print('Caught exception', error, 'trying again after a while')
17+
# increment count
18+
count += 1
19+
# sleep 1 second every time
20+
time.sleep(1)
21+
22+
if result == None:
23+
print("Error, could not fetch URL",url)
24+
# Return a tuple of (<return code>, <lasterror>)
25+
return (2, error)
26+
27+
# Return data of URL
28+
return result.content

Chapter02/codecomments3.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Code listing #5
2+
3+
def fetch_url(url, ntries=3, timeout=30):
4+
""" Fetch a given url and return its contents.
5+
6+
@params
7+
url - The URL to be fetched.
8+
ntries - The maximum number of retries.
9+
timeout - Timout per call in seconds.
10+
11+
@returns
12+
On success - Contents of URL.
13+
On failure - (error_code, last_error)
14+
"""
15+
16+
# This loop performs a network fetch of the URL, retrying upto
17+
# 'ntries' times in case of errors. In case the URL cant be
18+
# fetched, an error is returned.
19+
20+
# Initialize all state
21+
count, result, error = 0, None, None
22+
while count < ntries:
23+
try:
24+
result = requests.get(url, timeout=timeout)
25+
except Exception as error:
26+
print('Caught exception', error, 'trying again after a while')
27+
# increment count
28+
count += 1
29+
# sleep 1 second every time
30+
time.sleep(1)
31+
32+
if result == None:
33+
print("Error, could not fetch URL",url)
34+
# Return a tuple of (<return code>, <lasterror>)
35+
return (2, error)
36+
37+
# Return data of the URL
38+
return result.content

Chapter02/codecomments4.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Code listing #6
2+
3+
class UrlFetcher(object):
4+
""" Implements the steps of fetching a URL.
5+
6+
Main methods:
7+
fetch - Fetches the URL.
8+
get - Return the URLs data.
9+
"""
10+
11+
def __init__(self, url, timeout=30, ntries=3, headers={}):
12+
""" Initializer.
13+
@params
14+
url - URL to fetch.
15+
timeout - Timeout per connection (seconds).
16+
ntries - Max number of retries.
17+
headers - Optional request headers.
18+
"""
19+
self.url = url
20+
self.timeout = timeout
21+
self.ntries = retries
22+
self.headers = headers
23+
# Enapsulated result object
24+
self.result = result
25+
26+
def fetch(self):
27+
""" Fetch the URL and save the result """
28+
29+
# This loop performs a network fetch of the URL, retrying
30+
# upto 'ntries' times in case of errors.
31+
32+
count, result, error = 0, None, None
33+
while count < self.ntries:
34+
try:
35+
result = requests.get(self.url,
36+
timeout=self.timeout,
37+
headers = self.headers)
38+
except Exception as error:
39+
print('Caught exception', error, 'trying again after a while')
40+
# increment count
41+
count += 1
42+
# sleep 1 second every time
43+
time.sleep(1)
44+
45+
if result != None:
46+
# Save result
47+
self.result = result
48+
49+
def get(self):
50+
""" Return the data for the URL """
51+
52+
if self.result != None:
53+
return self.result.content
54+

0 commit comments

Comments
 (0)