#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Why this loop doesn't work as a class's method?


    Well, i wrote a simple loop that works with ctime() function just to get something to repeat every-second.

    Code:
    def sec_loop():
        c_time = 0
        while True:
            if c_time != time.ctime():
                print time.ctime()
                c_time = time.ctime()
    Then i felt like turning it into a class's method just to make things easy:

    Code:
    class CTimeLoop:
        
        def __init__(self, n):
            self.loop_ntimes = n
    
    
        def sec_loop(self):
            c_time = 0
            while self.loop_ntimes > 0:
                if c_time != time.ctime():
                    print time.ctime()
                    c_time = time.ctime()
                    self.loop_ntimes = self.loop_ntimes - 1
    It works great in the first example, returning time.ctime() each second, the problem is, when i use it as a object method the loop just doesn't work. It returns the time.ctime() string once and then quits.

    WHY?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    67
    Originally Posted by J Dihzy
    the problem is, when i use it as a object method the loop just doesn't work. It returns the time.ctime() string once and then quits.
    The trouble may lie in how you call the method. Seems to work for me both ways (although I slightly modified your first example to avoid an infinite loop):

    Code:
    >>> import time
    >>> time.ctime()
    'Sun Aug 11 14:09:46 2013'
    >>> def sec_loop(times=3):
    	c_time = ''
    	while times > 0:
    		if c_time != time.ctime():
    			print(time.ctime())
    			c_time = time.ctime()
    			times -= 1
    
    			
    >>> sec_loop()
    Sun Aug 11 14:14:09 2013
    Sun Aug 11 14:14:10 2013
    Sun Aug 11 14:14:11 2013
    >>> class CTimeLoop(object):
    	def __init__(self, times=3):
    		self.loop_times = times
    	def sec_loop(self):
    		c_time = ''
    		while self.loop_times > 0:
    			if c_time != time.ctime():
    				print(time.ctime())
    				c_time = time.ctime()
    				self.loop_times -= 1
    
    				
    >>> o = CTimeLoop()
    >>> o.sec_loop()
    Sun Aug 11 14:17:53 2013
    Sun Aug 11 14:17:54 2013
    Sun Aug 11 14:17:55 2013
    >>>
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by SuperOscar
    The trouble may lie in how you call the method. Seems to work for me both ways (although I slightly modified your first example to avoid an infinite loop):

    Code:
    >>> import time
    >>> time.ctime()
    'Sun Aug 11 14:09:46 2013'
    >>> def sec_loop(times=3):
    	c_time = ''
    	while times > 0:
    		if c_time != time.ctime():
    			print(time.ctime())
    			c_time = time.ctime()
    			times -= 1
    
    			
    >>> sec_loop()
    Sun Aug 11 14:14:09 2013
    Sun Aug 11 14:14:10 2013
    Sun Aug 11 14:14:11 2013
    >>> class CTimeLoop(object):
    	def __init__(self, times=3):
    		self.loop_times = times
    	def sec_loop(self):
    		c_time = ''
    		while self.loop_times > 0:
    			if c_time != time.ctime():
    				print(time.ctime())
    				c_time = time.ctime()
    				self.loop_times -= 1
    
    				
    >>> o = CTimeLoop()
    >>> o.sec_loop()
    Sun Aug 11 14:17:53 2013
    Sun Aug 11 14:17:54 2013
    Sun Aug 11 14:17:55 2013
    >>>
    For some reason i woke up this morning, tried it and it worked. Maybe i was so asleep last night that i didn't notice something that was obvious...

    Anyway, thank you!!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    your implementation burns the cpu.
    How about using
    time.sleep(1)

    or time.sleep(0.9) in conjunction with your loop.

    For accurate timing, see the rosetta code metronome project.

    rosettacode.org
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo