The following document gives an overview about the exercise. It shows some examples how such script can work. You don't have to slavishly adhere to it. ;-) For example, if you prefer a certain file format or you don't like to output, then be creative and do how you like it!
Write a Python script task.py
which is able to manage your tasks. It has the following properties:
-
Like "git", the
task.py
script contains several subcommands: create, list, edit, remove, show, help. -
Return error codes !=0 if there is an error.
-
Use the directory
~/.config/task/
to store your task files. -
Use simple text files to store your tasks. Every file ends with
.task
. -
Use the current date and time as a basename for your task file.
-
To make it easier, the content of a task file follows this structure:
title tag message
If the tag is empty use an empty line.
Here is the help output from task.py
:
$ python3 task.py -h
usage: task.py [-h] [--version] [--verbose] action ...
task is a small command line utility to create, list, remove, and edit tasks.
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
--verbose, -v increase verbosity level
Subcommands:
action Available actions
create (c) Create a task
list (l, li) List tasks
edit (e, ed) Edit a task
remove (r, rm)
Removes a task
show (s) Shows a task
help (h, ?) Help of subcommands
$ python3 task.py create -h
usage: task.py create [-h] title [tag] message
Create a task with a title, an optional tag and a description
positional arguments:
title A short summary of this task
tag the optional tag for this task
message a short description of this task
$ python3 task.py list -h
usage: task.py list [-h]
optional arguments:
-h, --help show this help message and exit
$ python3 task.py edit -h
usage: task.py edit [-h] [--title TITLE] [--tag TAG] [--message MESSAGE] date
positional arguments:
date the tasks' date
optional arguments:
-h, --help show this help message and exit
--title TITLE, -t TITLE
The title to change of this task
--tag TAG The tag to change for this task
--message MESSAGE, -m MESSAGE
The message to change for this task
$ python3 task.py remove -h
usage: task.py remove [-h] date
positional arguments:
date Removes the task with the specified date
optional arguments:
-h, --help show this help message and exit
$ python3 task.py show -h
usage: task.py show [-h] date
positional arguments:
date Shows the task of the specified date
optional arguments:
-h, --help show this help message and exit
To create a new task, pass a title, an optional tag, and a message:
$ python3 task.py create \
"Participate in a Python exercise" python "Write a task.py script"
Creating task:
title: Participate in a Python exercise
tag: python
msg: Write a task.py script
Wrote 2020-12-11T11:33:40.595956.task
The list
subcommand just enumerates and lists your tasks:
$ python3 task.py list
Available Tasks:
1: 2020-12-11T11:33:40.595956
Participate in a Python exercise
['python']
More ambitious users can pass an optional tag to list only tasks which contains this tag.
To edit a task, you need the date (you get it from the list
subommand. Depending on what you want to change, you can pass a title, a tag, or a message. For example, to change the title:
$ python3 task.py edit --message "Hiho" 2020-12-11T11:33:40.595956
Changed message
If you pass an date which does not contain a matching filename, the script will show you an error message:
$ python3 task.py edit --message "Hiho" unknown
[Errno 2] No such file or directory: '/home/toms/.config/task/tasks/unknown.task'
The exit code is != 0.
This should be straightforward. You pass a date and it removes the task from your directory.
$ python3 task.py remove 2020-12-11T11:33:40.595956
- Start with the CLI parser and a
parsecli
function. - Read the Python documentation about the
argparse
module. - Use docstrings! :-) You probably can't write doctests for all functions you write.
- Distinguish between output and return code. Any error messages must be written to stderror, normal messages to stdout.
- Name your command functions
cmd_<TASKCOMMAND>
to make it consistent and to distinguish it from other functions. - If you don't know a solution, research on StackOverflow, your favorite search engine, or in the python channel in RocketChat.
- Research if there is a standard module available before you write your own implementation.