Skip to content

Commit 842802b

Browse files
committed
Chapter 13
1 parent d8db855 commit 842802b

5 files changed

Lines changed: 170 additions & 0 deletions

File tree

Ch13/bullet_journal.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from calendar import month
2+
3+
4+
class Collection:
5+
6+
def __init__(self, title, page_start, length=1):
7+
self.title = title
8+
self.page_start = page_start
9+
self.page_end = page_start + length - 1
10+
self.item = []
11+
12+
def __str__(self):
13+
return self.title
14+
15+
def expand(self, by):
16+
self.page_end += by
17+
18+
def add_item(self, bullet, note, signifier=None):
19+
"""Adds an item to the monthly log."""
20+
21+
22+
class MonthlyLog(Collection):
23+
24+
def __init__(self, month, year, page_start, length=2):
25+
super().__init__(f"{month} {year}", page_start, length)
26+
self.events = []
27+
28+
def __str__(self):
29+
return f"{self.title} (Monthly Log)"
30+
31+
def add_event(self, event, date=None):
32+
"""Logs an event for the given date (today by default)."""
33+
34+
35+
class FutureLog(Collection):
36+
37+
def __init__(self, start_month, page_start):
38+
super().__init__("Future Log", page_start, 4)
39+
self.start = start_month
40+
self.months = [start_month] # TODO: Add other five months.
41+
42+
def add_item(self, bullet, note, signifier=None, month=None):
43+
"""Adds an item to the future log for the given month."""
44+
45+
46+
log = FutureLog('May 2023', 5)
47+
log.add_item('June 2023', '.', 'Clean mechanical keyboard')
48+
print(log) # prints "Future Log"
49+
50+
monthly = MonthlyLog('April', '2023', 9)
51+
monthly.add_event('Finally learned Python inheritance!')
52+
monthly.add_item('.', 'Email Ben re: coffee meeting')
53+
print(monthly) # prints "April 2023 (Monthly Log)"
54+
55+
to_read = Collection("Books to Read", 17)
56+
to_read.add_item('.', 'Anne of Avonlea')
57+
print(to_read) # prints "Books to Read"

Ch13/calzone.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Food:
2+
def __str__(self):
3+
return "Yum, what is it?"
4+
5+
6+
class Pizza(Food):
7+
def __str__(self):
8+
return "Piiiizzaaaaaa"
9+
10+
11+
class Sandwich(Food):
12+
def __str__(self):
13+
return "Mmm, sammich."
14+
15+
16+
class Calzone(Pizza, Sandwich):
17+
pass
18+
19+
20+
calzone = Calzone()
21+
print(calzone) # what gets printed??
22+
23+
24+
# class PizzaSandwich(Sandwich, Pizza):
25+
class PizzaSandwich(Pizza, Sandwich):
26+
pass
27+
28+
29+
class CalzonePizzaSandwich(Calzone, PizzaSandwich):
30+
def __str__(self):
31+
return Calzone.__str__(self)

Ch13/livesettings.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[MAGIC]
2+
UserName = Bob
3+
MagicNumber = 42

Ch13/make_calzone.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Food:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
6+
class Pizza(Food):
7+
def __init__(self, toppings, name="Pizza", **kwargs):
8+
super().__init__(name=name, **kwargs)
9+
self.toppings = toppings
10+
11+
12+
class Sandwich(Food):
13+
def __init__(self, bread, fillings, name="Sandwich", **kwargs):
14+
super().__init__(name=name, **kwargs)
15+
self.bread = bread
16+
self.fillings = fillings
17+
18+
19+
class Calzone(Pizza, Sandwich):
20+
def __init__(self, toppings):
21+
super().__init__(
22+
toppings=toppings,
23+
bread='pizza crust',
24+
fillings=toppings,
25+
name='Calzone'
26+
)
27+
28+
29+
# The usage...
30+
pizza = Pizza(toppings="pepperoni")
31+
sandwich = Sandwich(bread="rye", fillings="swiss")
32+
calzone = Calzone("sausage")

Ch13/mixins.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import configparser
2+
from pathlib import Path
3+
4+
5+
class SettingsFileMixin:
6+
7+
settings_path = Path('livesettings.ini')
8+
config = configparser.ConfigParser()
9+
10+
def read_setting(self, key):
11+
self.config.read(self.settings_path)
12+
try:
13+
return self.config[self.settings_section][key]
14+
except KeyError:
15+
raise KeyError("invalid section in settings file.")
16+
17+
18+
class Greeter(SettingsFileMixin):
19+
20+
def __init__(self, greeting):
21+
self.settings_section = 'MAGIC'
22+
self.greeting = greeting
23+
24+
def __str__(self):
25+
try:
26+
name = self.read_setting('UserName')
27+
except KeyError:
28+
name = "user"
29+
return f"{self.greeting} {name}!"
30+
31+
32+
class MagicNumberPrinter(SettingsFileMixin):
33+
34+
def __init__(self, greeting):
35+
self.settings_section = 'MAGIC'
36+
37+
def __str__(self):
38+
try:
39+
magic_number = self.read_setting('MagicNumber')
40+
except KeyError:
41+
magic_number = "unknown"
42+
return f"The magic number is {magic_number}!"
43+
44+
45+
greeter = Greeter("Salutations,")
46+
for i in range(100000):
47+
print(greeter)

0 commit comments

Comments
 (0)