def numpy_to_blob(p):
    p = p.view(numpy.uint8)
    b = pmt.make_blob(len(p))
    pmt.blob_data(b)[:] = p
    return b


THE_TABLE = (  #python type, check pmt type, to python, from python
    (None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL),
    (bool, pmt.is_bool, pmt.to_bool, pmt.from_bool),
    (str, pmt.is_symbol, pmt.symbol_to_string, pmt.string_to_symbol),
    (unicode, lambda x: False, None,
     lambda x: pmt.string_to_symbol(x.encode('utf-8'))),
    (int, pmt.is_integer, pmt.to_long, pmt.from_long),
    (long, pmt.is_uint64, lambda x: long(pmt.to_uint64(x)), pmt.from_uint64),
    (float, pmt.is_real, pmt.to_double, pmt.from_double),
    (complex, pmt.is_complex, pmt.to_complex, pmt.from_complex),
    (tuple, pmt.is_tuple, pmt_to_tuple, pmt_from_tuple),
    (list, pmt.is_vector, pmt_to_vector, pmt_from_vector),
    (dict, pmt.is_dict, pmt_to_dict, pmt_from_dict),
    (numpy.ndarray, pmt.is_blob, pmt.blob_data, numpy_to_blob),
)


def pmt_to_python(p):
    for python_type, pmt_check, to_python, from_python in THE_TABLE:
        if pmt_check(p): return to_python(p)
    return p  #give up, we return the same

Exemple #2
0
def uvector_to_numpy(uvector):
	match = None
	for test_func in uvector_mappings.keys():
		if test_func(uvector):
			match = uvector_mappings[test_func]
			return numpy.array(match[0](uvector), dtype = match[1])
	else:
		raise ValueError("unsupported uvector data type for conversion to numpy array %s"%(uvector))

type_mappings = ( #python type, check pmt type, to python, from python
    (None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL),
    (bool, pmt.is_bool, pmt.to_bool, pmt.from_bool),
    (str, pmt.is_symbol, pmt.symbol_to_string, pmt.string_to_symbol),
    (unicode, lambda x: False, None, lambda x: pmt.string_to_symbol(x.encode('utf-8'))),
    (int, pmt.is_integer, pmt.to_long, pmt.from_long),
    (long, pmt.is_uint64, lambda x: long(pmt.to_uint64(x)), pmt.from_uint64),
    (float, pmt.is_real, pmt.to_double, pmt.from_double),
    (complex, pmt.is_complex, pmt.to_complex, pmt.from_complex),
    (tuple, pmt.is_tuple, pmt_to_tuple, pmt_from_tuple),
    (list, pmt.is_vector, pmt_to_vector, pmt_from_vector),
    (dict, pmt.is_dict, pmt_to_dict, pmt_from_dict),
    (numpy.ndarray, pmt.is_uniform_vector, uvector_to_numpy, numpy_to_uvector),
)

def pmt_to_python(p):
    for python_type, pmt_check, to_python, from_python in type_mappings:
        if pmt_check(p): return to_python(p)
    raise ValueError("can't convert %s type to pmt (%s)"%(type(p),p))

def python_to_pmt(p):
    for python_type, pmt_check, to_python, from_python in type_mappings: