Re: [google-appengine] Problem putting/retrieving from datastore

52 views
Skip to first unread message
Message has been deleted

Robert Kluin

unread,
Aug 18, 2011, 12:19:00 AM8/18/11
to google-a...@googlegroups.com
Hi,
You should check out the Python logging package, it is super handy.
Add some logging calls to your code so you can see what is happening.
For example:

import logging

def post(self):
stguess = self.request.get('guess')
if stguess == 'new':
answer = Answer(ans = (random.randint(1, 100))).put()
logging.info('Wrote answer, key is: %s', str(answer.key()))
number = Answer.all().get()
if number is not None:
logging.info('Got answer, value is: %d', number.ans)
answer = number.ans
else:
logging.info('Oh No! Got no answer!')
answer = None
else:
logging.info('Doing nothing.')

Robert

On Wed, Aug 17, 2011 at 17:33, Matt <mpiec...@gmail.com> wrote:
> I am trying to pull an integer from the datastore but I keep getting
> None back. I store the integers here-
>
> class Answer(db.Model):
>    ans = db.IntegerProperty()
>
> As a test I did this -
>
> def post(self):
>
>        stguess = self.request.get('guess')
>        if stguess == 'new':
>            answer = Answer(ans = (random.randint(1, 100))).put()
>            number = Answer.all().get()
>            if number is not None:
>                answer = number.ans
>            else:
>                answer = None
>            msg = str(answer)
>
> So what that does is take user input from the user and if they entered
> 'new' follow the if path. ans equals a random integer between 1 and
> 100. Put it in Answer. Then immediately after, pull the first integer
> from Answer. If it is not None assign the variable answer to the
> result, else assign it to None. msg is displayed on screen. Instead of
> a random number which I am expecting to be displayed, None is
> displayed instead.
>
> I also tried using count to see how many items were actually in the
> database and the count was more than 0.
>
> Is there anything wrong with how I am putting or retrieving from the
> datastore? How can I change this so I get the random number into the
> answer variable, not None?
>
> Thanks for any help
>
> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Matt

unread,
Aug 18, 2011, 12:33:38 PM8/18/11
to Google App Engine
Thanks for the heads up on logging.

I tried the logging but don't know if I did it right. For the first
logging part in the code, I put:
logging.info('Wrote answer, key is: %s', str(answer.key()))
and it gave me this in the browser:
AttributeError: 'Key' object has no attribute 'key'

I tried replacing key with ans but that gave the same error type.
When I just put:
logging.info('Wrote answer, key is: %s', str(answer))
the log file comes with this:
Wrote answer, key is agxkZXZ-bnVtZ3Vlc3NyDAsSBkFuc3dlchhUDA

Am I doing something wrong or does this info mean anything? I honestly
don't know what to make of it or what to change to get the random
number in/out.. I feel like I am following what the docs say though.

Robert Kluin

unread,
Aug 18, 2011, 1:07:36 PM8/18/11
to google-a...@googlegroups.com
Hey Matt,
Yeah there was a typo in my same code, should not have been:

logging.info('Wrote answer, key is: %s', str(answer.key()))
but rather:

logging.info('Wrote answer, key is: %s', str(answer))

What that is telling you is that an answer entity was successfully
written. So, now you know that the entity is getting written, what is
the next logging line? Are you getting an entity back from the
datastore?


Robert

Matt

unread,
Aug 18, 2011, 4:16:49 PM8/18/11
to Google App Engine
It doesn't look like it. This is the log

INFO 2011-08-18 20:08:48,221 index.py:30] Wrote answer, key is
agxkZXZ-bnVtZ3Vlc3NyDAsSBkFuc3dlchhWDA
Traceback (most recent call last):
File "C:\Python25\lib\logging\__init__.py", line 744, in emit
msg = self.format(record)
File "C:\Python25\lib\logging\__init__.py", line 630, in format
return fmt.format(record)
File "C:\Python25\lib\logging\__init__.py", line 418, in format
record.message = record.getMessage()
File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage
msg = msg % self.args
TypeError: int argument required
INFO 2011-08-18 20:08:48,299 dev_appserver.py:4247] "POST / HTTP/
1.1" 200 -
INFO 2011-08-18 20:08:48,456 dev_appserver.py:4247] "GET /
numguess.css HTTP/1.1" 200 -


