Skip to content

Git and Gitlab - Beginners, working as a team

During this session, we will address collaborative work with Git and Gitlab. You will hence continue developing the smart todo list application, but this time as a group.

Several on Gitlab

Here, we will place ourselves in real conditions of collaborative work by deepening the concepts of Git. You will be divided according to your PRONTO project group.

  1. As a group, decide one of your Gitlab project to continue development
  2. The owner of the project, will search for and add the rest of the group to this project and give them the Maintainer status so they can contribute equally to it.
  3. Each added group member will now clone this shared repository
  4. When every member of the group has cloned the repository, go to the next section

All the necessary information on Gitlab to carry out these steps can be found here: Gitlab

Conflicts in Git

As we wish to simulate how beginner developers tend to work together, there is a set of instructions related to the work for the rest of the session:

  1. All work must be only commited and pushed on the main default branch
  2. All code developed must be added to the one python file main.py
  3. For each development task, focus on the minimal amount of code to complete the task (no restructuration of the code is to be made)

Now you can split the list of tasks between each other, and start development.

Tip

The git status command is very useful for providing information about the state of a repository and often offers solution hints.

Remember the basic Git workflow: add - commit - push

Does everything seem right?

Development Tasks

To continue developing the smart todo list application, the following set of tasks will need to be completed.

A common format for the todo is to be respected: [ ] todo text description | priority=low | due=none

Task 1 – Persist Todos in a File

Load todos from todos.txt if it exists. Start with an empty list if the file doesn’t exist. Save changes back to the file after each command.

Tip

Use open("todos.txt", "r")and readlines()

Use .strip() to remove newline characters

Handle file-not-found with try / except

Use open("todos.txt", "w") and write

Solution - Click to expand

Add this code section in the file beginning to replace how the todos object is created:

1
2
3
4
5
6
7
8
todos = []
try:
    f = open("todos.txt", "r")
    for line in f:
        todos.append(line.strip())
    f.close()
except:
    todos = []

Then save the todos object to file after each command by adding this code block at the end of the main while loop:

1
2
3
with open("todos.txt", "w") as f:
    for todo in todos:
        f.write(todo + "\n")

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)

Task 2 – Delete a Todo

Implement a delete command: remove a todo by number.

Tip

Convert input to integer: int(input())

Remove todo with list.pop(index)

Save file with open("todos.txt","w")

Solution - Click to expand

Add a new if case with the command delete:

1
2
n = int(input("Number to delete: "))
todos.pop(n-1)

Add new delete command to the list of commands shown at the beginning of the main while loop.

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)

Task 3 – Toggle Done Status

Implement a done command to mark a todo [ ] or [x]. Modify the add command so every todo has the new format.

Tip

Use .startswith("[ ]") and .replace()

Access the correct todo using list[index]

Solution - Click to expand

Add new done command with new if case block:

1
2
3
4
5
n = int(input("Number to mark done: "))
if todos[n-1].startswith("[ ]"):
    todos[n-1] = todos[n-1].replace("[ ]", "[x]", 1)
else:
    todos[n-1] = todos[n-1].replace("[x]", "[ ]", 1)

Modify the code line adding a new todo:

1
todos.append("[ ] " + new_todo + " | priority=none | due=none")

Add new done command to the list of commands shown at the beginning of the main while loop.

Danger

Whenever you change the todo format, you need to delete the todos.txt file as its content is not formated properly. You can also manually adapt it.

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)

Task 4 – Set Priority

Implement priority command to set low, medium, or high. Modify the add command so every todo has the new format.

Tip

Store priority inside the todo string: | priority=low |

Use .split(" | ") to access string parts

Recombine parts after modifying priority

Solution - Click to expand

Add new priority command with new if case block:

1
2
3
4
n = int(input("Number: "))
p = input("Priority (low/medium/high): ")
parts = todos[n-1].split(" | ")
todos[n-1] = parts[0] + " | priority=" + p + " | " + parts[2]

Modify the code line adding a new todo:

1
todos.append("[ ] " + new_todo + " | priority=none | due=none")

Add new priority command to the list of commands shown at the beginning of the main while loop.

Danger

Whenever you change the todo format, you need to delete the todos.txt file as its content is not formated properly. You can also manually adapt it.

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)

Task 5 – Set Due Date

Implement a due command to set a due date: | due=YYYY-MM-DD. Modify the add command so every todo has the new format.

Tip

Use .split(" | ") to access string parts

Modify the third part to include due date

Solution - Click to expand

Add new due command with new if case block:

1
2
3
4
n = int(input("Number: "))
d = input("Due date: ")
parts = todos[n-1].split(" | ")
todos[n-1] = parts[0] + "| " + parts[1] + " | due=" + d

Modify the code line adding a new todo:

1
todos.append("[ ] " + new_todo + " | priority=none | due=none")

Add new due command to the list of commands shown at the beginning of the main while loop.

Danger

Whenever you change the todo format, you need to delete the todos.txt file as its content is not formated properly. You can also manually adapt it.

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)

Task 6 – Redesign Display

Improve list output: add headers, numbering, separators.

Tip

Use print("----- TODOS -----")

Loop with for i in range(len(todos))

End with separator

Solution - Click to expand

Replace list command block with following code:

1
2
3
4
print("----- TODOS -----")
for i in range(len(todos)):
    print(str(i+1) + ". " + todos[i])
print("-----------------")

➡ Push your modification on the main branch on gitlab (with git add, git commit, git push)