Python question Write (and test) the following 10 functions (names are taken from old vi editor) for each implementation
(1) cmd_h: move cursor one character to the left
(2) cmd_I: move cursor one character to the right
(3) cmd_j: move cursor vertically up one line
(4) cmd_k: move cursor vertically down one line
(5) cmd_X: delete the character to the left of the cursor
(6) cmd_D: remove on current line from cursor to the end
(7) cmd_dd: delete current line and move cursor to the beginning of next line
(8) cmd_ddp: transpose two adjacent lines
(9) cmd_n: search for next occurrence of a string (assume that string to be searched is fully in one line.
(10) cmd_wq: write your representation as text file and save it
Think of and implement any other 5 functions (your choice)
For testing, you will read the following “nerdy” poem (from the “Zen of Python”) into your “file representation”.
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Code:
class func:
"""Store text file as list of strings where each string
represents a line"""
def __init__(self):
self.cursor = 0
f_in = open("test.txt", "r")
file_text = f_in.readlines() # read file into list of strings
def cmd_h(self):
"""move cursor one character to the left"""
if self.cursor > 0:
self.cursor = self.cursor - 1
else:
return self.cursor
self.get_text(func.file_text)
def cmd_I(self):
"""move cursor one character to the right"""
ls = func.file_text
length = 0
prev = 0
for line in ls:
length = len(line) + length
if prev <= self.cursor < length:
self.cursor = self.cursor + 1
prev = length
continue
self.get_text(func.file_text)
def cmd_k(self):
"""move cursor down vertically one line"""
ls = func.file_text
numLines = len(ls)
length = 0
prev = 0
for counter, line in enumerate(ls):
length = len(line) + length
if prev <= self.cursor <= length:
if counter != numLines:
self.cursor = self.cursor + len(line)
self.get_text(func.file_text)
break
prev = length
def cmd_j(self):
"""move cursor up vertically one line"""
ls = func.file_text
length = 0
older = 0
prev = 0
for counter, line in enumerate(ls):
length = len(line) + length
if prev <= self.cursor <= length:
if counter != 0:
self.cursor = self.cursor - older
self.get_text(func.file_text)
break
prev = length
older = len(line)
def cmd_X(self):
"""delete the character to the left of the cursor"""
ls = func.file_text
length = 0
prev = 0
result = []
if self.cursor > 0:
for line in ls:
length = len(line) + length
if prev <= self.cursor < length:
result.append(line[0:self.cursor-1] + line[self.cursor:])
self.cursor = self.cursor - 1
prev = length
continue
result.append(line)
self.get_text(result)
else:
self.get_text(func.file_text)
def cmd_D(self):
"""remove on current line from cursor to the end"""
ls = func.file_text
length = 0
prev = 0
result = []
for line in ls:
length = len(line) + length
if prev <= self.cursor <= length:
prev = length
continue
result.append(line)
self.get_text(result)
def cmd_dd(self):
"""delete current line and move cursor to the beginning of next line"""
ls = func.file_text
length = 0
prev = 0
result = []
for line in ls:
length = len(line) + length
if prev <= self.cursor <= length:
self.cursor = prev
prev = length
continue
result.append(line)
self.get_text(result)
def cmd_ddp(self, l1, l2):
"""transpose two adjacent lines"""
ls = func.file_text
tmp1 = ls[l1]
tmp2 = ls[l2]
result = []
for counter, line in enumerate(ls):
if counter == l1:
result.append(tmp2)
elif counter == l2:
result.append(tmp1)
else:
result.append(line)
self.get_text(result)
def cmd_n(self, s):
"""search for next occurrence of a string (assume that string to be searched is fully in one line."""
ls = func.file_text
length = 0
prev = 0
for line in ls:
length = len(line) + length
if line.find(s):
self.cursor = prev + line.find(s)
break
prev = length
self.get_text(func.file_text)
def cmd_wq(self):
"""write your representation as text file and save it"""
out_file = open("output.txt", "w")
out_file.write(str(self.get_text(func.file_text)))
out_file.close()
print("\nFile saved to output.txt")
def setCursor(self, cursor):
self.cursor = cursor
self.get_text(func.file_text)
def get_text(self, ls):
result = ""
length = 0
prev = 0
for line in ls:
length = len(line) + length
if prev <= self.cursor <= length:
end = self.cursor - length
result = result + line[0:end] + "^" + line[end:]
prev = length
continue
result = result + line
print(result)
return(result)
def main():
print("--- Printing initial")
fo = func()
fo.get_text(func().file_text)
print("\n--- Setting cursor randomly at 25")
fo.setCursor(25)
print("\n--- Move cursor 1 to the right")
fo.cmd_I()
print("\n--- Move cursor 1 to the left")
fo.cmd_h()
print("\n--- Move cursor down vertically one line")
fo.cmd_k()
print("\n--- Move cursor up vertically one line")
fo.cmd_j()
print("\n--- Delete the character to the left of cursor")
fo.cmd_X()
print("\n--- Remove current line from cursor")
fo.cmd_D()
print("\n--- Delete current line and move cursor to beginning of next line")
fo.cmd_dd()
print("\n--- Transpose lines 2 and 3")
fo.cmd_ddp(2,3)
print("\n--- Search next \"better\" string")
fo.cmd_n("better")
print("\n--- Write text and save as file")
fo.cmd_wq()
main()
Output:
--- Printing initial
^Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Setting cursor randomly at 25
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Move cursor 1 to the right
Beautiful is better than u^gly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Move cursor 1 to the left
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Move cursor down vertically one line
Beautiful is better than ugly.
Explicit is better than i^mplicit.
Simple is better than complex.
Complex is better than complicated.
--- Move cursor up vertically one line
Beautiful is better than ^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Delete the character to the left of cursor
Beautiful is better than^ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Remove current line from cursor
Explicit is better than ^implicit.
Simple is better than complex.
Complex is better than complicated.
--- Delete current line and move cursor to beginning of next line
^Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Transpose lines 2 and 3
^Beautiful is better than ugly.
Explicit is better than implicit.
Complex is better than complicated.Simple is better than complex.
--- Search next "better" string
Beautiful is ^better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
--- Write text and save as file
Beautiful is ^better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
File saved to output.txt
Saved file "output.txt" content:
Get Answers For Free
Most questions answered within 1 hours.