<commit_before>#!/usr/bin/env python
"""
.. module:: connection_generator.py
   :platform: Unix, Windows
   :synopsis: 

.. moduleauthor:: <NAME> <<EMAIL>>
.. moduleauthor:: <NAME> <<EMAIL>>
"""

from abc import ABCMeta, abstractmethod, abstractproperty

class IntervalSet:
    def __init__ (self, intervals = [], skip = 1):
        """
        intervals is a list of 2-tuples (FROM, TO) representing all
        integers i, FROM <= i <= TO
        """
        self.intervals = intervals
        self._skip = skip

    @property
    def skip (self):
        return self._skip

    def __iter__ (self):
        return self.intervals.__iter__ ()


class Mask:
    def __init__ (self,
                  sources = [], targets = [],
                  sourceSkip = 1, targetSkip = 1):
        """
        sources and targets are lists of 2-tuples (FROM, TO)
        representing all integers i, FROM <= i <= TO
        """
        self.sources = IntervalSet (sources, sourceSkip)
        self.targets = IntervalSet (targets, targetSkip)


class ConnectionGenerator:
    """
    The ConnectionGenerator interface.
    """
    
    __metaclass__ = ABCMeta
    
    @abstractproperty
    def size(self):
        """
        Returns the number of connections in the generator.
        
        :rtype: Integer (the number of the connections).
        :raises: TypeError 
        """
        pass
    
    @abstractproperty
    def arity(self):
        """
        Returns the number of parameters specified for an individual
        connection. It can be zero.
        
        :rtype: Integer
        :raises: TypeError
        """
        pass

    @abstractmethod
    def setMask(self, mask):
        """
        Inform the generator of which source and target indexes exist
        (must always be called before any of the methods below)
   
        :param mask: Mask object. skip (specified in mask) can be used in 
                     round-robin allocation schemes.
        
        :rtype: None
        :raises: TypeError
        """
        pass        
    
    @abstractmethod
    def __iter__(self):
        """
        Initializes and returns an iterator.
        Items are tuples (source_index, target_index, parameter0, parameter1, ...).
        The number of parameters is equal to arity.
        
        :rtype: Iterator
        :raises: TypeError
        """
        pass

    def setMasks(self, masks, local):
        """
        For a parallel simulator, we want to know the masks for all ranks
        
        :param masks: list of Mask objects 
        :param mask: integer 

        :rtype: None
        :raises: 
        """
        self.setMask(masks[local])<commit_msg>Added support for reading cg:s from xml.<commit_after>#!/usr/bin/env python
"""
.. module:: connection_generator.py
   :platform: Unix, Windows
   :synopsis: 

.. moduleauthor:: <NAME> <<EMAIL>>
.. moduleauthor:: <NAME> <<EMAIL>>
"""

from abc import ABCMeta, abstractmethod, abstractproperty

try:
    from lxml import etree
    from lxml.builder import E
except ImportError:
    pass

class IntervalSet:
    def __init__ (self, intervals = [], skip = 1):
        """
        intervals is a list of 2-tuples (FROM, TO) representing all
        integers i, FROM <= i <= TO
        """
        self.intervals = intervals
        self._skip = skip

    @property
    def skip (self):
        return self._skip

    def __iter__ (self):
        return self.intervals.__iter__ ()


class Mask:
    def __init__ (self,
                  sources = [], targets = [],
                  sourceSkip = 1, targetSkip = 1):
        """
        sources and targets are lists of 2-tuples (FROM, TO)
        representing all integers i, FROM <= i <= TO
        """
        self.sources = IntervalSet (sources, sourceSkip)
        self.targets = IntervalSet (targets, targetSkip)


class ConnectionGenerator:
    """
    The ConnectionGenerator interface.
    """
    
    __metaclass__ = ABCMeta
    
    @abstractproperty
    def arity(self):
        """
        Returns the number of parameters specified for an individual
        connection. It can be zero.
        
        :rtype: Integer
        :raises: TypeError
        """
        pass

    @abstractmethod
    def setMask(self, mask):
        """
        Inform the generator of which source and target indexes exist
        (must always be called before any of the methods below)
   
        :param mask: Mask object. skip (specified in mask) can be used in 
                     round-robin allocation schemes.
        
        :rtype: None
        :raises: TypeError
        """
        pass        
    
    def setMasks(self, masks, local):
        """
        For a parallel simulator, we want to know the masks for all ranks
        
        :param masks: list of Mask objects 
        :param mask: integer 

        :rtype: None
        :raises: 
        """
        self.setMask(masks[local])

    @abstractmethod
    def __len__(self):
        """
        Returns the number of connections in the generator.
        
        :rtype: Integer (the number of the connections).
        :raises: TypeError 
        """
        pass
    
    @abstractmethod
    def __iter__(self):
        """
        Initializes and returns an iterator.
        Items are tuples (source_index, target_index, parameter0, parameter1, ...).
        The number of parameters is equal to arity.
        
        :rtype: Iterator
        :raises: TypeError
        """
        pass

    tagMap = {}

    @classmethod
    def selectImplementation (cls, tag, module):
        cls.tagMap[tag] = module

    @classmethod
    def fromXML (cls, root):
        assert root.tag in tagMap, 'found no implementation for XML tag %s' % root.tag
        module = cls.tagMap[root.tag]
        return module.connectionGeneratorFromXML (root)
{'fraction_non_alphanumeric': 0.09072235535775419, 'fraction_numerical': 0.002396439575487847, 'mean_word_length': 1.8572127139364303, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4487742', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1553, 'n_words': 538}