forked from CalebCurry/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09-objects-io.py
More file actions
167 lines (124 loc) · 3.97 KB
/
09-objects-io.py
File metadata and controls
167 lines (124 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class Book():
favs = [] #class
def __init__(self, title, pages):
self.title = title
self.pages = pages
def is_short(self):
if self.pages < 100:
return true
#What happens when you pass object to print?
def __str__(self):
return f"{self.title}, {self.pages} pages long"
#What happens when you use ==?
def __eq__(self, other):
if(self.title == other.title and self.pages == other.pages):
return True
#It's approriate to give something for __hash__ when you override __eq__
# #This is the recommended way if mutable (like it is here):
__hash__ = None
########## Passing by Object Reference ##########
book = Book("Where Is My Mother?", 100)
print(book)
def modify(book):
book.title = "Changed noob"
modify(book)
print(book)
#What is we print id to follow?
def modify(book):
print(id(book))
book.title = "Changed noob"
print(id(book))
print(id(book))
modify(book)
print(id(book))
#We get the same number for everything
book = Book("Are You My Mother?", 100)
print("let's reassign")
def modify(book):
print(id(book))
book = Book("Changed noob", 100)
print(id(book)) #This is a different id.
#The original is unchanged. Reassigning
print(id(book))
modify(book)
print(id(book))
print(book) #Stayed the same!
########## Reading from a file ##########
file = open("input.txt", "a") #append. Creates if not there
file = open("input.txt", "w") #overwrites
#file = open("input.txt", "r+") #Read and write. Throws exception if not there
#not as easy to work with on r/w in my oppinion
#\t to sep title from page count
file.write("Are You My Mother?\t72\n")
file.write("The Digging-est Dog\t72")
file.close() #close when done
file = open("input.txt", "r")
#for line in file:
# print(line, end="") #end="" as \n is kept in line
#print()
#Numerous ways to read into list. Here is one.
data = file.read().split('\n')
print(data)
#https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list
file.close()
#Each line is title\tpages so we split it.
book1_data = data[0].split('\t')
book = Book(book1_data[0], book1_data[1])
book2_data = data[1].split('\t')
book2 = Book(book2_data[0], book2_data[1])
print(book)
print(book2)
########## Intro to Exception handling ##########
#When working with files (or doing anything in programming),
#Exceptions can be thrown
#file = open("doesntexist.csv", "r") #get wrecked son
try:
file = open("doesntexist.csv", "r")
except Exception as e:
print(type(e))
print(e)
try:
file = open("input.txt", "r") #Make sure it exists
data = int(file.read()) #This should fail
except FileNotFoundError as e: #subclass of OSError
print("This file is not found")
except OSError as e:
print("Couldn't open file")
except PermissionError as e:
print("file is locked")
except ValueError as e:
print("Cannot parse data. Check file")
except Exception as e:
print(type(e))
print(e)
finally:
file.close()
print("Always runs")
########## with keyword ##########
#shorthand for opening file and automatically closed outside indent
with open("input.txt", "r") as file: #Make sure it exists
try:
int(file.read())
except: #example of using thin except here
print("parse error...etc...")
#do whatever
print(file.closed) #closed
#opening can still throw an exception...Maybe do it like so:
try:
with open("no", "r") as file:
int(file.read())
except Exception as e:
print(e)
#I think this could be written like so but may require another try inside else:
try:
file = open('nope')
except OSError as e:
print(e)
else:
with file:
#try:
file.read()
#except Exception as e:
# print(e)
#https://stackoverflow.com/questions/40640956/python-with-open-except-filenotfounderror/40641103
#https://stackoverflow.com/questions/28633555/how-to-handle-filenotfounderror-when-try-except-ioerror-does-not-catch-it