Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Steps to Complete Information Before working on this task, make sure that you have obtained a full score from Task 1 . This task builds
Steps to Complete
Information
Before working on this task, make sure that you have obtained a full score from Task This task builds on top of that work.
In this task you will reorganize the code base. Your goal is to separate the code that is strictly related to running the application in CLI from the more general code that can be leveraged by any interface eg CLI or GUI As an example, consider the run method of the AppEngine class.
def runself:
while True:
prompt 'What would you like to do
if self.correctanswer is not None:
prompt 'What amount should replace the questionmarks? $
cmd inputprompt
self.executecommandcmd
printfselfmessage
self.message None
if not self.continueexecution:
break
Clearly, this method is very specific to the CLI. It is almost certainly not suitable for the GUI where a user is mostly expected to click buttons, fill in text fields, etc. On the other hand, consider the removeitem method of the ItemPool class.
def removeitemself itemname:
if itemname not in self.items:
raise NonExistingItemErroritemname
del self.itemsitemname
This method is responsible for removing an item from the pool. It is not specific to CLI. It is likely to be used by both CLI and GUI. While from CLI it can be invoked via, eg a del command, from GUI it can be invoked, eg via a Remove button.
Danger
The steps that you are about to perform will change the application in such a way that a submission to Task will no longer be possible. If for whatever reason you plan to make submissions to Task in future, you should create a backup of your code base before proceeding further.
Restructure Codebase
Currently, the codebase has the following flat layout:
ppppdebugrefactortestpackage
app.py
errors.py
items.py
shoppinglist.py
Your goal is to reorganize the codebase so that the following layout is achieved:
ppppdebugrefactortestpackage
appcli.py
core
initpy
appengine.py
errors.py
items.py
shoppinglist.py
This means that you will:
Rename the app.py file to appcli.py
Create a new directory core and move the errors.py items.py and shoppinglist.py files to this directory. Note that VS Code may prompt you about some automatic changes to be done based on these actions. We recommend you do NOT accept those changes. First of all, this is because you would like to practice making such changes. Second, it is quite likely that the changes proposed by the VS Code may not be exactly what is needed. If you do not have enough experience with refactoring these changes may result in confusion.
Create empty initpy and appengine.py files in the core directory.
After you have moved the files around as described above, the application may no longer be working.
python appcli.py
Traceback most recent call last:
File appcli.py line in
from errors import
ModuleNotFoundError: No module named 'errors'
This is expected and it is no reason for concern. All you will have to do is to fix the import statements in all the application files. You have to realize that before the files were located in the same directory but this is no longer the case. It is up to you to figure what changes are needed to the import statements in all the files to make the application work again.
For example, the following no longer works:
from errors import InvalidItemNameError
It needs to be replaced with the following:
from core.errors import InvalidItemNameError
After making the changes, convince yourself that the application works by engaging in some nontrivial interaction.
python appcli.py
What would you like to do l
Shopping list with items has been created.
What would you like to do show list
SHOPPING LIST
Beef Steak x $
Macbook x $
TOTAL $
What would you like to do a
SHOPPING LIST
Beef Steak x $
Macbook x $
TOTAL $
What amount should replace the questionmarks? $
Not Correct! Expected $
You answered $
What would you like to do q
Have a nice day!
In addition convince yourself that the following statements work:
python
import core.errors
import core.items
import core.shoppinglist
Information
Do not proceed further unless you have convinced yourself that the application works. Be careful about leaving the original files in their original place. It is necessary to move the files not copy to the new location. If you would like to preserve the original files for future reference, you may keep the copies but keep them out of the project directory to minimize c
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started