Wiki

Clone wiki

pypes / Reverse_Field

import logging

from pypes.component import Component

log = logging.getLogger(__name__)

class Reverse(Component):

    __metatype__ = 'TRANSFORMER'

    def __init__(self):
        # initialize parent class
        Component.__init__(self)

        #Setup any user parameters required by this component 
        self.set_parameter('field', '')

        # log successful initialization message
        log.info('Component Initialized: %s' % self.__class__.__name__)

    def run(self):
        while True:
            field_to_reverse = self.get_parameter('field')            

            # for each packet waiting on our input port
            for packet in self.receive_all('in'):
                try:
                    # perform your custom logic here
                    value = packet.get(field_to_reverse)
                    if value is not None:
                        value = value[::-1]
                        packet.set(field_to_reverse, value)

                except Exception as e:
                    log.error('Component Failed: %s' % self.__class__.__name__)
                    log.error('Reason: %s' % str(e))                    

                # send the packet to the next component
                self.send('out', packet)

            # yield the CPU, allowing another component to run
            self.yield_ctrl()

Updated