# Writing a block in Python (+XML)

**URL:** <https://www.ruby-forum.com/t/writing-a-block-in-python-xml/238810>\
**Category:** GNU Radio\
**Created:** [January 30, 2014, 1:01am UTC](https://www.ruby-forum.com/t/writing-a-block-in-python-xml/238810 "2014-01-30T01:01:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [January 30, 2014, 1:01am UTC](https://www.ruby-forum.com/t/writing-a-block-in-python-xml/238810/1 "2014-01-30T01:01:52Z")

</div>

Have been trying to write a block in Python. Did not use the modtool.py,  
since my understanding is it may not yet support that.

The block has only one output, no inputs. Also wrote the corresponding  
XML file. the block shows up correctly in GRC when I select it and add  
it to the flowgraph.

I’ve defined the output signature in the work function ( 1, 1,  
gr.sizeof\_float). My work function writes to output\_items[0][ some  
indexes] and returns with a count of the number of written output items.

Initially I derived the block from gr.block, but it complained about no  
connection to the output, so I changed it to derive from gr.hier\_block2.  
When running the flowgraph with my block in it, gnuradio complains that  
there is no output connection inside my block.

The question is ‘what’ internally connects my output\_items to the  
block’s output?

I tried self.connect(myblockname, (self, 0)) to satisfy the connection,  
but myblockname apparently needs some sort of modifier in order to be a  
valid output that can be connected to the block’s output. Tried looking  
through python blocks in the source, and the 3.7.2 API, but no luck so  
far…

Any advice?

– Tom, N5EG

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 30, 2014, 8:14am UTC](https://www.ruby-forum.com/t/writing-a-block-in-python-xml/238810/2 "2014-01-30T08:14:46Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi Tom,

> Have been trying to write a block in Python. Did not use the  
> modtool.py, since my understanding is it may not yet support that.  
> Luckily, you’re wrong.  
> gr\_modtool -l python add  
> works like a charm.

> The block has only one output, no inputs. Also wrote the  
> corresponding XML file. the block shows up correctly in GRC when I  
> select it and add it to the flowgraph.  
> It’s a source 🙂  
> I’ve defined the output signature in the work function ( 1, 1,  
> gr.sizeof\_float). My work function writes to output\_items[0][ some  
> indexes] and returns with a count of the number of written output  
> items.  
> very well 🙂

> Initially I derived the block from gr.block, but it complained  
> about no connection to the output, so I changed it to derive from  
> gr.hier\_block2. When running the flowgraph with my block in it,  
> gnuradio complains that there is no output connection inside my  
> block.  
> Oh, that’s wrong. A hier\_block is a hierarchical block, that is, only  
> a set of connections between other blocks and maybe some logic.  
> You can’t have “work” in a hier\_block; well, you can, but it won’t be  
> called by the GNU Radio scheduler.

> Any advice?  
> A hier block can’t be used here. I don’t know about your flowgraph,  
> but: did you really connect your gr.block-subclass’ output to let’s  
> say a null sink or a vector sink?

My advice is: Use gr\_modtool, it really makes life easier and failsafer.

Greetings,  
Marcus

> – Tom, N5EG
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ Discuss-gnuradio  
> mailing list [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1  
Comment: Using GnuPG with Thunderbird - [http://www.enigmail.net/](http://www.enigmail.net/)

iQEcBAEBAgAGBQJS6fumAAoJEAFxB7BbsDrLQmMIAIEgeZgSM8UI8BfUyMnzoJWb  
nc3oczsdrevBnprIsjAOfXgReU7gwYexASRBoEO4WFSsriVC5lf1bRMK6xRmbV+v  
kaVvrGWurIHWrkt0NWtktwUc0oWWE1fUTvk146V1LR41nUg6vYIw9qMwM3rwf2zV  
MeovU47+tsh3scyE9CEnGJxxiB7NGv7EaQ5EiuSXIFv+I8zrq/X2kFkX6K77oyhS  
VUmVkOS4U+fYaMzk90Efk06ehm5yaRrrDQoZq0oNUeRTyLYzz7rLSmI2wPOvBYeW  
uIhlHYTC0EYYmJy3WK+KggzhfUrw9yE547SkqUnKm2RFXxwfkzJqCwl0hsJ/Z/g=  
=aQ50  
-----END PGP SIGNATURE-----
