Author Dusty Phillips Reviewers



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

[
 148 
]
This code is not very safe. You can very easily move past the ending 
position, and if you try to go home on an empty file, it will crash. 
These examples are kept short to make them readable, but that doesn't 
mean they are defensive! You can improve the error checking of this 
code as an exercise; it might be a great opportunity to expand your 
exception handling skills.
The 
Document
class itself is hardly changed, except for removing the two methods 
that were moved to the 
Cursor
class:
class Document:
def __init__(self):
self.characters = []
self.cursor = Cursor(self)
self.filename = ''
def insert(self, character):
self.characters.insert(self.cursor.position,
character)
self.cursor.forward()
def delete(self):
del self.characters[self.cursor.position]
def save(self):
f = open(self.filename, 'w')
f.write(''.join(self.characters))
f.close()
We simply updated anything that accessed the old cursor integer to use the new object 
instead. We can test that the 
home
method is really moving to the newline character:
>>> d = Document()
>>> d.insert('h')
>>> d.insert('e')
>>> d.insert('l')
>>> d.insert('l')
>>> d.insert('o')
>>> d.insert('\n')
>>> d.insert('w')
www.it-ebooks.info


Chapter 5
[
 149 
]
>>> d.insert('o')
>>> d.insert('r')
>>> d.insert('l')
>>> d.insert('d')
>>> d.cursor.home()
>>> d.insert("*")
>>> print("".join(d.characters))
hello
*world
Now, since we've been using that string 
join
function a lot (to concatenate the 
characters so we can see the actual document contents), we can add a property
to the 
Document
class to give us the complete string:
@property
def string(self):
return "".join(self.characters)
This makes our testing a little simpler:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   140   141   142   143   144   145   146   147   ...   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