DocWiki DocWiki - 2 months ago 16
Python Question

Python: Store a num in Google App Engine

I am using Google App Engine Python.

I would like to store a simple variable number somewhere, so I can add, deduct and use this number.

examplenum=5
example+=1


Next time I use this examplenum, it wil become 6.

I know I can build a
Model
and add an
IntegerProperty
varible. But I guess that would be too much hassle because I only need one simple number not a model.

I have also read this question Simplest way to store a value in Google App Engine Python?, the
memcache
solution is not good for me. I want the number to stay forever not just temporary in the
memcache
.

Thanks a lot.

Answer

All the answers to the question you linked are relevent to you. If you want persistance you will have to create a model. Look at using get_or_insert to fetch/initialize an entity and give your entity a key_name so that you can keep fetching it easily whenever you need to store your values.

class MyAppData(db.Model):
    my_number = db.IntegerProperty()

# fetch entity whenever you need to store your value
data = MyAppData.get_or_insert(key_name='mydata', my_number=1)

data.my_number += 1
data.put()

You code does look suspiciously like a counter, and you might want to look at the sharding countering article and the problems that it solves in case they are relevent.