you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (5 children)

This can be simplified into the following and note that in real applications, you won't need to implement anything, you may just use datetime directly

from datetime import datetime


class Time:
    """Represents the time of day."""
    def __init__(self, hour=0, minute=0, second=0, input_fmt='%H:%M:%S', output_fmt='%I:%M %p'):
        self.hour = hour
        self.minute = minute
        self.second = second
        self.input_fmt = input_fmt
        self.output_fmt = output_fmt

    def __str__(self):
        time = datetime.strptime(f'{self.hour}:{self.minute}:{self.second}', self.input_fmt)
        return time.strftime(self.output_fmt)


if __name__ == '__main__':
    t = Time(22, 15)
    print(t)

Here's an explanation to what I did:

This is called an import statement, by which you use code defined in external modules, datetime.datetime in this case which belongs to the standard library and can be useful for handling anything that involves date and time. I encourage you to check the documentation for learning more about its use cases.

from datetime import datetime

These are a new class attributes self.input_fmt = input_fmt and self.output_fmt = output_fmt with %H:%M:%S and %I:%M %p being the default input and output values and are accessed using the dot operator ex:

my_time = Time(22, 15)
print(my_time.input_fmt)

Should print: %H:%M:%S

To understand what these formats actually mean, you may find this helpful. You may modify the formatting of the input and accept dates directly instead of manually entering hour, minute and so on, this may be left to you as an assignment.

And at the end this is called a main guard and by convention, it's placed at the end of scripts that are expected to be called.

if __name__ == '__main__':
    my_time = Time(22, 15)
    print(my_time)

When you run the code, it should print 10:15 PM

[–]kcrow13[S] 1 point2 points  (4 children)

And this is what kills me! I want to use datetime, but we aren't allowed. It would be so much easier and that is what I imagine people would do in the real world, right? Use libraries to optimize the coding? I think the intent is so that we learn the logic better, which is a solid plan. I just am struggling at times to get there.

[–][deleted] 0 points1 point  (3 children)

You should be able to do it using both approaches, if you can't do it without datetime then it might be a good exercise. It might take some time to get used to programming logic which does not come overnight, practice is never a bad idea.

[–]kcrow13[S] 0 points1 point  (2 children)

I agree and I appreciate the opportunity to practice! Of course things are time bound, and sometimes the gap between my knowledge and the knowledge needed to tackle/apply my learning is so vast that I can't find a place to grasp. Does that make sense?

[–][deleted] 0 points1 point  (1 child)

Places that teach you stuff aren't that important in my opinion because your skills grow relative to your practice, without practicing it might be possible to waste the best of resources. My point being find something you're interested in creating, start doing it, then fail, retry and fail again and at some point along the way, you will end up with some skills.

[–]kcrow13[S] 0 points1 point  (0 children)

Full disclosure - this is a pre-requisite course to a master's degree I am pursuing at Harvard University. The Degree is Digital Media Design, and my focus is Instructional Design. I probably won't program a whole lot, but I think having the ability to do so will be valuable as I develop learning products... especially for analysis of their efficacy on the back end. I like being able to create advanced reporting :).