Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Could someone help me finish my Python code for my currrent assignment. It is a two part assignment and I have the first part

Question:

Could someone help me finish my Python code for my currrent assignment. It is a two part assignment and I have the first part done, just need help with the second part of it. Below is my CursorBasedList code for the first part of the assignment and it is not to be changed. I need to write a simple text-editor program that utilizes my CursorBasedList class. When the text-editor program starts, it should ask for a text-file name (.txt) to edit. If the file name exists, it should load the file into an initially empty CursorBasedList object by reading each line from the file and use the insertAfter method to append the line to the list. Each node in the list will hold a single line of the text file. If the text-file name specified at startup does not exist, an empty CursorBasedList object is created to model editting a new file.

Assignment:

Regardless of whether you loaded a file or just created an empty list, a menu-driven loop very similar to the cursorBasedListTester.py program should allow you to edit the files content by modifying the list. (STRONG HINT: You might want to start with the cursorBasedListTester.py program as a rough starting point for your text-editor program.) You should NOT need to modify your CursorBasedList class only create a CursorBasedList object and use its methods. Make sure that your editor does not violate any preconditions of the CursorBasedList methods, so your editor is robust, i.e., does not crash when editting. When done editing, the lines of data contained in the nodes of the CursorBasedList are written back to the text file.

Your text-editor program should present a menu of options that allows the user to:

- Navigate and display the first line, i.e., the first line should be the current line

- Navigate and display the last line, i.e., the last line should be the current line

- Navigate and display the next line, i.e., the next line should become the current line. If there is no next line, tell the user and dont change the current line

- Navigate and display the previous line

- Insert a new line before the current line

- Insert a new line after the current line

- Delete the current line and have the line following become the current line. If there is no following line, the current line should be the last line.

- Replace the current line with a new line

- Save the current list back to a text file

Warning: When you load a text file into your list nodes, you can leave the characters on the end of each line of text. However, remember to add a character to end of inserted lines or replacement lines.

Here is my CursorBasedList code that is completed and should not be changed:

from node2way import Node2Way

class CursorBasedList(object): """ Linked implementation of a positional list.""" def __init__(self): """ Creates an empty cursor-based list.""" self._header = Node2Way(None) self._trailer = Node2Way(None) self._trailer.setPrevious(self._header) self._header.setNext(self._trailer) self._current = None self._size = 0

def hasNext(self): """ Returns True if the current item has a next item. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Empty list has no next item") return self._current.getNext() != self._trailer

def hasPrevious(self): """ Returns True if the current item has a previous item. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Empty list has no previous item") if self._current == self._header: return False return self._current.getPrevious() != self._header def first(self): """Moves the cursor to the first item if there is one. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Empty list has no first item") self._current = self._header.getNext()

def last(self): """Moves the cursor to the last item if there is one. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Empty list has no last item") self._current = self._trailer

def next(self): """Precondition: hasNext returns True. Postcondition: The current item is has moved to the right one item""" if self.isEmpty(): raise AttributeError("Empty list has no next item") if self.hasNext(): print("Current item has moved to the right one item") self._current = self._current.getNext()

def previous(self): """Precondition: hasPrevious returns True. Postcondition: The current item is has moved to the left one iten""" if self.isEmpty(): raise AttributeError("Empty list has no previous item") if self.hasPrevious(): print("Current item has moved to the left one item") self._current = self._current.getPrevious()

def insertAfter(self, item): """Inserts item after the current item, or as the only item if the list is empty. The new item is the current item.""" temp = Node2Way(None) temp.setData(item) if self.isEmpty(): self._header.setNext(temp); temp.setPrevious(self._header); temp.setNext(self._trailer); self._trailer.setPrevious(temp); self.first() return temp.setPrevious(self._current) if self.hasNext(): self._current.getNext().setPrevious(temp) temp.setNext(self._current.getNext()) else: self._trailer.setPrevious(temp) temp.setNext(self._trailer) self._current.setNext(temp) self._current = temp

def insertBefore(self, item): """Inserts item before the current item, or as the only item if the list is empty. The new item is the current item.""" temp = Node2Way(None) temp.setData(item) if self.isEmpty(): self._header.setNext(temp); temp.setPrevious(self._header); temp.setNext(self._trailer); self._trailer.setPrevious(temp); self.first() return temp.setNext(self._current) if self.hasPrevious(): self._current.getPrevious().setNext(temp) temp.setPrevious(self._current.getPrevious()) else: self._header.setNext(temp) temp.setPrevious(self._header) self._current.setPrevious(temp) self._current = temp

def getCurrent(self): """ Returns the current item without removing it or changing the current position. Precondition: the list is not empty""" if self.isEmpty(): raise AttributeError("Empty list has no current item") return self._current.getData()

def remove(self): """Removes and returns the current item. Making the next item the current item if one exists; otherwise the tail item in the list is the current item. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Cannot remove from an empty list") if self._current.getPrevious(): self._current.getPrevious().setNext(self._current.getNext()) if self._current.getNext(): self._current.getNext().setPrevious(self._current.getPrevious()) else: self._trailer = self._current.getPrevious() self.last() else: self._header = self._current.getNext()

def replace(self, newItemValue): """Replaces the current item by the newItemValue. Precondition: the list is not empty.""" if self.isEmpty(): raise AttributeError("Cannot replace item in an empty list") return self._current.setData(newItemValue)

def isEmpty(self): return self._header.getNext() == self._trailer

def __len__(self): """ Returns the number of items in the list.""" if self.isEmpty(): raise AttributeError("No items in an empty list") i = 0; temp = self._header.getNext() while temp != self._trailer: i = i + 1 temp = temp.getNext() return i def __str__(self): """Includes items from first through last.""" i = ""; temp = self._header.getNext() while temp != self._trailer: i = i + temp.getData() temp = temp.getNext() return i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions