Instructions for checklist

(produced at 05:28 a.m. UTC on 2024-11-08)

This task is part of project09 which is due at 23:00 EDT on 2024-04-09.

You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more.

You can download the starter code for this task using this link.

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 strings naming tasks, along with booleans indicating whether each task is completed or not. 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": False,
    "Meals": False
}

Each key represents something to be checked off, and the corresponding value represents whether it's checked off (True) or not (False).

After doing some packing for the trip, the dictionary might be filled out as follows:

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

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

  • addTask which will allow a new task to be added.
  • taskStatus which will return the status of a task.
  • completeTask which will mark a task as completed.
  • resetChecklist which will mark every task in the list as uncompleted.

...plus some more complex functions for displaying, loading, and saving these dictionaries:

  • displayChecklist which will format a dictionary as a checklist. This function must use a helper function named lengthAndValue for sorting, which you must also define.
  • saveChecklist which will save a checklist to a file.
  • loadChecklist which will load a checklist from a file.

Note: as usual, you will need to document each function you write. For this task, however, you do not need to worry about wasting fruit or wasting boxes.

The paragraphs below describe each function that you must define in detail.

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 in that checklist. In that case, this function must print a specific message (see the examples), and must return None.

taskStatus

This function must return the task status for a particular task from a checklist. These examples for taskStatus demonstrate how it works.

As with addTask, there's the possibility that the task specified is invalid (in this case, because it isn't in the checklist). In that case, taskStatus must print a specific message (see the examples), 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 taskStatus, there's a catch: if the task name provided is not in the checklist, this function must print a specific message (see the examples), and must return None.

resetChecklist

This function must reset the value of every task in a checklist to False. These examples for resetChecklist demonstrate how it works. It accept just one parameter: the checklist object to reset, and it must return the (now modified) checklist object that it was given.

Unlike the other functions, there's no condition under which this one returns None. Also note that while none of them may use a loop, this function must use a loop.

lengthAndValue

This function accepts a string as its only parameter, and it must return a tuple containing first the length of the string, and then the string itself. These examples for lengthAndValue show how it should work.

This function isn't related to checklists, but will be used as a sort key in the displayChecklist and saveChecklist functions.

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. In addition, the items must be sorted according to the lengths of their names; shortest to longest with ties broken by alphabetical order. You must use sort or sorted with the key= argument set to lengthAndValue in order to achieve this.

This function will only print, and will not return anything; its only parameter is the checklist object to display.

saveChecklist

This function accepts two parameters: a checklist and a filename., and it must write the checklist into the target file, deleting any previous contents that file had. using the exact format shown in these examples for saveChecklist. The format used in the file is exactly the same as the format used by displayChecklist, including how the items are sorted. You must use sort or sorted with the key= argument set to lengthAndValue in order to achieve this, and you must also use with ... as to open the file.

This function will not print or return anything.

loadChecklist

This function accepts one parameter: a filename., and it must load and return the checklist that was stored in that file by the saveChecklist function.. These loadChecklist examples show how it should work. Note in particular that if the file is not properly formatted, it should print a specific error message and return None.

You must use with ... as to open the file.

A note on APIs

All together, the function's you've defined in this task are a kind of "checklist API." We often have students ask us what "API" means, and we thought that building one yourself would be a good way to answer that question. API stands for Application Programming Interface, and it basically just means a bundle of function contracts that define what operations are supported by a certain library or code file. In this task, the "checklist API" says that the supported operations for a checklist are: add a task, check the status of a task, complete a task, and reset all tasks, plus you can print out a checklist, save it, or load it.

Other libraries that we use in this class each have their own API. For example, the turtle API defines what you can draw with turtle, the turtleBeads API defines some additional drawing possibilities; the wavesynth API defines options for building music, and the optimism API defines operations for setting up test cases. We've also used built-in Python APIs like the os API, and you could consider the methods for a particular type as an API as well, so we're using the "string API" when we use methods like .split or .lower. Basically, just like a function contract specifies what a function promises to do, and then the actual code defines how that's done, an API defines what a series of related functions will do, and then an implementation of that API defines the actual functions.

One thing you will learn about in later classes is how to design an API: for this task, we just told you what to write, but when you're not working on class assignments, you'll have to figure out what's needed and how things should fit together yourself.

Examples

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({'Task 1': False, 'Task 2': True}, 'New task')
Out[]:
{'Task 1': False, 'Task 2': True, 'New task': False}
In []:
addTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Flashlights' )
Out[]:
{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, 'Flashlights': False, }
In []:
addTask({'only': True}, 'only')
Prints
That task already exists.

taskStatus examples

These examples demonstrate how taskStatus works. Note that if the named task isn't in the checklist, a message is printed and None is returned.

In []:
taskStatus({'Task 1': False, 'Task 2': True}, 'Task 1')
Out[]:
False
In []:
taskStatus( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Tent' )
Out[]:
False
In []:
taskStatus( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Meals' )
Out[]:
True
In []:
taskStatus({}, 'Item')
Prints
That task does not exist.

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({'Task 1': False, 'Task 2': True}, 'Task 1')
Out[]:
{'Task 1': True, 'Task 2': True}
In []:
completeTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Tent' )
Out[]:
{ 'Tent': True, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }
In []:
completeTask( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'Meals' )
Out[]:
{ 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }
In []:
completeTask({}, 'Anything')
Prints
That task does not exist.

resetChecklist examples

These examples demonstrate how resetChecklist works.

In []:
resetChecklist({'Task 1': False, 'Task 2': True})
Out[]:
{'Task 1': False, 'Task 2': False}
In []:
resetChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, } )
Out[]:
{ 'Tent': False, 'Sleeping bags': False, 'Cooking supplies': False, 'Camp chairs': False, 'Snacks': False, 'Meals': False, }
In []:
resetChecklist({})
Out[]:
{}

lengthAndValue examples

These examples demonstrate how lengthAndValue works. Note that although it will be used for sorting, it does not itself do any sorting or even involve a loop.

In []:
lengthAndValue('hello')
Out[]:
(5, 'hello')
In []:
lengthAndValue('hi')
Out[]:
(2, 'hi')
In []:
lengthAndValue('goodbye')
Out[]:
(7, 'goodbye')

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. Also note that the keys of the checklist are sorted by the lengths of their names, shortest to longest, with ties broken by alphabetical order.

In []:
displayChecklist({'only': True})
Prints
[X] only
In []:
displayChecklist({'Task 1': False, 'Task 2': True})
Prints
[ ] Task 1 [X] Task 2
In []:
displayChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, } )
Prints
[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking supplies

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, including the sorting of the keys. Also note that if the checklist is empty, nothing gets written to the file.

In []:
saveChecklist({'Task 1': False, 'Task 2': True}, 'checklist.txt')
File
checklist.txt
[ ] Task 1 [X] Task 2
In []:
saveChecklist( { 'Tent': False, 'Sleeping bags': True, 'Cooking supplies': True, 'Camp chairs': False, 'Snacks': False, 'Meals': True, }, 'camping.txt' )
File
camping.txt
[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking supplies
In []:
saveChecklist({}, 'empty.txt')
File
empty.txt

loadChecklist examples

These examples demonstrate how loadChecklist works. It operates as the opposite of saveChecklist. Note that in these examples, the file contents shown must exist before the function is called; they are not produced by it, unlike with saveChecklist.

In []:
loadChecklist('checklist.txt')
Out[]:
{'Task 1': False, 'Task 2': True}
File
checklist.txt
[ ] Task 1 [X] Task 2
In []:
loadChecklist('camping.txt')
Out[]:
{ 'Tent': False, 'Meals': True, 'Snacks': False, 'Camp chairs': False, 'Sleeping bags': True, 'Cooking supplies': True, }
File
camping.txt
[ ] Tent [X] Meals [ ] Snacks [ ] Camp chairs [X] Sleeping bags [X] Cooking supplies
In []:
loadChecklist('empty.txt')
Out[]:
{}
File
empty.txt
In []:
loadChecklist('malformed.txt')
Prints
Invalid file format!
File
malformed.txt
Not a checklist.

Rubric

Group goals:
 
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 addTask must return the correct result
The result returned when your addTask function is run must match the solution result.
 
unknown addTask must print the correct output
The output printed when your addTask function is run must match the solution output.
 
unknown addTask must return the correct result
The result returned when your addTask function is run must match the solution result.
 
unknown addTask should modify and return the same dictionary it receives as an argument.
We will call addTask and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.
 
unknown taskStatus must return the correct result
The result returned when your taskStatus function is run must match the solution result.
 
unknown taskStatus must print the correct output
The output printed when your taskStatus function is run must match the solution output.
 
unknown taskStatus must return the correct result
The result returned when your taskStatus function is run must match the solution result.
 
unknown completeTask must return the correct result
The result returned when your completeTask function is run must match the solution result.
 
unknown completeTask must print the correct output
The output printed when your completeTask function is run must match the solution output.
 
unknown completeTask must return the correct result
The result returned when your completeTask function is run must match the solution result.
 
unknown completeTask should modify and return the same dictionary it receives as an argument.
We will call completeTask and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.
 
unknown resetChecklist must return the correct result
The result returned when your resetChecklist function is run must match the solution result.
 
unknown resetChecklist must return the correct result
The result returned when your resetChecklist function is run must match the solution result.
 
unknown resetChecklist should modify and return the same dictionary it receives as an argument.
We will call resetChecklist and check sure that it returns the same dictionary that was given to it as an argument, instead of creating a new dictionary as its result.
 
unknown lengthAndValue must return the correct result
The result returned when your lengthAndValue function is run must match the solution result.
 
unknown lengthAndValue must return the correct result
The result returned when your lengthAndValue function is run must match the solution result.
 
unknown displayChecklist must print the correct output
The output printed when your displayChecklist function is run must match the solution output.
 
unknown displayChecklist must print the correct output
The output printed when your displayChecklist function is run must match the solution output.
 
unknown saveChecklist must write 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 saveChecklist must write 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 loadChecklist must return the correct result.
We will set up test files and run loadChecklist to check that it can correctly load them.
 
unknown loadChecklist must print the correct output
The output printed when your loadChecklist function is run must match the solution output.
 
unknown loadChecklist must return the correct result.
We will set up test files and run loadChecklist to check that it can correctly load them.
 
unknown loadChecklist returns None when the file is not a checklist.
We will test loadChecklist with some files that are not properly formatted. It should return None.
 
unknown loadChecklist prints the correct message when the file is not a checklist.
We will test loadChecklist with some files that are not properly formatted. It should print the correct error message.
 
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 one place.
 
unknown Do not use any kind of loop
Within the definition of addTask with 2 parameters, do not use any kind of loop.
 
unknown Define taskStatus with 2 parameters
Use def to define taskStatus with 2 parameters
 
unknown Use a return statement
Within the definition of taskStatus with 2 parameters, use return _ in at least one place.
 
unknown Do not use any kind of loop
Within the definition of taskStatus with 2 parameters, do not use any kind of loop.
 
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 one place.
 
unknown Do not use any kind of loop
Within the definition of completeTask with 2 parameters, do not use any kind of loop.
 
unknown Define resetChecklist with 1 parameter
Use def to define resetChecklist with 1 parameter
 
unknown Use a return statement
Within the definition of resetChecklist with 1 parameter, use return _ in at least one place.
 
unknown Use any kind of loop
Within the definition of resetChecklist with 1 parameter, use any kind of loop in at least one place.
 
unknown Define lengthAndValue with 1 parameter
Use def to define lengthAndValue with 1 parameter
 
unknown Use a return statement
Within the definition of lengthAndValue with 1 parameter, use return _ in at least one place.
 
unknown Do not use any kind of loop
Within the definition of lengthAndValue with 1 parameter, do not use any kind of loop.
 
unknown Define displayChecklist with 1 parameter
Use def to define displayChecklist with 1 parameter
 
unknown Use a sorted() or .sort() call which uses lengthAndValue as the sort key
Within the definition of displayChecklist with 1 parameter, use sorted(_, key=lengthAndValue) or _.sort(key=lengthAndValue) in at least one place.
 
unknown Use any kind of loop
Within the definition of displayChecklist with 1 parameter, use any kind of loop in at least one place.
 
unknown Call print
Within the loop within the definition of displayChecklist with 1 parameter, call print in at least one place.
 
unknown Define saveChecklist with 2 parameters
Use def to define saveChecklist with 2 parameters
 
unknown Call open
Within the definition of saveChecklist with 2 parameters, call open in at least one place.
 
unknown Use a sorted() or .sort() call which uses lengthAndValue as the sort key
Within the definition of saveChecklist with 2 parameters, use sorted(_, key=lengthAndValue) or _.sort(key=lengthAndValue) in at least one place.
 
unknown Use any kind of loop
Within the definition of saveChecklist with 2 parameters, use any kind of loop in at least one place.
 
unknown Call write
Within the definition of saveChecklist with 2 parameters, call write in at least one place.
 
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 one place.
 
unknown Use any kind of loop
Within the definition of saveChecklist with 2 parameters, use any kind of loop in at least one place.
 
unknown Do not call open
Within the loop within the definition of saveChecklist with 2 parameters, do not call open.
 
unknown Call write
Within the definition of saveChecklist with 2 parameters, call write in exactly one place.
 
unknown Define loadChecklist with 1 parameter
Use def to define loadChecklist with 1 parameter
 
unknown Call open
Within the definition of loadChecklist with 1 parameter, call open in at least one place.
 
unknown Use any kind of loop
Within the definition of loadChecklist with 1 parameter, use any kind of loop in at least one place.
 
unknown Define loadChecklist with 1 parameter
Use def to define loadChecklist with 1 parameter
 
unknown Use a with statement
Within the definition of loadChecklist with 1 parameter, use with _ as _: ___ in at least one place.
 
unknown Use any kind of loop
Within the definition of loadChecklist with 1 parameter, use any kind of loop in exactly one place.
 
unknown Do not call open
Within the loop within the definition of loadChecklist with 1 parameter, do not call open.