matel
matel

Reputation: 485

python one more stupid debug

sorry, i am a bit of a pain but i damaged my code, i cannot understand what is wrong. I just removed a if statement but now it appears the timedelta is not recognized anymore and it breaks the code. I am pretty sure i havent removed any of the reference though. I am scratching my head but cannot find what is the problem..

Would you know what went wrong?

import random
import datetime
import csv
from itertools import groupby



def generator():

    i=0
    while 1:
        yield random.randint(-1, 1), datetime.datetime.now()
        i=i+1


def keyfunc(timestamp,interval):
    xt = datetime.datetime(2013, 4,4)
    dt=timestamp
    delta_second =(dt - xt).seconds
    normalize_second = (delta_second / (interval*60)) * (interval*60)
    newtime = xt + timedelta(seconds=normalize_second)
    return newtime


mynumber = 100
for random_number, current_time in generator():
    mynumber += random_number
    reftime5min = keyfunc(current_time,5)


print mynumber,",", current_time, reftime5min

the error i get now is:

Traceback (most recent call last):
  File "", line 35, in 
  File "", line 28, in keyfunc
NameError: global name 'timedelta' is not defined

Upvotes: 1

Views: 180

Answers (2)

John Szakmeister
John Szakmeister

Reputation: 46992

Change timedelta to datetime.timedelta. You're not importing the timedelta class directly, so you need to use the qualified name.

Upvotes: 4

Jakub M.
Jakub M.

Reputation: 33817

from datetime import timedelta

http://docs.python.org/2/library/datetime.html

Upvotes: 1

Related Questions