Instructions for checklist

(produced at 16:52 UTC on 2021-11-09)

This task is part of ps08 which is due at 23:59 EST on 2021-11-16.

This is an individual task.

You can submit this task using this link.

Put all of your work for this task into the file checklist.py
(which is provided among the starter files)

Overview

For this task, you will write some functions that perform very basic operations with dictionaries, allowing them to be used as a kind of checklist. These checklists will be able to store two types of information: true/false check-off tasks, and sub-lists of strings. An example helps illustrate this: The following dictionary represents a checklist for going camping:

{
    "Tent": False,
    "Sleeping bags": False,
    "Cooking supplies": False,
    "Camp chairs": False,
    "Snacks": [],
    "Meals": []
}

The "Tent", "Sleeping bags", "Cooking supplies", and "Camp chairs" items are just True/False values, while the "Snacks" and "Meals" entries are lists that can store specific strings indicating what gets packed. After packing for the trip, the dictionary might be filled out as follows:

{
    "Tent": True,
    "Sleeping bags": True,
    "Cooking supplies": True,
    "Camp chairs": True,
    "Snacks": ["Chips", "Trail mix", "Dried apricots", "Gummy bears"],
    "Meals": ["Oatmeal", "Oatmeal", "Sandwiches", "Sandwiches", "Ramen",
        "Hot dogs"]
}

To be specific about the structure: a checklist is a dictionary with strings for keys. Each string has either a Boolean or a list as its value. Keys with Boolean values are called tasks, and we set their value to True to mark them as completed. Keys with list values are called lists, and they will only contain strings, which we can add one by one to the end of the list.

Your job is to write a series of simple functions for manipulating this type of dictionary:

  • displayChecklist which will format it as a checklist.
  • addTask which will allow a new checkable slot to be added.
  • addList which will allow a new list slot to be added.
  • completeTask which will mark a task as completed.
  • addItemToList which will append an item to the contents of a list slot.
  • saveChecklist which will save the checklist into a text file.

If you want an extra challenge, there is an ungraded bonus function loadChecklist for loading a checklist from a file that was saved with saveChecklist.

Note: as usual, you will need to document each function you write.

displayChecklist

This function accepts a checklist as its only parameter, and it must print out the checklist using the exact format shown in these examples for displayChecklist. In particular, each task in the checklist will be printed on a line that starts with either '[X] ' if it is completed, or '[ ] ' if it is not yet completed. Each sub-list in the checklist will have its name printed followed by a colon, and then each item from the list will be printed with a dash and a space in front of it.

This function will only print, and will not return anything; its only parameter is the checklist object to display. It will print tasks and lists in whatever order they occur in in the dictionary it is given (use a for loop over the dictionary for this).

addTask

This function must add a task to a checklist, setting its initial value to False. These examples for addTask demonstrate how it works. It will accept two parameters: the checklist object to add to, and the task name to add. In addition to adding the task, it must return the (now modified) checklist object that it was given.

There is one issue, however: a task cannot be added to a checklist if the name requested is already being used by another task or list in that checklist. In that case, this function must print the message 'That name has already been used.', and must return None.

addList

This function must add a list to a checklist, setting its initial value to an empty list. These examples for addList demonstrate how it works. It will accept two parameters: the checklist object to add to, and the list name to add. In addition to adding the list, it must return the (now modified) checklist object that it was given.

As with addTask, a list cannot be added that shares a name with an existing task or list. In that case, this function must print the message 'That name has already been used.', and must return None.

completeTask

This function must set the value of a task in a checklist to True. These examples for completeTask demonstrate how it works. It will accept two parameters: the checklist object to add to, and the name of the task to mark as completed. In addition to changing the task value, it must return the (now modified) checklist object that it was given.

As with addTask and addList, there's a catch: if the task name provided is not in the checklist, or if it's actually a sub-list instead of a task, this function must print the message 'That's not a valid task name.', and must return None.

addItemToList

This function must append a string to a sub-list with a certain name within a checklist. These examples for addItemToList demonstrate how it works. It will accept three parameters: the checklist object to add to, and the name of the list to append to, and the string to append to that list. As with the other functions in this task, it must return the (now modified) checklist object that it was given.

There's also the usual catch: if the named list does not exist or is a task instead of a list, this function must print the message 'That's not a valid list name.', and must return None. (Note that this error message is different from the message for completeTask.)

saveChecklist

The last function you need to write is called saveChecklist, and it must write out the checklist to a file, using the same format used by displayChecklist. These examples for saveChecklist demonstrate how it works. It will accept two parameters: the checklist object to save, and the name of the file that it should create (or overwrite). It does not need to return anything, and it doesn't need to handle any errors.

As an extra goal, you should use a with statement to handle the file opening and closing process.

Challenge Problem

If you've completed the six functions above, you're done. However, as an ungraded challenge problem, we invite you to try defining a function called loadChecklist. This function takes a single argument (the filename to load from) and reads in data in the same format as saveChecklist writes, creating a checklist based on the data and returning it.

If you're stuck with this and want a bit more guidance on loadChecklist, click here. To tackle this problem, you'll want to do the following:
  • Read the file one line at a time.
  • Use conditionals to determine what kind of line you're looking at: the first or last characters in the line give you hints.
  • Call the methods you've defined above to build your result dictionary step-by-step.
  • You may need a variable to remember the name of the current list that you're reading into.
  • Feel free to print something like 'Invalid file format!' and return None if you encounter text that is inconsistent with the formatting defined by saveChecklist.

Examples

displayChecklist examples

These examples demonstrate how displayChecklist works. Note that whether a task is completed or not is indicated using either a capital 'X' or a space between square brackets, and that for a list entry, the name appears first with a colon at the end of the line, then each item gets its own line with a dash and a space at the beginning of the line.

In []:
displayChecklist({'Task 1': False, 'Task 2': True})
Prints
[ ] Task 1 [X] Task 2
In []:
displayChecklist( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True} )
Prints
[ ] Item 1 Stuff: - Thing 1 - Thing 2 [X] Item 2
In []:
displayChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], } )
Prints
[ ] Tent [X] Sleeping bags [X] Cooking supplies [ ] Camp chairs Snacks: - Chips Meals: - Ramen - Oatmeal - Sandwiches

addTask examples

These examples demonstrate how addTask works. Note that the value for the new task is always False, and if the task can't be added because the desired name is already being used, a message is printed and None is returned.

In []:
addTask({}, 'New task')
Out[]:
{'New task': False}
In []:
addTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'New task' )
Out[]:
{ 'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True, 'New task': False, }
In []:
addTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }, 'Flashlights' )
Out[]:
{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], 'Flashlights': False, }
In []:
addTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Item 1' )
Prints
That name has already been used.
In []:
addTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Stuff' )
Prints
That name has already been used.

addList examples

These examples demonstrate how addList works. Note that the value for the new list is always an empty list. Like addTask, a message is printed and None is returned if the name is already in use.

In []:
addList({}, 'New list')
Out[]:
{'New list': []}
In []:
addList( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'New list' )
Out[]:
{ 'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True, 'New list': [], }
In []:
addList( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }, 'Games' )
Out[]:
{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], 'Games': [], }
In []:
addList( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Item 1' )
Prints
That name has already been used.
In []:
addList( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Stuff' )
Prints
That name has already been used.

completeTask examples

These examples demonstrate how completeTask works. Note that a message is printed and None is returned if the task isn't already in the checklist, or if a list is named instead of a task. Also note that if the task is already completed, that's not an error, and it just stays completed.

In []:
completeTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Item 1' )
Out[]:
{'Item 1': True, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}
In []:
completeTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }, 'Tent' )
Out[]:
{ 'Tent': True, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }
In []:
completeTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Item 2' )
Out[]:
{'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}
In []:
completeTask( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Not present' )
Prints
That's not a valid task name.
In []:
completeTask({}, 'Anything')
Prints
That's not a valid task name.

addItemToList examples

These examples demonstrate how addItemToList works. Note that a message is printed and None is returned if the named list isn't already in the list, or if a task is named instead of a list.

In []:
addItemToList( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Stuff', 'New item' )
Out[]:
{'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2', 'New item'], 'Item 2': True}
In []:
addItemToList( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }, 'Meals', 'Curry' )
Out[]:
{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches', 'Curry'], }
In []:
addItemToList( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'Item 1', 'Error' )
Prints
That's not a valid list name.
In []:
addItemToList({}, "Doesn't exist", 'Anything')
Prints
That's not a valid list name.

saveChecklist examples

These examples demonstrate how saveChecklist works. Note that the text it writes into the specified file is the same as what gets printed by displayChecklist. Note that if the checklist is empty, nothing gets printed, and if there's a list in the checklist that's empty, the name gets printed with a colon afterwards but there won't be any lines for items.

In []:
saveChecklist( {'Item 1': False, 'Stuff': ['Thing 1', 'Thing 2'], 'Item 2': True}, 'checklist.txt' )
File
checklist.txt
[ ] Item 1 Stuff: - Thing 1 - Thing 2 [X] Item 2
In []:
saveChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': ['Chips'], 'Meals': ['Ramen', 'Oatmeal', 'Sandwiches'], }, 'camping.txt' )
File
camping.txt
[ ] Tent [X] Sleeping bags [X] Cooking supplies [ ] Camp chairs Snacks: - Chips Meals: - Ramen - Oatmeal - Sandwiches
In []:
saveChecklist({}, 'empty.txt')
File
empty.txt
In []:
saveChecklist({'Stuff': []}, 'nolist.txt')
File
nolist.txt
Stuff:

Rubric

 
unknown Style Requirements
How your code is written.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown All functions are documented
Each function you define must include a non-empty documentation string as the very first thing in the function.
 
unknown Procedure Requirements
What code you use to solve the problem.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown Define displayChecklist with 1 parameter
Use def to define displayChecklist with 1 parameter
 
unknown Use a loop
Within the definition of displayChecklist with 1 parameter, use any kind of loop in at least once place.
 
unknown Call print
Within the definition of displayChecklist with 1 parameter, call print in at least once place.
 
unknown Define addTask with 2 parameters
Use def to define addTask with 2 parameters
 
unknown Use a return statement
Within the definition of addTask with 2 parameters, use return _ in at least once place.
 
unknown Define addList with 2 parameters
Use def to define addList with 2 parameters
 
unknown Use a return statement
Within the definition of addList with 2 parameters, use return _ in at least once place.
 
unknown Define completeTask with 2 parameters
Use def to define completeTask with 2 parameters
 
unknown Use a return statement
Within the definition of completeTask with 2 parameters, use return _ in at least once place.
 
unknown Define addItemToList with 3 parameters
Use def to define addItemToList with 3 parameters
 
unknown Use a return statement
Within the definition of addItemToList with 3 parameters, use return _ in at least once place.
 
unknown Define saveChecklist with 2 parameters
Use def to define saveChecklist with 2 parameters
 
unknown Use a loop
Within the definition of saveChecklist with 2 parameters, use any kind of loop in at least once place.
 
unknown Call open
Within the definition of saveChecklist with 2 parameters, call open in at least once place.
 
unknown Call write
Within the definition of saveChecklist with 2 parameters, call write in at least once place.
 
unknown Extra goals
Complete all extra goals in addition to the core goals for a perfect score.
 
unknown Define saveChecklist with 2 parameters
Use def to define saveChecklist with 2 parameters
 
unknown Use a with statement
Within the definition of saveChecklist with 2 parameters, use with _ as _: ___ in at least once place.
 
unknown Product Requirements
Your code's result values.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown addTask returns the correct result
The result returned when your addTask function is run must match the solution result.
 
unknown addList returns the correct result
The result returned when your addList function is run must match the solution result.
 
unknown completeTask returns the correct result
The result returned when your completeTask function is run must match the solution result.
 
unknown addItemToList returns the correct result
The result returned when your addItemToList function is run must match the solution result.
 
unknown Extra goals
Complete all extra goals in addition to the core goals for a perfect score.
 
unknown addTask returns the correct result
The result returned when your addTask function is run must match the solution result.
 
unknown addList returns the correct result
The result returned when your addList function is run must match the solution result.
 
unknown completeTask returns the correct result
The result returned when your completeTask function is run must match the solution result.
 
unknown addItemToList returns the correct result
The result returned when your addItemToList function is run must match the solution result.
 
unknown Behavior Requirements
What your code does from the user's perspective.
 
unknown Core goals
Complete all core goals for core credit. Get partial credit for completing at least half, and more partial credit for completing at least 90%.
 
unknown displayChecklist prints the correct output
The output printed when your displayChecklist function is run must match the solution output.
 
unknown saveChecklist writes the correct data into checklist.txt
The data written to checklist.txt when your saveChecklist function is run must match what the solution writes.
 
unknown Extra goals
Complete all extra goals in addition to the core goals for a perfect score.
 
unknown displayChecklist prints the correct output
The output printed when your displayChecklist function is run must match the solution output.
 
unknown addTask prints the correct output
The output printed when your addTask function is run must match the solution output.
 
unknown addList prints the correct output
The output printed when your addList function is run must match the solution output.
 
unknown completeTask prints the correct output
The output printed when your completeTask function is run must match the solution output.
 
unknown addItemToList prints the correct output
The output printed when your addItemToList function is run must match the solution output.
 
unknown saveChecklist writes the correct data into checklist.txt
The data written to checklist.txt when your saveChecklist function is run must match what the solution writes.