开发者

How to store a multi dimensional array inside Google App Engine datastore

class Matrix(db.Model):
 values = db.ListProperty()
开发者_开发问答
obj = Matrix()

wx = [[1,0],[0,1]]

obj.put()

how to store wx matrix inside datastore ?


You will want to serialize your matrix. How you should serialize the data depends on whether or not you are going to query based on data in the matrix.

If you are not going to query, just use JSON (or something similar).

from django.utils import simplejson as json

class Matrix(db.Model):
 values = db.StringProperty(indexed=False)

matrix = Matrix()
# will be a string like: '[[1, 0], [0, 1]]'
matrix.values = json.dumps([[1,0],[0,1]])
matrix.put()

# To get back to the matrix:
matrix_values = json.loads(matrix.values)

If you are going to try querying for matrices that contain an 'exact row', then you might want to do something like:

class Matrix(db.Model):
 values = db.ListProperty()

matrix = Matrix()
values = [[1,0],[0,1]]
# will be a list of strings like:  ['1:0', '0:1']
matrix.values = [':'.join([str(col) for col in row]) for row in values]
matrix.put()

# To get back to the matrix:
matrix_values = [[int(col) for col in row.split(':')] for row in matrix.values]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