Example of building SciDB array with literals for SciDB-R, SciDB-py

The following example from SciDB documentation builds a SciDB array with literals:

iquery -aq "build(< a1 : double, a2 : string > [r=0:3,100,0, c=0:0,100,0], 
          '[  [ (1, \'One\')] , 
              [ (2, \'Two\')] , 
              [ (3, \'Three\')] , 
              [ (4, \'Four\')]  
            ]',
          true)"

The output is:

{r,c} a1,a2
{0,0} 1,'One'
{1,0} 2,'Two'
{2,0} 3,'Three'
{3,0} 4,'Four'

SciDBR

In SciDB-R, you need to add extra escape characters:

a = scidb("build(< a1 : double, a2 : string > [r=0:3,100,0, c=0:0,100,0], 
          '[  [ (1, \\'One\\')] , 
              [ (2, \\'Two\\')] , 
              [ (3, \\'Three\\')] , 
              [ (4, \\'Four\\')]  
            ]',
          true)")

SciDB-Py

Problem

Given the list of values and associated value ids:

vals = ('Jack Sparrow', 'Hematology', 'val3', 'Jan 3, 2019')
val_ids = [5, 6, 15, 8]

I want to generate the following build literal query in Python

build(<metadata_value:string,metadata_value_id:int64>[idx=0:4], '[ (\'Jack Sparrow\',5),(\'Hematology\',6),(\'val3\',15),(\'Jan 3, 2019\',8) ]', true)

Checking the AFL string

iquery -aq "build(<metadata_value:string,metadata_value_id:int64>[idx=0:4], '[ (\'Jack Sparrow\',5),(\'Hematology\',6),(\'val3\',15),(\'Jan 3, 2019\',8) ]', true)"
# {idx} metadata_value,metadata_value_id
# {0} 'Jack Sparrow',5
# {1} 'Hematology',6
# {2} 'val3',15
# {3} 'Jan 3, 2019',8

Solution

vals = ('Jack Sparrow', 'Hematology', 'val3', 'Jan 3, 2019')
val_ids = [5, 6, 15, 8]
xx = ["(\\'{0}\\', {1})".format(vals[idx], val_ids[idx]) for idx in range(0, len(vals))]
q3 = '''build(<metadata_value:string,metadata_value_id:int64>[idx=0:{0}],
        '[{1}]', true)'''. \
    format(len(vals),
           ','.join(xx)). \
    replace('\n', '')

from scidbpy import connect
db = connect()
db.iquery(q3, fetch=True)