# Inserting code into top\_block.py

**URL:** <https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186>\
**Category:** GNU Radio\
**Created:** [September 23, 2014, 9:02pm UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186 "2014-09-23T21:02:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 23, 2014, 9:02pm UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186/1 "2014-09-23T21:02:45Z")

</div>

Hey All,  
I’m trying to figure out if what I want to do is possible in GNU Radio  
Companion or if there’s a better way to achieve this functionality.  
I have some code that I want to execute after all the blocks and  
connections have been initialized in the top\_block init()but before the  
tb.Start(). Is there some way I can create a custom block that inserts  
code  
into top\_block.py at that point or inserts the code at the end of the  
init()? I currently have my code running in gnuradio-runtime, but I want  
to  
have this same functionality without altering the gnuradio trunk code,  
hence why I’m thinking of a custom block that can insert the code where  
I  
want.  
Thanks,  
Ryan M.

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [September 23, 2014, 9:15pm UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186/2 "2014-09-23T21:15:51Z")

</div>

You might be able to trick the ‘Variable’ block into doing what you  
want.

M

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [September 23, 2014, 9:54pm UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186/3 "2014-09-23T21:54:30Z")

</div>

The most straightforward way is to make a small Python program that  
calls the class generated by GRC. Copy the autogenerated code after

if **name** == ‘ **main** ’

into another file, and insert your logic before the tb.start() line.

- Jeff

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [September 24, 2014, 1:54am UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186/4 "2014-09-24T01:54:16Z")

</div>

Shouldn’t he “cut” the code?

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [September 24, 2014, 2:21am UTC](https://www.ruby-forum.com/t/inserting-code-into-top-block-py/232186/5 "2014-09-24T02:21:03Z")

</div>

Vanush,

GRC will overwrite any changes you make to the generated Python file, so  
you would want to do any custom programming outside that file. The  
‘ **main** ’ code is only hit if you execute the file as the top level  
program.

- Jeff
