0

I have a task where I need to run certain functions based on a text file to count certain text. The first set of instructions I was able to do myself as it is not user based and it is just a general count of text items in the text file (tasks.txt). My problem I am having now is how do I calculate certain things from a text file based on user only. There is currently only one task but if I had more than one task assigned and to a different user how would I do the calculations for that specific user only? Any advice would really be appreciated.

The Text File is: (tasks.txt)

User assigned to task:

bobby

Task Title :

Fly

Task Description:

Fly to the moon

Task Due Date: 2020-04-01

Date Assigned: 2020-03-03

Task Completed:

No

Task number:

1

1st set of tasks: (completed)

The total number of tasks that have been generated

The total number of completed tasks.

The total number of uncompleted tasks.

The total number of tasks that haven’t been completed and that are overdue.

The percentage of tasks that are incomplete.

The percentage of tasks that are overdue. 

2nd set of tasks (User Based):

The total number of tasks assigned to that user.

What percentage of the total number of tasks have been assigned to that user?

What percentage of the tasks assigned to that user have been completed?

What percentage of the tasks assigned to that user must still be completed?

What percentage of the tasks assigned to that user have not yet been completed and are overdue? 

My code so far:

data2 = open('tasks.txt').read()
    count3 = data2.count('Task Title')
    count4 = data2.count('Yes')
    count5 = data2.count('No')

    with open("tasks.txt", "r")as f5:
        today = datetime.datetime.today()
        overdue = 0
        for line in f5:
            if not line.startswith('Task Due Date'): continue
            field, value = line.split(':')
            if field == 'Task Due Date':
                if datetime.datetime.strptime(value.strip(), '%Y-%m-%d') < today:
                    overdue = overdue + 1
            ab = (overdue/count3)*100
            abb = (count5/count3)*100
            print("Total number of tasks: " + str(count3) + "\nTotal number of completed tasks: " + str(count4) + "\nTotal number of incomplete tasks: " + str(count5) + "\n" +
            "The percentage of overdue tasks is: " + str(ab) + "%" + "\n" + "The percentage of incomplete jobs is: " + str(abb) + "%")
            num_lines = sum(1 for line in open('user.txt'))
            print("The number of registered users is: " + str(num_lines) + "\n")
            data3 = open('tasks.txt','r').read()


            usr_check = input("Please input a user name to write details of that user.\n")
            count6 = data3.count(usr_check)
            count7 = (count6/count3)*100
            print("The user " + str(usr_check) + " has total tasks of: " + str(count6) + "\n" + str(usr_check)+ "'s" + " " + "percentage of total tasks is: "
            + str(count7) + "%")
NoobDev88
  • 23
  • 6

1 Answers1

0

You can first read the file, create Objects (class Task), and for each task create a new class, append it into an array with these object.

An object should contain

  • user
  • title
  • dates

And every important information, so later you can work with them easily.

Then you can run through that list and count the number of task (yes, a len() is a better way), you can create a dictionary, and dinamically add keys (users that is assigned to a task) and count how many tasks correspond to a specific user, and all the process you need to get done.

Whats better is in the dictionary, you could use a normal list to contain the objects correspond to a user, so you can count that list, you can group objects by being done or nor, and so on.

Specific data structures is easier to work with. I recommend using classes for tasks.

nagyl
  • 1,644
  • 1
  • 7
  • 18