# (Python) Source Block Outputs All 0's

**URL:** <https://www.ruby-forum.com/t/python-source-block-outputs-all-0s/239247>\
**Category:** GNU Radio\
**Created:** [February 18, 2014, 7:51pm UTC](https://www.ruby-forum.com/t/python-source-block-outputs-all-0s/239247 "2014-02-18T19:51:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [February 18, 2014, 7:51pm UTC](https://www.ruby-forum.com/t/python-source-block-outputs-all-0s/239247/1 "2014-02-18T19:51:35Z")

</div>

Hi All,

I have implemented a python source block:

```
def __init__ (self, kA1=4,kB1=4,k2=4,NA1=8,NB1=8,N2=8,M=8):
    gr.sync_block. __init__ (self,
        name="blsd_enc_b",
        in_sig=None,
        out_sig=[numpy.uint8,numpy.uint8])

```

but I can’t get it to output anything but 0’s…

```
def work(self, input_items, output_items):
    out_cA = output_items[0]
    out_cB = output_items[1]

    out_cA = [5, 6, 7, 8, 9, 10] #cA[0]
    out_cB = [5, 6, 7, 8, 9, 10] #cB[0]
    self.produce(0,len(out_cA))
    self.produce(1,len(out_cB))
    print 'out_cA = ', out_cA, ', len(out_cA) = ', len(out_cA)
    print 'out_cB = ', out_cB, ', len(out_cB) = ', len(out_cB)
    return -1 #-2

```

I connect both outputs to a file sync of type byte, and it stores 6  
bytes but its all “\00\00\00\00\00\00”

The print lines give:

out\_cA = [5, 6, 7, 8, 9, 10] , len(out\_cA) = 6  
out\_cB = [5, 6, 7, 8, 9, 10] , len(out\_cB) = 6

as expected.

Any advice?

Regards,

David

* * *

NOTE: The information in this email and any attachments may be  
confidential and/or legally privileged. This message may be read, copied  
and used only by the intended recipient. If you are not the intended  
recipient, please destroy this message, delete any copies held on your  
system and notify the sender immediately.

Toshiba Research Europe Limited, registered in England and Wales  
(2519556). Registered Office 208 Cambridge Science Park, Milton Road,  
Cambridge CB4 0GZ, England. Web: www.toshiba.eu/research/trl

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [February 18, 2014, 10:07pm UTC](https://www.ruby-forum.com/t/python-source-block-outputs-all-0s/239247/2 "2014-02-18T22:07:36Z")

</div>

On Tue, Feb 18, 2014 at 12:50 PM, David Halls  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> print 'out\_cA = ', out\_cA, ', len(out\_cA) = ', len(out\_cA)  
> out\_cB = [5, 6, 7, 8, 9, 10] , len(out\_cB) = 6
> 
> as expected.
> 
> Any advice?
> 
> Regards,
> 
> David

David,

The problem here is that you’re not inserting items in to the out\_cA  
(and out\_cB) arrays, you’re actually reassigning out\_cA to be this new  
list.

Honestly, I’ve never written a python block, but my understanding is  
that you’d have to do something like this [http://dpaste.com/1634971/](http://dpaste.com/1634971/)

The items in the out\_items list are actually numpy arrays, and they  
need to be indexed. The [:] indexing is a shortcut of indexing the  
entire array.

Another thing I’m not really sure about is whether the -1 will work or  
not. I think you might have to return 6 so that the scheduler knows  
there are 6 items to work on, and then the next time work is called  
you return -1 ( assuming you’re wanting to stop the flowgraph here?).  
Somebody else might chime in here if I’m wrong.

Nathan

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [February 18, 2014, 11:13pm UTC](https://www.ruby-forum.com/t/python-source-block-outputs-all-0s/239247/3 "2014-02-18T23:13:00Z")

</div>

Hi Nathan,

Thanks for your reply 🙂

I’ve done little python before, am trying to incorporate a collaborators  
code into my system and he’s used python.

I am used to C, where it would copy the pointer. I understand your  
advice - thanks, I will look at the link.

The return value is mimicking the WORK\_CALLED\_PRODUCE (-2) and WORK\_DONE  
return values that can be used when you have multiple outputs which may  
output different number of items and the produce function is used.

I am not sure why the block keeps repeating if I use -2 as I use  
successfully in many c++ blocks - I’m not used to writing source  
blocks… maybe others have comments?

David

* * *

From: West, Nathan [removed\_email\_address@domain.invalid]  
Sent: 18 February 2014 21:06  
To: David Halls  
Cc: GNURadio D.ion List  
Subject: Re: [Discuss-gnuradio] (Python) Source Block Outputs All 0’s

On Tue, Feb 18, 2014 at 12:50 PM, David Halls  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> print 'out\_cA = ', out\_cA, ', len(out\_cA) = ', len(out\_cA)  
> out\_cB = [5, 6, 7, 8, 9, 10] , len(out\_cB) = 6
> 
> as expected.
> 
> Any advice?
> 
> Regards,
> 
> David

David,

The problem here is that you’re not inserting items in to the out\_cA  
(and out\_cB) arrays, you’re actually reassigning out\_cA to be this new  
list.

Honestly, I’ve never written a python block, but my understanding is  
that you’d have to do something like this [http://dpaste.com/1634971/](http://dpaste.com/1634971/)

The items in the out\_items list are actually numpy arrays, and they  
need to be indexed. The [:] indexing is a shortcut of indexing the  
entire array.

Another thing I’m not really sure about is whether the -1 will work or  
not. I think you might have to return 6 so that the scheduler knows  
there are 6 items to work on, and then the next time work is called  
you return -1 ( assuming you’re wanting to stop the flowgraph here?).  
Somebody else might chime in here if I’m wrong.

Nathan

* * *

NOTE: The information in this email and any attachments may be  
confidential and/or legally privileged. This message may be read, copied  
and used only by the intended recipient. If you are not the intended  
recipient, please destroy this message, delete any copies held on your  
system and notify the sender immediately.

Toshiba Research Europe Limited, registered in England and Wales  
(2519556). Registered Office 208 Cambridge Science Park, Milton Road,  
Cambridge CB4 0GZ, England. Web: www.toshiba.eu/research/trl
