class Light:
def __init__(self):
self._status = False
def swithon(self):
self._status = True
def switchoff(self):
self._status = False
def __repr__(self):
s = "on" if self._status else "off"
return f"Light<{s}>"
l1 = Light()
l1
Light<off>
print(l1)
Light<off>
l1.swithon()
l1
Light<on>
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"{self.x},{self.y}"
import math
class Vector:
def __init__(self, point1, point2):
self.start = point1 # this is instance of point passed
self.end = point2
def magnitude(self):
xoffset = self.end.x - self.start.x
yoffset = self.end.y - self.start.y
return math.sqrt(xoffset**2+yoffset**2)
def __add__(self, vector2):
p1 = Point(self.start.x + vector2.start.x, self.start.y + vector2.start.y)
p2 = Point(self.end.x + vector2.end.x, self.end.y + vector2.end.y)
return Vector(p1, p2)
def __repr__(self):
return f"Vector<{self.magnitude()}>"
s = "hello"
s + "world"
'helloworld'
v1 = Vector(Point(0,0), Point(0, 1))
v1
Vector<1.0>
v2 = Vector(Point(0,0), Point(0, 1))
v1 + v2
Vector<2.0>
v3 = Vector(Point(0,0), Point(1,0))
v1 + v3
Vector<1.4142135623730951>
x = 4
"*" * 5
'*****'
5 * "*"
'*****'
class Light:
def __init__(self):
self.status = False
def switchon(self):
self.status = True
def switchoff(self):
self.status = False
def __repr__(self):
s = "on" if self.status else "off"
return f"Light<{s}>"
class YellowLight(Light): # Light is parent class, YellowLight is child class
color = "Yellow"
y1 = YellowLight()
y1
Light<off>
y1.color
'Yellow'
y1.color
'Yellow'
y1.status
False
y1.switchon()
y1.color
'Yellow'
y1.status
True
y1
Light<on>
y2 = YellowLight()
y2.color
'Yellow'
y2.status
False
y2
Light<off>
y1
Light<on>
YellowLight.color = "crimson_yellow"
y1.color
'crimson_yellow'
y2.color
'crimson_yellow'
class EbookReader:
def __init__(self, filename):
with open(filename) as f:
self._lines = f.readlines()
self._pagesize = 20
self._position = 0
def get_next_page(self):
if self._position < len(self._lines):
page = self._lines[self._position:self._position+self._pagesize]
self._position += self._pagesize
return "".join(page)
return ""
def read_next_page(self):
print(self.get_next_page())
def go_to(self, linenum):
if linenum >=0 and linenum < len(self._lines):
self._position = linenum
def go_to_start(self):
self.go_to(0)
with open("poem.txt") as f:
contents = f.read()
with open("sample_book.txt", "w") as f:
for i in range(30):
f.write(contents)
ebook_reader = EbookReader("sample_book.txt")
ebook_reader.read_next_page() #
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea.
ebook_reader.read_next_page()
Namespaces are one honking great idea -- let's do more of those! The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea.
ebook_reader.read_next_page()
If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now.
ebook_reader.go_to(15)
ebook_reader.read_next_page()
Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess.
Problem
Timer which can be used to time any computation.timer = Timer()
timer.start()
s = 0
for i in range(10000):
for j in range(1000):
s += i*j + 1
timer.stop()
print(timer.time_taken())
import time
time.time()
1633444920.887516