1

currently I am getting errors in python but I cannot seem to find them

def dictionaryObjectParsed():
    a = []
    b = []
    a, b = zip(*(map(lambda x: x.rstrip('\n\r').split('\t'), open('/Users/settingj/Desktop/NOxMultiplier.csv').readlines())))
    for x in range(0,len(a)):
        print a[x]
        print b[x]

def timer(f):
    threading.Timer(1, timer, f).start()
    print time.strftime('%I:%M:%S %p %Z')

timer(dictionaryObjectParsed)

Heres the error I'm getting

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 756, in run
    self.function(*self.args, **self.kwargs)
TypeError: timer() argument after * must be a sequence, not function

I was able to do this earlier but I think I did something to create this error, what the heck :(

I clearly am passing arguments to the timer function ... right?

EDIT

I also tried timer(dictionaryObjectParsed) but nothing...

Also, sorry for the noobie question this is just my second day in python... :P

9
  • 1
    Show the full traceback and double-check that the code you posted here is identical with the code that produces said traceback. Commented Aug 13, 2013 at 17:38
  • dictionaryObjectParsed() actually returns None, so one argument is being passed to timer. Commented Aug 13, 2013 at 17:39
  • Ca you explain what you hope this line will do: threading.Timer(1,timer).start() ? Are you trying to do a time-delayed recursion of some kind? Commented Aug 13, 2013 at 17:43
  • Yes, I am attempting to call dictionaryObjectParsed() every second with the time printed after Commented Aug 13, 2013 at 17:43
  • Oh, that's not what that line does. I'll post an answer. Commented Aug 13, 2013 at 17:44

3 Answers 3

3

Pass the function without calling it (drop the '()')..

timer(dictionaryObjectParsed)

and

def timer(f):
    threading.Timer(1,f).start()
    print time.strftime('%I:%M:%S %p %Z')

instead of

threading.Timer(1,timer)

You are trying to create a recursive timer function, I think, by mistake. The error you are getting, is calling the function 'timer' again, without the function parameter. I think it was a simple mistake.


Ok, so you do want a recursive function, so try this:

def timer(f):
    threading.Timer(1,timer,[f,]).start()
    f()
    print time.strftime('%I:%M:%S %p %Z')

worked?

Sign up to request clarification or add additional context in comments.

3 Comments

i tired that, no fix :(
found another detail, maybe now? edit your question with the error you are getting anyway...
this fixes the crash issue but now timer isn't being called again after a second, the function is only being outputted once
0

The actual syntax of instantiating a Timer Instance is

threading.Timer(interval, function, args=[], kwargs={})

There are 2 problems in your implementation

  1. You are registering a function that accepts 1 argument without passing any argument to your registered function
  2. You are recursively registering the caller function in the timer routine.

I believe, your intention is to register the function argument rather than the caller function. That would eventually change your implementation to

def timer(f):
    threading.Timer(1,f).start()
    print time.strftime('%I:%M:%S %p %Z')

But due to some odd reasons, you want to register the caller function, you need to also pass the argument as a parameter to threading.Timer, conforming to the syntax in the documentation

def timer(f):
    threading.Timer(1,timer, f).start()
    print time.strftime('%I:%M:%S %p %Z')

Comments

0

You have multiple errors.

Try this:

def timer(f):
    f()                           # NOTE THIS NEW LINE
    threading.Timer(1,timer, f).start()  # NOTE CHANGE ON THIS LINE
    print time.strftime('%I:%M:%S %p %Z')

timer(dictionaryObjectParsed)     # NOTE CHANGE ON THIS LINE

Note that on the final line, you want to pass the function, not the result of the invoking the function.

Note that on the line threading.Timer ..., you want to pass enough arguments that subsequent invokations of timer() have the correct number of args.

Note the new line -- without it, dictionaryObjectParsed will never be invoked!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.