views:

67

answers:

2

Greetings!

I am trying to work with a single column in the datatstore, I can view and display the contents, like this:

q = test.all()
q.filter("adjclose =", "adjclose")
q = db.GqlQuery("SELECT * FROM test")

results = q.fetch(5)
for p in results:
    p1 = p.adjclose     
    print "The value is --> %f" % (p.adjclose)

however i need to calculate the historical values with the adjclose column, and I am not able to get over the errors

for c in range(len(p1)-1):
    TypeError: object of type 'float' has no len()

here is my code!

for c in range(len(p1)-1):
    p1.append(p1[c+1]-p1[c]/p1[c])
    p2 = (p1[c+1]-p1[c]/p1[c])
    print "the p1 value<--> %f" % (p2)
    print "dfd %f" %(p1)

new to python, any help will be greatly appreciated!

thanks in advance

Ray

HERE IS THE COMPLETE CODE

class CalHandler(webapp.RequestHandler):

    def get(self):
        que = db.GqlQuery("SELECT * from test")
        user_list = que.fetch(limit=100)

        doRender(
            self,
            'memberscreen2.htm',
            {'user_list': user_list} )


q = test.all()
q.filter("adjclose =", "adjclose")
q = db.GqlQuery("SELECT * FROM test")

results = q.fetch(5)
for p in results:
    p1 = p.adjclose     
    print "The value is --> %f" % (p.adjclose)
    for c in range(len(p1)-1):
        p1.append(p1[c+1]-p1[c]/p1[c])
        print "the p1 value<--> %f" % (p2)
        print "dfd %f" %(p1)
A: 

I would do this:

for c in xrange(p1-1)

Essentially, it sounds like p1 already contains the len, so you can just use it directly. Also, note that using the xrange function is usually a bit faster than range. The reason being that xrange returns an iterator while range builds a list. In other words, if you did range(1000000), it would create a list of a million integers!

Jason Baker
A: 

The error is telling you that the value bound to p1 doesn't have a length. That makes sense, because it's a float (also in the error message).

I suspect that what you meant to type was p instead of p1. This is a great example of why you shouldn't use meaningless variable names. This would be a lot easier to spot if the names were something like current_result (instead of p) and current_adjclose instead of p1.

It looks like you might be attempting to do some sort of transformation in your inner loop (it's not 100% clear because of the variable names). If so, that's crying out for you to use a list comprehension.

Also, it looks like you are possibly trying to reuse variables; don't. There's not a shortage of them, after all, and it makes it hard to choose meaningful variable names.

Hank Gay