1

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
1
  • Please do indent your code properly in your question. It's perhaps not relevant in this case, but makes it very hard to read otherwise. Commented Apr 4, 2013 at 10:22

2 Answers 2

4

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

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

Comments

1
from datetime import timedelta

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

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.