简体   繁体   English

Python 块中可变数量的输入和输出?

[英]Variable number of inputs and outputs in Python Block?

I am building a GNURadio block in Python that has a variable number of input, So I can change the number of input in each simulation.我正在 Python 中构建一个 GNURadio 块,它具有可变数量的输入,因此我可以在每次模拟中更改输入数量。 I use self.k=k to get the number of input as:我使用 self.k=k 来获取输入的数量:

def __init__(self, k=4):  # only default arguments here
    """arguments to this function show up as parameters in GRC"""
    self.k=k  ....
    in_sig=[np.float32 for k in range(k)],

However, this works only when I change the value of k inside the python code但是,这仅在我更改 python 代码中的 k 值时有效

def __init__(self, k=3):  

or或者

def __init__(self, k=2 ) ....

How do I make it change automatically from the block embedded itself without change the codes?如何在不更改代码的情况下使其从嵌入的块中自动更改?

""" Embedded Python Blocks: """ 嵌入 Python 块:

Each time this file is saved, GRC will instantiate the first class it finds to get ports and parameters of your block.每次保存此文件时,GRC 都会实例化它找到的第一个 class 以获取您的块的端口和参数。 The arguments to init will be the parameters.初始化的 arguments 将是参数。 All of them are required to have default values!所有这些都需要有默认值!

import numpy as np
from gnuradio import gr


class blk(gr.sync_block):  # other base classes are basic_block, decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self, example_param=1, Nr=4, Nt=4):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        self.Nr = Nr
        self.Nt = Nt
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.float32 for k in range(Nr)],
            out_sig=[np.float32 for k in range(Nt)],
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.example_param = example_param

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        output_items[0][:] = input_items[0] * self.example_param
        output_items[1][:] = input_items[1] * self.example_param
        return len(output_items[0])

In your code you gave the parameter ka default value in case it wasn't set.在您的代码中,您为参数 ka 提供了默认值,以防未设置。 If you want the variable to be something else than the default value you will have to set it when calling the method.如果您希望变量不是默认值,则必须在调用方法时设置它。

class Radio():

    def __init__(self, k=4):  # only default arguments here
        self.k=k
        print("k equals: " + self.k)


Radio(k=6)   #prints "k equals: 6"
Radio()      #prints "k equals: 4"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM