2

In my code I am trying to extract some data from a file. I am getting this error when I am trying to run my code on line 61. My code here:

from datetime import date
from math import floor;
from adjset_builder import adjset_builder
def check_and_update(d,start,end,log):    
#    print start,end;
    if start in d:
        if end in d[start]:
            log.write("{0}-{1}\n".format(start, end))
            if d[start][end] == 1:
                print "one found"
            d[start][end] += 1

def build_dictionary(my_adjset,my_list,factor,outfile,log):
    log.write("building dictionary:\n");

    window_size = int(floor(len(my_list)*factor));
    if window_size<2:
        log.write("too small\n")
        return;   
    log.write('Total:{0},windowsize:{1}\n'.format(len(my_list),window_size));
    log.write("Window at place: 0,")
    for i in xrange(window_size):
        j = i+1;
        while j<window_size:
            check_and_update(my_adjset, my_list[i][1], my_list[j][1],log);
            j=j+1

    i=1;
    while i<=len(my_list)-window_size:
        log.write("{0},".format(i)) 
        j=i;
        k=i+window_size-1;
        while j<k:
            check_and_update(my_adjset, my_list[i][1], my_list[j][1],log);  
            j+=1
        i += 1  
    log.write("\nDictionary building done\n")        

def make_movie_list(infilename,factor):
    log=open('log.txt','w');
    outfile=open(infilename.split('.')[0]+"_plot_"+str(factor)+".txt",'w');
    f=open(infilename,'r');
    my_adjset=dict()
    adjset_builder('friends.txt', my_adjset);

    count =1
    while True:        
        string = f.readline();
        if string=='':
            break;        
        log.write("count:{0}\n".format(count))
        count += 1

        [movie,freunde] = string.split('=');
        freunde = freunde.split(';')
        mylist=[]
        for i in freunde:
            [user_id,date] = i.split(' ');
            [yy,mm,dd] = date.split('-');  
#            l=list((date(int(yy),int(mm),int(dd)),user_id))
            mylist.append([date(int(yy),int(mm),int(dd)),user_id]); ## line 61         
        log.write("list built");   
        print mylist         
        break;    
#        build_dictionary(my_adjset, mylist, factor,outfile,log) 
        del(mylist);   

    print 'Done'

    log.close();
    outfile.close();
    f.close();
    print count 


if __name__ == '__main__':    
    make_movie_list('grades_processed.txt',.5)

However when I tried to simulate the same thing in 'Console' I do not get any error:

dd='12'
mm='2'
yy='1991'
user_id='98807'  
from datetime import date
from datetime import date
l=list((date(int(yy),int(mm),int(dd)),user_id))
l [datetime.date(1991, 2, 12), '98807']

Might be something very silly but I am a beginner so can not seem to notice the mistake. Thank you!

2
  • you must have a declared a variable name str somewhere in your code? Commented May 27, 2012 at 16:37
  • Please show the error (including full traceback). Also, line numbers don't really help us. Please clearly mark the line causing the error.
    – Marcin
    Commented May 27, 2012 at 16:45

2 Answers 2

7

This makes date a function:

from datetime import date

This makes date a string:

        [user_id,date] = i.split(' ');

You get a TypeError now, since date is no longer a function:

mylist.append([date(int(yy),int(mm),int(dd)),user_id]); 

One way to avoid this error is to import modules instead of functions:

import datetime as dt
mylist.append([dt.date(int(yy),int(mm),int(dd)),user_id])

or more succinctly,

mylist.append([dt.date(*date.split('-')), user_id])

PS: Remove all those unnecessary semicolons!

5

You have a variable called date, rename it so that it doesn't shadow the date function from datetime.

1
  • 1
    +1 good answer, could be improved with a simple example date = 'bla' and date(int(yy), int(mm), int(dd)) causes the same error.
    – Levon
    Commented May 27, 2012 at 17:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.