Skip to content

Commit 0f5e5b5

Browse files
committed
working on exceptions
1 parent bbdd532 commit 0f5e5b5

3 files changed

Lines changed: 95 additions & 7 deletions

File tree

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎python/08-oop-basics.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def is_short(self):
8989
if self.pages < 100:
9090
return true
9191

92-
book = Book("Are You My Mother?", 100)
92+
book = Book("Are You My Mother?", 72)
9393
book.log() #no need to pass anything to self parameter.
9494

9595
#A note on __init__
@@ -154,7 +154,7 @@ def is_short(self):
154154
return true
155155

156156

157-
book = Book("Are You My Mother?", 100)
157+
book = Book("Are You My Mother?", 72)
158158
book2 = Book("The Digging-est Dog", 72)
159159

160160
Book.favs.append(book)
@@ -201,9 +201,9 @@ def __hash__(self):
201201
return hash(self.title) ^ hash(self.pages) #xor with hash of attributes
202202
#from Mastering Object-Oriented Python
203203

204-
book = Book("Are You My Mother", 100)
204+
book = Book("Are You My Mother", 72)
205205
print(book)
206-
equal_book = Book("Are You My Mother", 100)
206+
equal_book = Book("Are You My Mother", 72)
207207
print("Are they considered equal?", book == equal_book) #yep
208208
print("Are they the same object?", book is equal_book) #nope
209209
book2 = Book("The Digging-est Dog", 72)

‎python/09-objects-io.py‎

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __eq__(self, other):
2121
#It's approriate to give something for __hash__ when you override __eq__
2222
# #This is the recommended way if mutable (like it is here):
2323
__hash__ = None
24-
24+
2525

2626
########## Passing by Object Reference ##########
2727

@@ -63,5 +63,93 @@ def modify(book):
6363

6464

6565
########## Reading from a file ##########
66-
########## Context manager (as?) ##########
67-
########## Intro to Exception handling ##########
66+
67+
file = open("input.txt", "a") #append. Creates if not there
68+
file = open("input.txt", "w") #overwrites
69+
#file = open("input.txt", "r+") #Read and write. Throws exception if not there
70+
#not as easy to work with on r/w in my oppinion
71+
72+
#\t to sep title from page count
73+
file.write("Are You My Mother?\t72\n")
74+
file.write("The Digging-est Dog\t72")
75+
file.close() #close when done
76+
77+
file = open("input.txt", "r")
78+
79+
#for line in file:
80+
# print(line, end="") #end="" as \n is kept in line
81+
#print()
82+
83+
#Numerous ways to read into list. Here is one.
84+
data = file.read().split('\n')
85+
print(data)
86+
#https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list
87+
88+
file.close()
89+
90+
#Each line is title\tpages so we split it.
91+
book1_data = data[0].split('\t')
92+
book = Book(book1_data[0], book1_data[1])
93+
94+
book2_data = data[1].split('\t')
95+
book2 = Book(book2_data[0], book2_data[1])
96+
97+
print(book)
98+
print(book2)
99+
100+
101+
########## Intro to Exception handling ##########
102+
103+
104+
#When working with files (or doing anything in programming),
105+
#Exceptions can be thrown
106+
107+
#file = open("doesntexist.csv", "r") #get wrecked son
108+
109+
try:
110+
file = open("doesntexist.csv", "r")
111+
except Exception as e:
112+
print(type(e))
113+
print(e)
114+
115+
try:
116+
file = open("input.txt", "r") #Make sure it exists
117+
data = int(file.read()) #This should fail
118+
except FileNotFoundError as e:
119+
print("This file is not found")
120+
except PermissionError as e:
121+
print("file is locked")
122+
except ValueError as e:
123+
print("Cannot parse data. Check file")
124+
except Exception as e:
125+
print(type(e))
126+
print(e)
127+
finally:
128+
file.close()
129+
print("Always runs")
130+
131+
132+
########## with keyword ##########
133+
134+
#shorthand for opening file and automatically closed outside indent
135+
with open("input.txt", "r") as file: #Make sure it exists
136+
137+
try:
138+
int(file.read())
139+
except: #example of using thin except here
140+
print("parse error...etc...")
141+
#do whatever
142+
143+
print(file.closed) #closed
144+
145+
#opening can still throw an exception...Maybe do it like so:
146+
try:
147+
with open("no", "r") as file: #Make sure it exists
148+
try:
149+
int(file.read())
150+
except:
151+
print("parse error...etc...")
152+
#do whatever
153+
154+
except Exception as e:
155+
print(e)

0 commit comments

Comments
 (0)