-1

I created this small python file to time how much time I spend practicing coding, it all worked perfectly until I attempted to turn all the functions I had into a class, once I did that I can't seem to get it to work, I have tried to add self to the parameters of my functions and it hasn't worked. below is the code

import time

class Timer:
    def time_convert(second, text):
        minute = second // 60
        second = second % 60
        hour = minute // 60
        minute = minute % 60
        print(text, " = {0}:{1}:{2}\n".format(int(hour), int(minute), second))

    def timer_calc( start_time, end_time):
        time_lapsed = end_time - start_time
        return time_lapsed

    def file_writer(file_name, file_contents):
        file = open(file_name, "w")
        file.flush()
        file.write(file_contents)
        file.close()

    def file_reader(file_name):
        file = open(file_name, "r")
        file_contents = file.read()
        file.close()
        return file_contents

    def timer_main(file_name):
        previous_time = file_reader(file_name)
        input("Press Enter to start")
        start_time = time.time()
        input("Press Enter to stop")
        end_time = time.time()
        seconds_coding = timer_calc(start_time, end_time)
        total_time = float(previous_time) + seconds_coding
        file_writer(file_name, str(total_time))

        time_convert(seconds_coding, "Current Coding Time")
        time_convert(total_time, "Total Coding Time")
        total_coding_time()

    def total_coding_time(self):
        python = file_reader("TimeSpentPython.txt")
        tod = file_reader("TimeSpentTOD.txt")
        cs50 = file_reader("TimeSpentCS50.txt")

        total = float(python) + float(tod) + float(cs50)
        time_convert(total, "The complete total coding hours are")
        time.sleep(5)

    def main(self):
        while True:
            user_input = input("what are you going to program on ? \npython (1)\n"
                               "The Odin Project (2)\nCS50 (3)\nOr Exit (4)\n")
            if user_input == "1":
                timer_main("TimeSpentPython.txt")
            elif user_input == "2":
                timer_main("TimeSpentTOD.txt")
            elif user_input == "3":
                timer_main("TimeSpentCS50.txt")
            elif user_input == "4":
                exit(0)
            else:
                print("please enter a valid choice")

if __name__ == "__main__":
    timer = Timer()
    timer.main()

The error shown in my IDE tells me that there are no such methods whenever I try to call a method from a different method inside the class.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Rios
  • 71
  • 1
  • 6
  • 2
    How to call a static method: https://stackoverflow.com/questions/11759269/calling-static-method-in-python. Also please include the exact error trace in the question – sshashank124 Jan 13 '20 at 02:29
  • *"The error shown in my IDE"* - Please turn off your firewall so we can see it, too. – Kelly Bundy Jan 13 '20 at 02:34
  • There's no reason for `Timer` to exist, as `Timer` doesn't encapsulate any data. All your "methods" should just be regular functions. – chepner Jan 13 '20 at 12:18

2 Answers2

0

You need to use self a lot more often. A self parameter inside every method definition, and a self before you calling every method.

-------------------------------------------------------------------------------

import time

class Timer:
    def time_convert(second, text):
        minute = second // 60
        second = second % 60
        hour = minute // 60
        minute = minute % 60
        print(text, " = {0}:{1}:{2}\n".format(int(hour), int(minute), second))

    def timer_calc(self, start_time, end_time):
        time_lapsed = end_time - start_time
        return time_lapsed

    def file_writer(self, file_name, file_contents):
        file = open(file_name, "w")
        file.flush()
        file.write(file_contents)
        file.close()

    def file_reader(self, file_name):
        file = open(file_name, "r")
        file_contents = file.read()
        file.close()
        return file_contents

    def timer_main(self, file_name):
        previous_time = self.file_reader(file_name)
        input("Press Enter to start")
        start_time = time.time()
        input("Press Enter to stop")
        end_time = time.time()
        seconds_coding = self.timer_calc(start_time, end_time)
        total_time = float(previous_time) + seconds_coding
        self.file_writer(file_name, str(total_time))

        time_convert(seconds_coding, "Current Coding Time")
        time_convert(total_time, "Total Coding Time")
        total_coding_time()

    def total_coding_time(self):
        python = self.file_reader("TimeSpentPython.txt")
        tod = self.file_reader("TimeSpentTOD.txt")
        cs50 = self.file_reader("TimeSpentCS50.txt")

        total = float(python) + float(tod) + float(cs50)
        self.time_convert(total, "The complete total coding hours are")
        time.sleep(5)

    def main(self):
        while True:
            user_input = input("what are you going to program on ? \npython (1)\n"
                               "The Odin Project (2)\nCS50 (3)\nOr Exit (4)\n")
            if user_input == "1":
                self.timer_main("TimeSpentPython.txt")
            elif user_input == "2":
                timer_main("TimeSpentTOD.txt")
            elif user_input == "3":
                timer_main("TimeSpentCS50.txt")
            elif user_input == "4":
                exit(0)
            else:
                print("please enter a valid choice")

if __name__ == "__main__":
    timer = Timer()
    timer.main()
Community
  • 1
  • 1
Jiaju Huang
  • 79
  • 1
  • 2
  • There's no reason to add `self` just to make the class work. If you don'ts `self`, the method should be a function. If *all* the methods should be functions, you should get rid of the class altogether. – chepner Jan 13 '20 at 12:19
0

You don't have a class. You just have a group of functions.

Only three things have changed in the code below:

  1. I dropped the class and made each method a standalone function.
  2. I dropped the self parameter from total_coding_time.
  3. I dropped the instantiation of the class and call main directly.

import time

def time_convert(second, text):
    minute = second // 60
    second = second % 60
    hour = minute // 60
    minute = minute % 60
    print(text, " = {0}:{1}:{2}\n".format(int(hour), int(minute), second))

def timer_calc( start_time, end_time):
    time_lapsed = end_time - start_time
    return time_lapsed

def file_writer(file_name, file_contents):
    file = open(file_name, "w")
    file.flush()
    file.write(file_contents)
    file.close()

def file_reader(file_name):
    file = open(file_name, "r")
    file_contents = file.read()
    file.close()
    return file_contents

def timer_main(file_name):
    previous_time = file_reader(file_name)
    input("Press Enter to start")
    start_time = time.time()
    input("Press Enter to stop")
    end_time = time.time()
    seconds_coding = timer_calc(start_time, end_time)
    total_time = float(previous_time) + seconds_coding
    file_writer(file_name, str(total_time))

    time_convert(seconds_coding, "Current Coding Time")
    time_convert(total_time, "Total Coding Time")
    total_coding_time()

def total_coding_time():
    python = file_reader("TimeSpentPython.txt")
    tod = file_reader("TimeSpentTOD.txt")
    cs50 = file_reader("TimeSpentCS50.txt")

    total = float(python) + float(tod) + float(cs50)
    time_convert(total, "The complete total coding hours are")
    time.sleep(5)

def main(self):
    while True:
        user_input = input("what are you going to program on ? \npython (1)\n"
                           "The Odin Project (2)\nCS50 (3)\nOr Exit (4)\n")
        if user_input == "1":
            timer_main("TimeSpentPython.txt")
        elif user_input == "2":
            timer_main("TimeSpentTOD.txt")
        elif user_input == "3":
            timer_main("TimeSpentCS50.txt")
        elif user_input == "4":
            exit(0)
        else:
            print("please enter a valid choice")

if __name__ == "__main__":
    main()
chepner
  • 497,756
  • 71
  • 530
  • 681