1

I have imported the following modules at the top of my python script:

import os
import sys
import time
import datetime
import random
import pprint
from random import randint
from time import sleep
from datetime import datetime, timedelta

But, I am still getting a module object error for this part of my code:

def check(low,high):
    with open('done.txt', 'r+') as done:
        ts = time.time()
        sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
        done_completed = open('done_completed.txt', "a")
        for line in done:
            now = datetime.now()
            now_time = now.time()
            if now_time >= time(23,30) and now_time <= time(06,30):
                print "sleeping"
                sleep(5000)
            else:
                done_id = line.strip()[20:]
                then = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')

(There's another if elif elif under all that but I figured it isn't relevant to the error)

(Yes, I am a python beginner)

The error is:

  File "/home/joe/Desktop/follomatic/follomatic.py", line 85, in check
if now_time >= time(23,30) and now_time <= time(06,30):
TypeError: 'module' object is not callable

Does anyone see what is wrong? Have I not specified to call the module in the right way?

Thanks :)

joep1
  • 325
  • 4
  • 18
  • `time` is a module, not a function. You probably meant `time.time()`. – grc Feb 19 '15 at 12:04
  • Thanks grc - whereabouts in the code do you mean? now_time = time.time() instead of now_time = now.time() ? – joep1 Feb 19 '15 at 12:19
  • In the line with the error, you have `time(23,30)` and `time(06,30)`. I'm not sure what you were intending, but this doesn't work since `time` is a module. – grc Feb 19 '15 at 12:26
  • @grc: [time.time()](https://docs.python.org/2/library/time.html#time.time) takes no arguments. But [datetime.time()](https://docs.python.org/2/library/datetime.html?highlight=datetime.time#datetime.time) does. Not to be confused with [datetime.datetime.time()](https://docs.python.org/2/library/datetime.html?highlight=datetime.time#datetime.datetime.time). As I said in my answer, it's a bit of a rat's nest. :) – PM 2Ring Feb 19 '15 at 12:53
  • @joep1: Even if you fix the function calls up, this logic isn't going to work with `datetime.time`s : `if now_time >= time(23,30) and now_time <= time(06,30):` That test will never be `True`. You'd need something like `if now_time >= time(23,30) or now_time <= time(06,30):`. – PM 2Ring Feb 19 '15 at 13:23

1 Answers1

2

Python's datetime module is a bit of a rat's nest. :) And it can get even more confusing if you use from imports.

This code should clarify things a bit. Note that this is not a great way to do things. It's generally better to work with datetime.datetime objects rather than datetime.time objects, otherwise things get messy if you need to handle time intervals that include midnight (or multiple days). But I wrote this code to roughly correspond with the code in your question, and I wanted to make it easy to test at any time of day.

    #!/usr/bin/env python

    ''' datetime.time manipulation demo

        From http://stackoverflow.com/q/28605732/4014959

        Written by PM 2Ring 2015.02.19
    '''

    from time import sleep
    import datetime

    now = datetime.datetime.now()
    now_time = now.time()

    #A datetime.time example
    t1 = datetime.time(5, 30)
    print 't1 is', t1

    now = datetime.datetime.now()
    now_time = now.time()
    print 'now is %s, now_time is %s' % (now, now_time)

    begin_datetime = now + datetime.timedelta(seconds=5)
    end_datetime = now + datetime.timedelta(seconds=10)

    begin_time = begin_datetime.time()
    end_time = end_datetime.time()

    print 'begin', begin_time
    print 'end', end_time

    for i in xrange(15):
        now_time = datetime.datetime.now().time()
        print now_time, begin_time <= now_time <= end_time
        sleep(1)

typical output

t1 is 05:30:00
now is 2015-02-19 23:44:39.152786, now_time is 23:44:39.152786
begin 23:44:44.152786
end 23:44:49.152786
23:44:39.152905 False
23:44:40.153999 False
23:44:41.155191 False
23:44:42.156398 False
23:44:43.156614 False
23:44:44.157810 True
23:44:45.159028 True
23:44:46.160231 True
23:44:47.161444 True
23:44:48.162660 True
23:44:49.163869 False
23:44:50.165076 False
23:44:51.166650 False
23:44:52.167842 False
23:44:53.169053 False
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Thanks! Going to take me a while to get my head around this but it seems to work! – joep1 Feb 19 '15 at 14:51
  • Weirdly, my code DOES work if you take it out of the with/for loops... I remembered where I got it originally from the top answer of this post http://stackoverflow.com/questions/10048249/how-do-i-determine-if-current-time-is-within-a-specified-range-using-pythons-da/10048290#10048290 – joep1 Feb 19 '15 at 16:11