Pipelines and Blocks - Define the Block Class | Razor Think
Accessing and Manipulating Data

Pipelines and Blocks – Define the Block Class

By December 11, 2020 No Comments

Pipelines and Blocks – Define the Block Class

More articles in The Deep Learning Modeler

  • from razor.blocks import Block, inputs, outputs @inputs.atomic.generic(name='text', doc='A string of text to split') @inputs.atomic.generic(name='delimiter', doc="A single character or a sequence of characters") @outputs.atomic.generic(name='data', doc='An array of text') class SplitString(Block): def run(self, text, delimiter, data): result = text.split(delimiter) data.put(result)

This block can be instantiated and used as follows:

  • split_string = ( SplitString() .text('91-97384-20742') .delimiter('-') )

The instantiated block, can be executed as follows. Once the block is executed the results are stored in a dictionary, and the values can be access as show below.

  • results = split_string.execute() results['data'].values()

Discussion

Conceptually, blocks are akin to functions in a programming language. They take in inputs as parameters, perform some kind of operation on them and then return an output. In case of blocks, there can be multiple outputs as well and each input and output of a block are individually configured. Examples of blocks with multiple outputs will follow shortly.

Notice that run method receives inputs by the same name as they were declared using the @inputs decorator. They are keyword arguments so the order of their declaration and order of parameters in run need not be the same.

Also notice how the block is instantiated and used. Razor SDK widely adopts method-chaining pattern across most of its functionality. In this pattern, a method returns the object itself so that another method of the same object can be called right after. In the above example, input methods like .text and .delimiter are dynamically created in addition to the various other methods provided by the Block base class such as .execute. An interface method will be created for every input and output declared during the block definition. These methods will return the current value of the input if a new value is not passed as parameter. For example:

  • split_string.text(), split_string.delimiter()

An interface method with no parameters provided cannot be chained any further as they return the current value rather than the block object. So calling

  • split_string.text().delimiter('/')
  • # FIXME: this isn't working help(split_string.delimiter)

Leave a Reply