How to store a multidimensional array in the Google App Engine data warehouse

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

obj = Matrix()

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

obj.put()

how to store wx matrix inside data store?

+3
source share
1 answer

You will need to serialize your matrix. How you should serialize the data depends on whether you intend to query the data based on the data in the matrix.

If you are not going to request, 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 try to execute a query for matrices containing an "exact row", you can 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]
+5
source

Source: https://habr.com/ru/post/1778561/


All Articles