If that means anything I see a TypeError... maybe that's what is
stopping the random number entity to be pulled back out?

Thanks again for the help.

Robert Kluin

unread,
Aug 18, 2011, 11:29:22 PM8/18/11
to google-a...@googlegroups.com
That is an excellent clue. Try changing

logging.info('Got answer, value is: %d', number.ans)
to:
logging.info('Got answer, value is: %s', str(number.ans))


Also, just noticed one other thing. Instead of 'if number is not
None', in Python you can do 'if number' -- much nicer.

Tim Hoffman

unread,
Aug 18, 2011, 11:40:47 PM8/18/11
to google-a...@googlegroups.com


Also, just noticed one other thing.  Instead of 'if number is not
None', in Python you can do 'if number' -- much nicer.


Though not very good if number can be 0 or "" as opposed to None ;-)

I am not sure which reads better

number is not None
or
not number is None
or
or number != None



T

Matt

unread,
Aug 19, 2011, 12:13:09 AM8/19/11
to Google App Engine
Thanks again.

Changed that line and now this appears in the logs:
INFO 2011-08-19 04:02:28,358 index.py:33] Got answer, value is:
None
So it is putting something in and it is None. I am not sure why. I
must not be using .put() correctly to store the random number?
answer = Answer(ans = (random.randint(1, 100))).put()

I have tried variations of the put() such as not assigning a random
number inside this statement then referring to it, but it always comes
up as None.

Tim Hoffman

unread,
Aug 19, 2011, 1:30:17 AM8/19/11
to google-a...@googlegroups.com
Hi

WHen you retrieve the record are you sure your getting the one you think you are ?
Each time you put you create a new record.


You get is returning the first record it finds (key order)
If you wrote some Answer entities early on with no value, then you wont get a value in the Answer object.

Probably should include your current code state, so we can see where you are up to.

T

Robert Kluin

unread,
Aug 19, 2011, 1:31:49 AM8/19/11
to google-a...@googlegroups.com
On Thu, Aug 18, 2011 at 23:40, Tim Hoffman <zute...@gmail.com> wrote:
>
>>
>> Also, just noticed one other thing.  Instead of 'if number is not
>> None', in Python you can do 'if number' -- much nicer.
>
> Though not very good if number can be 0 or "" as opposed to None ;-)

True. But I find that syntax cleaner for an entity. Maybe just a
personal preference though...

>
> I am not sure which reads better
>
> number is not None

I like this one, it is very natural feeling.

> or
> not number is None
> or
> or number != None

None is an instance, iirc. So you're supposed to use 'is' to check
identity, I think. ;)

>
>
>
> T


>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-appengine/-/OZApIUGT9noJ.

Robert Kluin

unread,
Aug 19, 2011, 1:33:57 AM8/19/11
to google-a...@googlegroups.com
Hey Matt,
You should use the datastore viewer and make sure you don't have
some bad data in there. I'm guessing you've got entities where ans is
not set for your earlier testing.

The good news is that you now know how to use logging, so you'll be
able to debug issues like this better in the future.


Robert

Nadun Kulatunge

unread,
Aug 19, 2011, 5:06:43 AM8/19/11
to google-a...@googlegroups.com
wow cool fun stuff << http://j.gs/I9f >> i think you will like it
--
Bye TC

Matt

unread,
Aug 19, 2011, 12:00:49 PM8/19/11
to Google App Engine
Thank you so much.. that was it!!

I cleared the datastore for this app and tried again. Out came a
random number!
It was my mistake must have read the docs wrong because I thought I
had read that .get() returns the most recent record, not the first
that it finds (key order).
Thank you again! I learned a few things that I can utilize in the
future.
Matt
Reply all
Reply to author
Forward
0 new messages