Skip to content

Commit 7055dbb

Browse files
committed
Chapter 11
1 parent dac706a commit 7055dbb

32 files changed

Lines changed: 323 additions & 0 deletions

Ch10/error.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3: can't open file '/home/jason/Code/Repositories/DeadSimplePython/Ch10/print_error.py': [Errno 2] No such file or directory

Ch10/output.txt

Whitespace-only changes.

Ch11/213AnywhereAve.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Beautiful 3 bed, 2.5 bath on 2 acres.
2+
Finished basement, covered porch.
3+
Kitchen features granite countertops and new appliances.
4+
Large fenced yard with mature trees and garden space.
5+
$856,752

Ch11/78SomewhereRd.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
78 Somewhere Road, Anytown PA
2+
Tiny 2-bed, 1-bath bungalow. Needs repairs.
3+
Built in 1981; original kitchen and appliances.
4+
Small backyard with old storage shed.
5+
Built on ancient burial ground.
6+
$431,998

Ch11/78SomewhereRd_Original.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
78 Somewhere Road, Anytown PA
2+
Tiny 2-bed, 1-bath bungalow. Needs repairs.
3+
Built in 1981; original kitchen and appliances.
4+
Small backyard with old storage shed.
5+
Built on ancient burial ground.
6+
$431,998

Ch11/address_print.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
number = 245
2+
street = "8th Street"
3+
city = "San Francisco"
4+
state = "CA"
5+
zip_code = 94103
6+
7+
# print(f"{number} {street} {city} {state} {zip_code}")
8+
print(number, street, city, state, zip_code)

Ch11/check_stream_capabilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
with open("213AnywhereAve.txt", 'r') as file:
2+
print(file.readable()) # prints 'True'
3+
print(file.writable()) # prints 'False'
4+
print(file.seekable()) # prints 'True'

Ch11/error.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Scary error occurred

Ch11/house_showing.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class House:
2+
def __init__(self, address, house_key, **rooms):
3+
self.address = address
4+
self.__house_key = house_key
5+
self.__locked = True
6+
self._rooms = dict()
7+
for room, desc in rooms.items():
8+
self._rooms[room.replace("_", " ").lower()] = desc
9+
10+
def unlock_house(self, house_key):
11+
if self.__house_key == house_key:
12+
self.__locked = False
13+
print("House unlocked.")
14+
else:
15+
raise RuntimeError("Wrong key! Could not unlock house.")
16+
17+
def explore(self, room):
18+
if self.__locked:
19+
raise RuntimeError("Cannot explore a locked house.")
20+
21+
try:
22+
return f"The {room.lower()} is {self._rooms[room.lower()]}."
23+
except KeyError as e:
24+
raise KeyError(f"No room {room}") from e
25+
26+
def lock_house(self):
27+
self.__locked = True
28+
print("House locked!")
29+
30+
31+
class HouseShowing:
32+
33+
def __init__(self, house, house_key):
34+
self.house = house
35+
self.house_key = house_key
36+
37+
def __enter__(self):
38+
self.house.unlock_house(self.house_key)
39+
return self
40+
41+
def show(self, room):
42+
print(self.house.explore(room))
43+
44+
def __exit__(self, exc_type, exc_val, exc_tb):
45+
if exc_type:
46+
print("Sorry about that.")
47+
self.house.lock_house()
48+
49+
50+
house = House("123 Anywhere Street", house_key=1803,
51+
living_room="spacious",
52+
office="bright",
53+
bedroom="cozy",
54+
bathroom="small",
55+
kitchen="modern")
56+
57+
with HouseShowing(house, house_key=1803) as showing:
58+
showing.show("Living Room")
59+
showing.show("bedroom")
60+
# showing.show("porch")
61+
showing.show("kitchen")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from calendar import c
2+
3+
4+
# with open('78SomewhereRd.txt', 'r+') as real_estate_listing:
5+
# contents = real_estate_listing.read()
6+
7+
# contents = contents.replace('Tiny', 'Cozy')
8+
# contents = contents.replace('Needs repairs', 'Full of potential')
9+
# contents = contents.replace('Small', 'Compact')
10+
# contents = contents.replace('old storage shed', 'detached workshop')
11+
# contents = contents.replace('Built on ancient burial ground.',
12+
# 'Unique atmosphere.')
13+
# real_estate_listing.seek(0)
14+
# real_estate_listing.write(contents)
15+
# real_estate_listing.truncate()
16+
17+
with open('78SomewhereRd.txt', 'r+') as real_estate_listing:
18+
contents = real_estate_listing.readlines()
19+
new_contents = []
20+
for line in contents:
21+
line = line.replace('Tiny', 'Cozy')
22+
line = line.replace('Needs repairs', 'Full of potential')
23+
line = line.replace('Small', 'Compact')
24+
line = line.replace('old storage shed', 'detached workshop')
25+
line = line.replace('Built on ancient burial ground.',
26+
'Unique atmosphere.')
27+
new_contents.append(line)
28+
real_estate_listing.seek(0)
29+
real_estate_listing.writelines(new_contents)
30+
real_estate_listing.truncate()

0 commit comments

Comments
 (0)