[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] PMT blobs in Python

From: Marcus Müller
Subject: Re: [Discuss-gnuradio] PMT blobs in Python
Date: Sun, 04 Aug 2013 15:29:57 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7

On second thought: Does using pmt.make_vector solve your problem?
in C++:
p = pmt::make_u8vector(length, fill);
(do stuff like pmt::u8vector_set(p, index, value), or even unsigned char* p_vec = pmt::u8vector_writable_elements(p,length). The later can summon hell if you use the same PMT object in multiple places.)

in python:
myvec= pmt.u8vector_elements(p)
Note, however, that this will inherently copy each individual uchar to a python (long) int.

If you really want the raw bytes,  you can always get really dependent on PMT structure (which is basically fine, as long as you know that PMT won't change during the lifetime of your application), and use
in python, as this will give you a string.

Thinking about that, there is yet another option, that suddendly blob's  into mind.
in C++ , generate a std::string, and assign your char data to it:
std::string looks_like_it_s_text_but_is_data s();
s.assign(reinterpret_cast<const char*>(mydata), length);
pmt::pmt_t p = pmt::intern(s);

In python, you can use pmt.to_string(p) to get back the string, in case you want to use
it for a buffer.

PLEASE, be aware that this is NOT a good idea for high data volume or high throughput;
string_to_symbol hashes the string, and saves the hash as well as the pmt_t in a table.

On 08/04/2013 12:08 PM, Marcus Müller wrote:
I can't really test this right now,
but you could insert
  %include <cdata.i>
into your project's swig file[1]; this should generate a python method
   cdata(ptr, length)
that you can feed your blob_data() to. I'm not quite sure that WILL
work as is, maybe you will need a C(++) wrapper that simply returns
an array of known type (that is, int* instead of void*) that you can
yourself wrap into a SWIG array_class (like in the linked document).


[1] http://www.swig.org/Doc1.3/Library.html#Library_nn7
On 08/04/2013 04:37 AM, Peter Horvath wrote:

can I convert a blob created by my C++ code (unsigned chars) to a Python
list or NumPy array?

pmt.blob_data() returns a 'SwigPyObject' with which I can't do anything.
Passing it to a NumPy array constructor doesn't seem to do the job.

The blobs are in a tuple:
pmt::pmt_t b1 = pmt::make_blob(a, a_length);
pmt::pmt_t b2 = pmt::make_blob(b, b_length);
return pmt::make_tuple(b1, b2);

And I try to check the contents of b1 in QA code like this:
res = my_test_function()
b1 = pmt.blob_data(pmt.tuple_ref(res, 0))
b1_len = pmt.blob_length(pmt.tuple_ref(res, 0))


Discuss-gnuradio mailing list

Discuss-gnuradio mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]