Browse Source

added case study of chapter 05

master
T. Meissner 10 years ago
parent
commit
0dbac08bd9
1 changed files with 47 additions and 0 deletions
  1. +47
    -0
      python_3_oop/chapter05/editor.py

+ 47
- 0
python_3_oop/chapter05/editor.py View File

@ -0,0 +1,47 @@
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()
@property
def string(self):
return "".join(self.characters)
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

Loading…
Cancel
Save