Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet143/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   139   140   141   142   143   144   145   146   ...   183
Bog'liq
python3-oop

[
 146 
]
We'll answer these questions as we go, but let's start with the simplest possible 
Document
class first and see what it can do:
class Document:
def __init__(self):
self.characters = []
self.cursor = 0
self.filename = ''
def insert(self, character):
self.characters.insert(self.cursor, character)
self.cursor += 1
def delete(self):
del self.characters[self.cursor]
def save(self):
with open(self.filename, 'w') as f:
f.write(''.join(self.characters))
def forward(self):
self.cursor += 1
def back(self):
self.cursor -= 1
This simple class allows us full control over editing a basic document. Have a look
at it in action:
>>> doc = Document()
>>> doc.filename = "test_document"
>>> doc.insert('h')
>>> doc.insert('e')
>>> doc.insert('l')
>>> doc.insert('l')
>>> doc.insert('o')
>>> "".join(doc.characters)
'hello'
>>> doc.back()
>>> doc.delete()
>>> doc.insert('p')
>>> "".join(doc.characters)
'hellp'
www.it-ebooks.info


Chapter 5
[
 147 
]
Looks like it's working. We could connect a keyboard's letter and arrow keys to these 
methods and the document would track everything just fine.
But what if we want to connect more than just arrow keys. What if we want to connect 
the 
Home
and 
End
keys as well? We could add more methods to the 
Document
class 
that search forward or backwards for newline characters (in Python, a newline 
character, or 
\n
represents the end of one line and the beginning of a new one) in 
the string and jump to them, but if we did that for every possible movement action 
(move by words, move by sentences, 
Page Up

Page Down
, end of line, beginning of 
whitespace, and more), the class would be huge. Maybe it would be better to put those 
methods on a separate object. So, let us turn the cursor attribute into an object that is 
aware of its position and can manipulate that position. We can move the forward and 
back methods to that class, and add a couple more for the 
Home
and 
End
keys:
class Cursor:
def __init__(self, document):
self.document = document
self.position = 0
def forward(self):
self.position += 1
def back(self):
self.position -= 1
def home(self):
while self.document.characters[
self.position-1] != '\n':
self.position -= 1
if self.position == 0:
# Got to beginning of file before newline
break
def end(self):
while self.position < len(self.document.characters
) and self.document.characters[
self.position] != '\n':
self.position += 1
This class takes the document as an initialization parameter so the methods have 
access to the content of the document's character list. It then provides simple 
methods for moving backwards and forwards, as before, and for moving to the
home
and 
end
positions.
www.it-ebooks.info


When to Use Object-oriented Programming

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   139   140   141   142   143   144   145   146   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish