Python Forum
Timer class not working as expected.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timer class not working as expected.
#1
Hi folks.

From what I can find on how to compare dates, basically use the < and >, and how to put a class structure together, the following should work. But it doesn't. If I check more than or less than the tripped routine is always returning True. Is this because it's trying to return an object rather than a value??? And why does get dates return ,bound method customer.getsdates blah blah 0x0blahblah?

import datetime
import time


class clstimer:
    def __init__(self, interval):
        self.interval=interval
        self.startdate=datetime.datetime.now()

    def tripped():
        nowdate=datetime.datetime.now()
        checkdate=self.startdate + datetime.timedelta(seconds=self.interval)

        if nowdate>checkdate:
            self.startdate=datetime.datetime.now()
            return True
        else:
            return False

    def getdates():
        nowdate=datetime.datetime.now()
        checkdate=self.startdate + datetime.timedelta(seconds=self.interval)
        return str(nowdate), str(checkdate)
        

def main():
    tmr=clstimer(5)

    print(tmr.getdates)

    while True:
        if tmr.tripped:
            print("Tripped")
        else:
            print("Nope")
        time.sleep(1)


main()
What am I misunderstanding?

Many thanks.
Reply
#2
Hello, first of all, you are missing 'self' argument for methods tripped() and getdates(). And second - if you want to execute the method you need to add parentheses like this:
print(tmr.getdates())
also for tripped method inside your while loop:
tmr.tripped()
otherwise they are just method references.
Reply
#3
But if I put brackets on I get told the defd takes no arguments but I'm supplying 1.

That's what I thought, but it just won't have it.
Reply
#4
Yes, as I wrote you also need to add 'self' argument to methods tripped and getdates.

    def tripped(self):
        nowdate=datetime.datetime.now()
        checkdate=self.startdate + datetime.timedelta(seconds=self.interval)
 
        if nowdate>checkdate:
            self.startdate=datetime.datetime.now()
            return True
        else:
            return False

    def getdates(self):
        nowdate=datetime.datetime.now()
        checkdate=self.startdate + datetime.timedelta(seconds=self.interval)
        return str(nowdate), str(checkdate)
Reply
#5
Ahhhhh, crud. Sorry, I had missed that bit in your explanation.

Lesson lernt. Many thanks. :o)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple conditional not working as expected return2sender 8 934 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Custom method to handle exceptions not working as expected gradlon93 3 941 Dec-22-2022, 07:12 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,030 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  set and sorted, not working how expected! wtr 2 1,254 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  email timer/rss feed timer ndiniz 1 2,040 Feb-02-2021, 07:18 PM
Last Post: nilamo
  append not working as expected teachinggeek 2 2,219 Apr-09-2020, 04:32 AM
Last Post: buran
  What is the strategy for working with class variables? AlekseyPython 3 2,947 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  skeleton class needs working instantiation guard hereathome 3 2,992 Dec-10-2017, 03:35 PM
Last Post: hshivaraj
  How are these methods working within this class workerbee 3 2,885 Nov-29-2017, 04:15 PM
Last Post: hshivaraj
  Code not working as expected. an_droid 4 3,908 Mar-09-2017, 02:14 PM
Last Post: an_droid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020