簡體   English   中英

Python 塊中可變數量的輸入和輸出?

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

我正在 Python 中構建一個 GNURadio 塊,它具有可變數量的輸入,因此我可以在每次模擬中更改輸入數量。 我使用 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)],

但是,這僅在我更改 python 代碼中的 k 值時有效

def __init__(self, k=3):  

或者

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

如何在不更改代碼的情況下使其從嵌入的塊中自動更改?

""" 嵌入 Python 塊:

每次保存此文件時,GRC 都會實例化它找到的第一個 class 以獲取您的塊的端口和參數。 初始化的 arguments 將是參數。 所有這些都需要有默認值!

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])

在您的代碼中,您為參數 ka 提供了默認值,以防未設置。 如果您希望變量不是默認值,則必須在調用方法時設置它。

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