<commit_before>from core.modules.vistrails_module import Module, ModuleError, ModuleConnector,\
     InvalidOutput
from core.modules.basic_modules import Boolean, String, Integer, Float, Tuple,\
     File, NotCacheable, Constant
from core.modules.module_registry import get_module_registry
from core.vistrail.port_spec import PortSpec
from core.utils import VistrailsInternalError
from list_module import ListOfElements
import copy


#################################################################################
## Fold Operator

class Fold(Module, NotCacheable):
    """The Fold Module is a high-order operator to implement some other structures,
    such as map, filter, sum, and so on.
    To use it, the user must inherit this class.
    Initially, the method setInitialValue() must be defined.
    Later, the method operation() must be defined."""
    
    def updateUpstream(self):
        """A modified version of the updateUpstream method."""

        ## Getting list of connectors
        connectors_InputList = self.inputPorts.get('InputList')
        if connectors_InputList==None:
            raise VistrailsInternalError('Missing value from port InputList')

        ## Updating connectors from 'InputList'
        for connector in connectors_InputList:
            connector.obj.update()
            
        self.inputList = self.getInputFromPort('InputList')
        
        self.partialResult = None
        self.initialValue = None
        
        self.setInitialValue()
        self.partialResult = self.initialValue
        self.elementResult = None
        
        ## If there is some function to consider...
        if self.hasInputFromPort('FunctionPort'):

            ## Getting list of connectors
            self.connectors_FunctionPort = self.inputPorts.get('FunctionPort')
            connectors_InputPort = self.inputPorts.get('InputPort')
            connectors_OutputPort = self.inputPorts.get('OutputPort')

            if self.connectors_FunctionPort==None:
                raise VistrailsInternalError('Missing value from port FunctionPort')
            if connectors_InputPort==None:
                raise VistrailsInternalError('Missing value from port InputPort')
            if connectors_OutputPort==None:
                raise VistrailsInternalError('Missing value from port OutputPort')
           
            ## Updating connectors from 'InputPort'
            for connector in connectors_InputPort:
                connector.obj.update()

            ## Updating connectors from 'OutputPort'
            for connector in connectors_OutputPort:
                connector.obj.update()

            ## Updating connectors from 'FunctionPort' --> This one must be the last
            self.updateFunctionPort()
        
        else:
            for i in xrange(len(self.inputList)):
                ## Setting information for logging stuff
                self.first_iteration = False
                if i==0:
                    self.is_fold_operator = True
                    self.first_iteration = True
                    self.last_iteration = False
                if i==((len(self.inputList))-1):
                    self.last_iteration = True
                    
                ## Getting the value inside the list
                self.element = self.inputList[i]
                self.operation()
                
        for iport, connectorList in copy.copy(self.inputPorts.items()):
            for connector in connectorList:
                if connector.obj.get_output(connector.port) is InvalidOutput:
                    self.removeInputConnector(iport, connector)

    def updateFunctionPort(self):
        """
        Function to be used inside the updateUsptream method of the Fold module. It
        updates the modules connected to the FunctionPort port.
        """
        nameInput = self.getInputFromPort('InputPort')
        nameOutput = self.getInputFromPort('OutputPort')

        ## Update everything for each value inside the list
        for i in xrange(len(self.inputList)):
            self.element = self.inputList[i]
            for connector in self.connectors_FunctionPort:
                if not self.upToDate:
                    ##Type checking
                    if i==0:
                        self.typeChecking(connector.obj, nameInput)
                    
                    connector.obj.upToDate = False
                    connector.obj.already_computed = False
                    
                    ## Setting information for logging stuff
                    connector.obj.first_iteration = False
                    if i==0:
                        connector.obj.is_fold_operator = True
                        connector.obj.first_iteration = True
                        connector.obj.last_iteration = False
                    if i==((len(self.inputList))-1):
                        connector.obj.last_iteration = True

                    self.setInputValues(connector.obj, nameInput)
                connector.obj.update()
                
                ## Getting the result from the output port
                if nameOutput not in connector.obj.outputPorts:
                    raise ModuleError(connector.obj,\
                                      'Invalid output port: %s'%nameOutput)
                self.elementResult = connector.obj.get_output(nameOutput)
            self.operation()

    def setInputValues(self, module, inputPorts):
        """
        Function used to set a value inside 'module', given the input port(s).
        """
        if len(inputPorts)==1:
            ## Cleaning the previous connector...
            if inputPorts[0] in module.inputPorts:
                del module.inputPorts[inputPorts[0]]
            new_connector = ModuleConnector(create_constant(self.element),\
                                            'value')
            print inputPorts[0]
            module.set_input_port(inputPorts[0], new_connector)
        else:
            for j in xrange(len(inputPorts)):
                ## Cleaning the previous connector...
                if inputPorts[j] in module.inputPorts:
                    del module.inputPorts[inputPorts[j]]
                new_connector = ModuleConnector(create_constant(self.element[j]),\
                                                'value')
                module.set_input_port(inputPorts[j], new_connector)

    def typeChecking(self, module, inputPorts):
        """
        Function used to check if the types of the input list element and of the
        inputPort of 'module' match.
        """
        if len(inputPorts)==1:
            port_spec1 = module.moduleInfo['pipeline'].modules\
                         [module.moduleInfo['moduleId']].get_port_spec(inputPorts[0],\
                                                                       'input')
            for element in self.inputList:
                v_module = create_module(element, port_spec1.signature)
                if v_module!=None:<commit_msg>A bug was fixed inside fold.py, something I had forgotten. Besides, a "print" line was removed.<commit_after>from core.modules.vistrails_module import Module, ModuleError, ModuleConnector,\
     InvalidOutput
from core.modules.basic_modules import Boolean, String, Integer, Float, Tuple,\
     File, NotCacheable, Constant
from core.modules.module_registry import get_module_registry
from core.vistrail.port_spec import PortSpec
from core.utils import VistrailsInternalError
from list_module import ListOfElements
import copy


#################################################################################
## Fold Operator

class Fold(Module, NotCacheable):
    """The Fold Module is a high-order operator to implement some other structures,
    such as map, filter, sum, and so on.
    To use it, the user must inherit this class.
    Initially, the method setInitialValue() must be defined.
    Later, the method operation() must be defined."""
    
    def updateUpstream(self):
        """A modified version of the updateUpstream method."""

        ## Getting list of connectors
        connectors_InputList = self.inputPorts.get('InputList')
        if connectors_InputList==None:
            raise VistrailsInternalError('Missing value from port InputList')

        ## Updating connectors from 'InputList'
        for connector in connectors_InputList:
            connector.obj.update()
            
        self.inputList = self.getInputFromPort('InputList')
        
        self.partialResult = None
        self.initialValue = None
        
        self.setInitialValue()
        self.partialResult = self.initialValue
        self.elementResult = None
        
        ## If there is some function to consider...
        if self.hasInputFromPort('FunctionPort'):

            ## Getting list of connectors
            self.connectors_FunctionPort = self.inputPorts.get('FunctionPort')
            connectors_InputPort = self.inputPorts.get('InputPort')
            connectors_OutputPort = self.inputPorts.get('OutputPort')

            if self.connectors_FunctionPort==None:
                raise VistrailsInternalError('Missing value from port FunctionPort')
            if connectors_InputPort==None:
                raise VistrailsInternalError('Missing value from port InputPort')
            if connectors_OutputPort==None:
                raise VistrailsInternalError('Missing value from port OutputPort')
           
            ## Updating connectors from 'InputPort'
            for connector in connectors_InputPort:
                connector.obj.update()

            ## Updating connectors from 'OutputPort'
            for connector in connectors_OutputPort:
                connector.obj.update()

            ## Updating connectors from 'FunctionPort' --> This one must be the last
            self.updateFunctionPort()
        
        else:
            for i in xrange(len(self.inputList)):
                ## Getting the value inside the list
                self.element = self.inputList[i]
                self.operation()
                
        for iport, connectorList in copy.copy(self.inputPorts.items()):
            for connector in connectorList:
                if connector.obj.get_output(connector.port) is InvalidOutput:
                    self.removeInputConnector(iport, connector)

    def updateFunctionPort(self):
        """
        Function to be used inside the updateUsptream method of the Fold module. It
        updates the modules connected to the FunctionPort port.
        """
        nameInput = self.getInputFromPort('InputPort')
        nameOutput = self.getInputFromPort('OutputPort')

        ## Update everything for each value inside the list
        for i in xrange(len(self.inputList)):
            self.element = self.inputList[i]
            for connector in self.connectors_FunctionPort:
                if not self.upToDate:
                    ##Type checking
                    if i==0:
                        self.typeChecking(connector.obj, nameInput)
                    
                    connector.obj.upToDate = False
                    connector.obj.already_computed = False
                    
                    ## Setting information for logging stuff
                    connector.obj.first_iteration = False
                    if i==0:
                        connector.obj.is_fold_operator = True
                        connector.obj.first_iteration = True
                        connector.obj.last_iteration = False
                    if i==((len(self.inputList))-1):
                        connector.obj.last_iteration = True

                    self.setInputValues(connector.obj, nameInput)
                connector.obj.update()
                
                ## Getting the result from the output port
                if nameOutput not in connector.obj.outputPorts:
                    raise ModuleError(connector.obj,\
                                      'Invalid output port: %s'%nameOutput)
                self.elementResult = connector.obj.get_output(nameOutput)
            self.operation()

    def setInputValues(self, module, inputPorts):
        """
        Function used to set a value inside 'module', given the input port(s).
        """
        if len(inputPorts)==1:
            ## Cleaning the previous connector...
            if inputPorts[0] in module.inputPorts:
                del module.inputPorts[inputPorts[0]]
            new_connector = ModuleConnector(create_constant(self.element),\
                                            'value')
            module.set_input_port(inputPorts[0], new_connector)
        else:
            for j in xrange(len(inputPorts)):
                ## Cleaning the previous connector...
                if inputPorts[j] in module.inputPorts:
                    del module.inputPorts[inputPorts[j]]
                new_connector = ModuleConnector(create_constant(self.element[j]),\
                                                'value')
                module.set_input_port(inputPorts[j], new_connector)

    def typeChecking(self, module, inputPorts):
        """
        Function used to check if the types of the input list element and of the
        inputPort of 'module' match.
        """
        if len(inputPorts)==1:
            port_spec1 = module.moduleInfo['pipeline'].modules\
                         [module.moduleInfo['moduleId']].get_port_spec(inputPorts[0],\
                                                                       'input')
            for element in self.inputList:
                v_module = create_module(element, port_spec1.signature)
                if v_module!=None:
{'fraction_non_alphanumeric': 0.08605211298445369, 'fraction_numerical': 0.0018246843296109772, 'mean_word_length': 1.8251546391752578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3783590', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3141, 'n_words': 875}