def compose_arrays(a1, a2, firstfield='etag'):
    """
    Compose composite arrays by generating an extended datatype containing
    all the fields. The two arrays must have the same length.
    """
    assert len(a1) == len(a2),  (len(a1), len(a2))
    if a1.dtype.names is None and len(a1.shape) == 1:
        # the first array is not composite, but it is one-dimensional
        a1 = numpy.array(a1, numpy.dtype([(firstfield, a1.dtype)]))

    fields1 = [(f, a1.dtype.fields[f][0]) for f in a1.dtype.names]
    if a2.dtype.names is None:  # the second array is not composite
        assert len(a2.shape) == 2, a2.shape
        width = a2.shape[1]
        fields2 = [('value%d' % i, a2.dtype) for i in range(width)]
        composite = numpy.zeros(a1.shape, numpy.dtype(fields1 + fields2))
        for f1 in dict(fields1):
            composite[f1] = a1[f1]
        for i in range(width):
            composite['value%d' % i] = a2[:, i]
        return composite

    fields2 = [(f, a2.dtype.fields[f][0]) for f in a2.dtype.names]
    composite = numpy.zeros(a1.shape, numpy.dtype(fields1 + fields2))
    for f1 in dict(fields1):
        composite[f1] = a1[f1]
    for f2 in dict(fields2):
        composite[f2] = a2[f2]
    return composite
def compose_arrays ( a1 , a2 , firstfield = 'etag' ) : assert len ( a1 ) == len ( a2 ) , ( len ( a1 ) , len ( a2 ) ) if a1 . dtype . names is None and len ( a1 . shape ) == 1 : a1 = numpy . array ( a1 , numpy . dtype ( [ ( firstfield , a1 . dtype ) ] ) ) fields1 = [ ( f , a1 . dtype . fields [ f ] [ 0 ] ) for f in a1 . dtype . names ] if a2 . dtype . names is None : assert len ( a2 . shape ) == 2 , a2 . shape width = a2 . shape [ 1 ] fields2 = [ ( 'value%d' % i , a2 . dtype ) for i in range ( width ) ] composite = numpy . zeros ( a1 . shape , numpy . dtype ( fields1 + fields2 ) ) for f1 in dict ( fields1 ) : composite [ f1 ] = a1 [ f1 ] for i in range ( width ) : composite [ 'value%d' % i ] = a2 [ : , i ] return composite fields2 = [ ( f , a2 . dtype . fields [ f ] [ 0 ] ) for f in a2 . dtype . names ] composite = numpy . zeros ( a1 . shape , numpy . dtype ( fields1 + fields2 ) ) for f1 in dict ( fields1 ) : composite [ f1 ] = a1 [ f1 ] for f2 in dict ( fields2 ) : composite [ f2 ] = a2 [ f2 ] return composite
Compose composite arrays by generating an extended datatype containing
    all the fields. The two arrays must have the same length.