AEA
AEA

Reputation: 223

Two functions calling Time lib causing error, workaround possible?

I have two pieces of code in one script which individually work fine, however because they are both calling the "time" library within the same script, I am receiving the error

"TypeError: 'module' object is not callable"

However running them individually (commenting out the other) yields two working pieces of code. An example of the code is enclosed below: Note the comments for how to make each part of the code work.

from sched import scheduler
from time import time, sleep

## If you comment out this line then run periodically will work fine
import time

import datetime
import random

s = scheduler(time, sleep)
random.seed()

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval + random.randrange(-5, 10)
    s.run()

def pubdate():
    return '{pubdate} {pubtime}'.format(
        pubdate = datetime.date.today().strftime("%d %B %Y"),
        pubtime = time.strftime('%H:%M')
    )

## If you comment out this print the run periodically will work fine
print("""<pubDate>%s</pubDate>""" % (pubdate()))

def runme():
    print "example prints/code"

runme()

## If you comment out this line the pubdate works fine
#run_periodically(time()+5, time()+1000000, 10, runme)

I wondered what work around is possible, to enable to get this code to work with both functions in the same script. Kind regards AEA

Upvotes: 0

Views: 61

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122152

You are rebinding time to the module:

from time import time, sleep

import time

The second line replaces the time you imported first. Pick one style of importing from this module and stick to it.

import time

s = scheduler(time.time, time.sleep)

# ...
    pubtime = time.strftime('%H:%M')

or use

from time import time, sleep, strftime

s = scheduler(time, sleep)

# ...
    pubtime = strftime('%H:%M')

Upvotes: 4

Related Questions