Skip to content

Commit 3be3947

Browse files
committed
Modular-Programming-with-Python
Modular-Programming-with-Python Code
1 parent ac80343 commit 3be3947

File tree

99 files changed

+2756
-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.

99 files changed

+2756
-0
lines changed

chapter-1/animals/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Package initialization file.

chapter-1/animals/cat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# cat.py
2+
3+
def speak():
4+
print("meow")
5+

chapter-1/animals/cow.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# cow.py
2+
3+
def speak():
4+
print("moo")
5+

chapter-1/animals/dog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# dog.py
2+
3+
def speak():
4+
print("woof")
5+

chapter-1/animals/horse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# dog.py
2+
3+
def speak():
4+
print("neigh")
5+

chapter-1/animals/sheep.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# dog.py
2+
3+
def speak():
4+
print("baa")
5+

chapter-1/cache/cache.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# cache.py
2+
3+
import datetime
4+
5+
MAX_CACHE_SIZE = 100 # Maximum number of entries in the cache.
6+
7+
#############################################################################
8+
9+
def init():
10+
global _cache
11+
_cache = {} # Maps key to (timestamp, value) tuple.
12+
13+
14+
def set(key, value):
15+
global _cache
16+
if key not in _cache and len(_cache) >= MAX_CACHE_SIZE:
17+
_remove_oldest_entry()
18+
_cache[key] = [datetime.datetime.now(), value]
19+
20+
21+
def get(key):
22+
global _cache
23+
if key in _cache:
24+
_cache[key][0] = datetime.datetime.now()
25+
return _cache[key][1]
26+
else:
27+
return None
28+
29+
30+
def contains(key):
31+
global _cache
32+
return key in _cache
33+
34+
35+
def size():
36+
global _cache
37+
return len(_cache)
38+
39+
#############################################################################
40+
41+
# Private functions:
42+
43+
def _remove_oldest_entry():
44+
global _cache
45+
oldest = None
46+
for key in _cache.keys():
47+
if oldest == None:
48+
oldest = key
49+
elif _cache[key][0] < _cache[oldest][0]:
50+
oldest = key
51+
52+
if oldest != None:
53+
del _cache[oldest]
54+

chapter-1/cache/test_cache.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# test_cache.py
2+
3+
import random
4+
import string
5+
import cache
6+
7+
def random_string(length):
8+
s = ''
9+
for i in range(length):
10+
s = s + random.choice(string.ascii_letters)
11+
return s
12+
13+
cache.init()
14+
15+
for n in range(1000):
16+
while True:
17+
key = random_string(20)
18+
if cache.contains(key):
19+
continue
20+
else:
21+
break
22+
value = random_string(20)
23+
cache.set(key, value)
24+
print("After {} iterations, cache has {} entries".format(n+1, cache.size()))
25+

chapter-1/shaghetti/config.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Configuration file for the spaghetti.py program.
2+
3+
[config]
4+
5+
num_data_points=10
6+

chapter-1/shaghetti/spaghetti.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# spaghetti.py -- an example of bad coding.
2+
3+
import configparser
4+
5+
def load_config():
6+
config = configparser.ConfigParser()
7+
config.read("config.ini")
8+
return config['config']
9+
10+
def get_data_from_user():
11+
config = load_config()
12+
data = []
13+
for n in range(config.getint('num_data_points')):
14+
value = input("Data point {}: ".format(n+1))
15+
data.append(value)
16+
return data
17+
18+
def print_results(results):
19+
for value,num_times in results:
20+
print("{} = {}".format(value, num_times))
21+
22+
def analyze_data():
23+
data = get_data_from_user()
24+
results = {}
25+
for value in data:
26+
try:
27+
results[value] = results[value] + 1
28+
except KeyError:
29+
results[value] = 1
30+
return results
31+
32+
def sort_results(results):
33+
sorted_results = []
34+
for value in results.keys():
35+
sorted_results.append((value, results[value]))
36+
sorted_results.sort()
37+
return sorted_results
38+
39+
if __name__ == "__main__":
40+
results = analyze_data()
41+
sorted_results = sort_results(results)
42+
print_results(sorted_results)
43+

0 commit comments

Comments
 (0)