Python collections.Iterable() Examples

The following are code examples for showing how to use collections.Iterable(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_Iterable(self):
        # Check some non-iterables
        non_samples = [None, 42, 3.14, 1j]
        for x in non_samples:
            self.assertNotIsInstance(x, Iterable)
            self.assertFalse(issubclass(type(x), Iterable), repr(type(x)))
        # Check some iterables
        samples = [str(),
                   tuple(), list(), set(), frozenset(), dict(),
                   dict().keys(), dict().items(), dict().values(),
                   (lambda: (yield))(),
                   (x for x in []),
                   ]
        for x in samples:
            self.assertIsInstance(x, Iterable)
            self.assertTrue(issubclass(type(x), Iterable), repr(type(x)))
        # Check direct subclassing
        class I(Iterable):
            def __iter__(self):
                return super(I, self).__iter__()
        self.assertEqual(list(I()), [])
        self.assertFalse(issubclass(str, I))
        self.validate_abstract_methods(Iterable, '__iter__')
        self.validate_isinstance(Iterable, '__iter__') 
Example 2
Project: Py-Utils   Author: LonamiWebs   File: extrapolate.py    MIT License 6 votes vote down vote up
def get_subseqs(s, ops):
    """Returns a list of sequences given when applying the list of (ops)
       on them, until a constant one is found, thus:
       new[0] = next seq of s with ops[0]
       new[i] = next seq of new[i-1] with op[i]
       
       If 'ops' is not a list, then the same operation will be repeated.
       The length of 'ops' should be equal to the length of 's' minus 1"""
    if len(s) < 2:
        # We can't get the next sequence based on two terms if there's only one
        return []
    
    if not isinstance(ops, Iterable):
        ops = [ops for _ in range(len(s)-1)]
    
    # Start with the initial subsequence
    subseqs = [get_subseq(s, ops[0])]
    
    # And base the next subsequences on the previous one until they're constant
    i = 1
    while not is_constant(subseqs[-1]) and len(subseqs[-1]) > 1:
        subseqs.append(get_subseq(subseqs[-1], ops[i]))
        i += 1
    
    return subseqs 
Example 3
Project: Py-Utils   Author: LonamiWebs   File: extrapolate.py    MIT License 6 votes vote down vote up
def next_item(s, ops, invops):
    """Tries to guess the next item on the sequence 's' by using a table
       of (ops). This method will fail if the sequence doesn't provide enough
       information, and will assume that the latest sequence should remain
       constant.
       
       The inverse operations 'invops' are also required to transverse back
       the tree in order to get the proper results"""
    ss = get_subseqs(s, ops)
    # We assume the last item all below Repeats
    r = ss[-1][-1]
    
    # Ensure we have a list of inverse operations
    if not isinstance(invops, Iterable):
        invops = [invops for _ in range(len(s)-1)]
    
    # We need to chain this result to the previous ones
    # Range of invops: end....1
    # Range of subseq: end-1..0
    for i in reversed(range(1, len(ss))):
        r = invops[i](ss[i-1][-1], r)
    
    # Now we have the accumulated inverse operation
    return invops[0](s[-1], r) 
Example 4
Project: python-linq   Author: jakkes   File: core.py    MIT License 6 votes vote down vote up
def concat(self, iterable):
        """Adds an iterable to the sequence
        
        Arguments:
            iterable {Iterable} -- The collection to add
        
        Raises:
            ValueError -- If the supplied object is not iterable
        
        Returns:
            Iterable {From} -- Returns a `From` object that wraps the new collection of objects
        """


        if not isinstance(iterable, collections.Iterable):
            raise ValueError("Object is not iterable")

        self.extensions.append(iterable)
        return self 
Example 5
Project: python-linq   Author: jakkes   File: core.py    MIT License 6 votes vote down vote up
def take(self, count):
        """Selects the amount of elements specified
        
        Arguments:
            count -- The number of elements to select
        
        Returns:
            Iterable {From} -- `From` object wrapping the selected elements
        """

        def sequence():
            n = 0
            for x in self:
                
                if n >= count:
                    break
                
                yield x
                n += 1

        return From(x for x in sequence()) 
Example 6
Project: python-linq   Author: jakkes   File: core.py    MIT License 6 votes vote down vote up
def takeWhile(self, condition):
        """Selects elements as long as the condition is fulfilled
        
        Arguments:
            condition -- Expression returning `True` or `False`
        
        Returns:
            Iterable {From} -- `From` object wrapping the selected elements
        """


        def sequence():
            for x in self:
                if condition(x):
                    yield x
                else:
                    break

        return From(x for x in sequence()) 
Example 7
Project: python-linq   Author: jakkes   File: core.py    MIT License 6 votes vote down vote up
def skip(self, count):
        """Skips the first elements in the sequence

        Arguments:
            count {int} -- The number of elements to skip

        Returns:
            Iterable {From} -- The remaining elements wrapped in a From object.
        """

        def sequence():
            i = 0
            for obj in self:
                if i < count:
                    i += 1
                    continue

                yield obj

        return From(x for x in sequence()) 
Example 8
Project: python-linq   Author: jakkes   File: core.py    MIT License 6 votes vote down vote up
def skipWhile(self, condition):
        """Skips the first elements in the sequence while the condition evaluates `True`

        Arguments:
            condition -- The number of elements to skip

        Returns:
            Iterable {From} -- The remaining elements wrapped in a From object.
        """
        def sequence():
            skipping = True
            for obj in self:
                if skipping:
                    if condition(obj):
                        continue
                    else:
                        skipping = False
                yield obj
        
        return From(x for x in sequence()) 
Example 9
Project: epyc   Author: simoninireland   File: lab.py    GNU General Public License v2.0 6 votes vote down vote up
def addParameter( self, k, r ):
        """Add a parameter to the experiment's parameter space. k is the
        parameter name, and r is its range.

        :param k: parameter name
        :param r: parameter range"""

        if isinstance(r, six.string_types) or not isinstance(r, collections.Iterable):
            # range is a single value (where a string constitutes a single value), make it a list
            r = [ r ]
        else:
            if isinstance(r, collections.Iterable):
                # range is an iterable, make into a list
                r = list(r)

        self._parameters[k] = r 
Example 10
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 11
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 12
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: fake.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, obj_ref, prop_list=None, missing_list=None):
        self.obj = obj_ref

        if not isinstance(prop_list, collections.Iterable):
            prop_list = []

        if not isinstance(missing_list, collections.Iterable):
            missing_list = []

        # propSet is the name your Python code will need to
        # use since this is the name that the API will use
        if prop_list:
            self.propSet = prop_list

        # missingSet is the name your python code will
        # need to use since this is the name that the
        # API we are talking to will use.
        if missing_list:
            self.missingSet = missing_list 
Example 13
Project: sic   Author: Yanixos   File: pyparsing.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 14
Project: sic   Author: Yanixos   File: pyparsing.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 15
Project: bigquerylayers   Author: smandaric   File: pyparsing.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 16
Project: bigquerylayers   Author: smandaric   File: pyparsing.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 17
Project: AshsSDK   Author: thehappydinoa   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 18
Project: AshsSDK   Author: thehappydinoa   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 19
Project: trelliolibs   Author: quikmile   File: helpers.py    MIT License 6 votes vote down vote up
def record_to_dict(recs, normalize=None):
        if normalize and not isinstance(normalize, collections.Iterable):
            normalize = [normalize]
        elif not normalize:
            normalize = normalize if normalize else [lambda i: i]

        if not isinstance(recs, list):
            data = dict(recs)
            for j in normalize:
                data = j(data)
            return data
        elif isinstance(recs, list):
            _l = []
            for i in recs:
                data = dict(i)
                for j in normalize:
                    data = j(data)
                _l.append(data)
            return _l 
Example 20
Project: trelliolibs   Author: quikmile   File: helpers.py    MIT License 6 votes vote down vote up
def record_to_tuple(recs, normalize=None):
        if normalize and not isinstance(normalize, collections.Iterable):
            normalize = [normalize]
        elif not normalize:
            normalize = normalize if normalize else [lambda i: i]
        _l = []
        if not isinstance(recs, list):
            recs = [recs]
        for i in recs:
            data = tuple(i)
            for j in normalize:
                data = j(data)
            _l.append(data)
        if len(_l) > 1:
            return _l
        elif len(_l) > 0:
            return _l[0]
        else:
            return _l 
Example 21
Project: twitterscraper   Author: taspinar   File: main.py    MIT License 6 votes vote down vote up
def default(self, obj):
        if hasattr(obj, '__json__'):
            return obj.__json__()
        elif isinstance(obj, collections.Iterable):
            return list(obj)
        elif isinstance(obj, dt.datetime):
            return obj.isoformat()
        elif hasattr(obj, '__getitem__') and hasattr(obj, 'keys'):
            return dict(obj)
        elif hasattr(obj, '__dict__'):
            return {member: getattr(obj, member)
                    for member in dir(obj)
                    if not member.startswith('_') and
                    not hasattr(getattr(obj, member), '__call__')}

        return json.JSONEncoder.default(self, obj) 
Example 22
Project: matchminer-engine   Author: dfci   File: cerberus.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def __normalize_coerce(self, processor, field, value, error):
        if isinstance(processor, _str_type):
            processor = self.__get_rule_handler('normalize_coerce', processor)

        elif isinstance(processor, Iterable):
            result = value
            for p in processor:
                result = self.__normalize_coerce(p, field, result, error)
                if errors.COERCION_FAILED in \
                    self.document_error_tree.fetch_errors_from(
                        self.document_path + (field,)):
                    break
            return result

        try:
            return processor(value)
        except Exception as e:
            self._error(field, error, str(e))
            return value 
Example 23
Project: matchminer-engine   Author: dfci   File: cerberus.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _validate_validator(self, validator, field, value):
        """ {'oneof': [
                {'type': 'callable'},
                {'type': 'list',
                 'schema': {'oneof': [{'type': 'callable'},
                                      {'type': 'string'}]}},
                {'type': 'string'}
                ]} """
        if isinstance(validator, _str_type):
            validator = self.__get_rule_handler('validator', validator)
            validator(field, value)
        elif isinstance(validator, Iterable):
            for v in validator:
                self._validate_validator(v, field, value)
        else:
            validator(field, value, self._error) 
Example 24
Project: ICDAR-2019-SROIE   Author: zzzDavid   File: utils.py    MIT License 6 votes vote down vote up
def encode(self, text):
        """Support batch or single str.

        Args:
            text (str or list of str): texts to convert.

        Returns:
            torch.IntTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
            torch.IntTensor [n]: length of each text.
        """
        if isinstance(text, str):
            text = [
                self.dict[char.lower() if self._ignore_case else char]
                for char in text
            ]
            length = [len(text)]
        elif isinstance(text, collections.Iterable):
            length = [len(s) for s in text]
            text = ''.join(text)
            text, _ = self.encode(text)
        return (torch.IntTensor(text), torch.IntTensor(length)) 
Example 25
Project: Repobot   Author: Desgard   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 26
Project: Repobot   Author: Desgard   File: pyparsing.py    MIT License 6 votes vote down vote up
def __init__( self, exprs, savelist = False ):
        super(ParseExpression,self).__init__(savelist)
        if isinstance( exprs, _generatorType ):
            exprs = list(exprs)

        if isinstance( exprs, basestring ):
            self.exprs = [ ParserElement._literalStringClass( exprs ) ]
        elif isinstance( exprs, collections.Iterable ):
            exprs = list(exprs)
            # if sequence of strings provided, wrap with Literal
            if all(isinstance(expr, basestring) for expr in exprs):
                exprs = map(ParserElement._literalStringClass, exprs)
            self.exprs = list(exprs)
        else:
            try:
                self.exprs = list( exprs )
            except TypeError:
                self.exprs = [ exprs ]
        self.callPreparse = False 
Example 27
Project: GeoPy   Author: aerler   File: misc.py    GNU General Public License v3.0 6 votes vote down vote up
def defaultNamedtuple(typename, field_names, defaults=None):
  ''' wrapper for namedtuple that supports defaults; adapted from stackoverflow:
      https://stackoverflow.com/questions/11351032/named-tuple-and-optional-keyword-arguments ''' 
  T = col.namedtuple(typename, field_names) # make named tuple
  T.__new__.__defaults__ = (None,) * len(T._fields) # set defaults to None
  # add custom defaults
  if defaults is not None:
    if isinstance(defaults, col.Mapping):
        prototype = T(**defaults)
    elif isinstance(defaults, col.Iterable):
        prototype = T(*defaults)
    else: raise ArgumentError(str(defaults))
    T.__new__.__defaults__ = tuple(prototype)
#     # add self-referenc defaults
#     if ref_prefix:
#       l = len(ref_prefix)
#       for field,value in T._asdict().iteritems():
#         if isinstance(value,basestring) and value[:l] == ref_prefix:
#           T.__dict__[field] = T.__dict__[value[l:]]
#     # N.B.: this would have to go into the constructor in order to work...
  # return namedtuple with defaults
  return T
  
# create a named tuple instance on the fly from dictionary 
Example 28
Project: GeoPy   Author: aerler   File: misc.py    GNU General Public License v3.0 6 votes vote down vote up
def __call__(self, *args, **kwargs):
    ''' Call function element-wise, by iterating over the argument list. Multiple arguments are supported using
        multiple list arguments (of the same length); key-word arguments are passed directly to the function. '''
    if isinstance(args[0], col.Iterable) and not isinstance(args[0], str):
      l = len(args[0])
      for arg in args: # check consistency
        if not isinstance(arg, col.Iterable) and len(arg)==l: 
          raise TypeError('All list arguments have to be of the same length!')
      results = [] # output list
      for i in range(l):
        eltargs = [arg[i] for arg in args] # construct argument list for this element
        results.append(self.f(*eltargs, **kwargs)) # results are automatically wrapped into tuples, if necessary
    else:
      results = (self.f( *args, **kwargs),) # just call function normally and wrap results in a tuple
    # make sure result is a tuple
    return tuple(results)
      

## useful functions

# check if input is a valid index or slice 
Example 29
Project: pyfilter   Author: tingiskhan   File: utils.py    MIT License 6 votes vote down vote up
def flatten(*args):
    """
    Flattens an array comprised of an arbitrary number of lists. Solution found at:
        https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists
    :param iterable: The iterable you wish to flatten.
    :type iterable: collections.Iterable
    :return:
    """
    out = list()
    for el in args:
        if isinstance(el, Iterable) and not isinstance(el, (str, bytes, torch.Tensor)):
            out.extend(flatten(*el))
        else:
            out.append(el)

    return tuple(out) 
Example 30
Project: pyfilter   Author: tingiskhan   File: utils.py    MIT License 6 votes vote down vote up
def state_dict(self):
        """
        Gets the state dictionary of all the serializable items in the module
        :rtype: dict
        """
        # TODO: This might be improved (?)
        # TODO: Implement serializing as native objects. How to deserialize though?
        # TODO: Only supports 1 level iterables currently, fix this
        res = dict()
        res[_OBJTYPENAME] = self.__class__.__name__

        for name, a in _yield_objs(self):
            if isinstance(a, torch.Tensor):
                res[name] = a
            elif isinstance(a, Iterable):
                if all(isinstance(it, torch.Tensor) for it in a) and all(it._base is None for it in a):
                    res[name] = a
                elif all(isinstance(it, _NATIVE) for it in a):
                    res[name] = a
            elif isinstance(a, HelperMixin):
                res[name] = a.state_dict()
            elif isinstance(a, _NATIVE):
                res[name] = a

        return res 
Example 31
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_direct_subclassing(self):
        for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
            class C(B):
                pass
            self.assertTrue(issubclass(C, B))
            self.assertFalse(issubclass(int, C)) 
Example 32
Project: pyblish-win   Author: pyblish   File: test_collections.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_registration(self):
        for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
            class C:
                __metaclass__ = type
                __hash__ = None  # Make sure it isn't hashable by default
            self.assertFalse(issubclass(C, B), B.__name__)
            B.register(C)
            self.assertTrue(issubclass(C, B)) 
Example 33
Project: gql   Author: graphql-python   File: dsl.py    MIT License 5 votes vote down vote up
def serialize_list(serializer, values):
    assert isinstance(values, collections.Iterable), 'Expected iterable, received "{}"'.format(repr(values))
    return [serializer(v) for v in values] 
Example 34
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def __init__(self, iterable):
        """Wraps an iterable object
        
        Arguments:
            iterable {Iterable} -- An iterable collection of objects
        
        Raises:
            ValueError -- If iterable is not an iterable collection
        """
        if not isinstance(iterable, collections.Iterable):
            raise ValueError("Object is not iterable")

        
        self.iterable = iterable
        self.extensions = [] 
Example 35
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def select(self, transform):
        """Transforms each object in the sequence.
        
        Arguments:
            transform -- Function describing the transformation
        
        Returns:
            Iterable {From} -- Returns a `From` object wrapping the new objects for further use
        """
        return From(transform(x) for x in self) 
Example 36
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def selectMany(self, transform = lambda x: x):
        """Selects objects, with the possibility of transforming them, for all the underlying lists into
        one sequence. Useful when the collection is composed of multiple subcollections containing objects. 
        
        Keyword Arguments:
            transform -- Expression describing the transformation. (default: no transform is applied and the underlying objects are selected as is)
        
        Returns:
            Iterable {From} -- Returns a `From` object wrapping the new objects for further use.
        """

        return From(transform(x) for y in self for x in y) 
Example 37
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def where(self, condition):
        """Filters the sequence for the given condition
        
        Arguments:
            condition -- Expression returning `True` or `False` with a single input.
        
        Returns:
            Iterable {From} -- Returns a `From` object wrapping all objects for which the condition is True.
        """

        return From(x for x in self if condition(x)) 
Example 38
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def groupBy(self, key, transform = lambda x: x):
        """Groups all elements based on the given key.
        
        Arguments:
            key -- Expression determining which key to use
        
        Keyword Arguments:
            transform -- Expression describing the transform the objects when creating the groups.
            Can be any lambda expression or function with a single input argument (default: Use the elements as is)
        
        Returns:
            Iterable {From} -- A `From` object wrapping a collection of `Grouping` objects.
            Every `Grouping` object has two attributes, `key` and `values` which contains the
            transformed objects.
        """

        def sequence():
            groups = dict()
            for x in self:
                k = key(x)
                if k in groups:
                    groups[k].append(transform(x))
                else:
                    groups[k] = [transform(x)]

            for k in groups:
                yield Grouping(k, groups[k])

        return From(x for x in sequence()) 
Example 39
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def groupJoin(self, extension, innerKey, outerKey, innerTransform = lambda x: x, outerTransform = lambda x: x):
        
        """Joins the sequence with objects from another sequence.
        
        Arguments:
            extension {Iterable} -- The other sequence
            innerKey -- Expression determining what key to use from the inner objects
            outerKey -- Expression determining what key to use from the outer objects
        
        Keyword Arguments:
            innerTransform -- The transform to apply to the inner objects (default: no transform applied)
            outerTransform -- The transform to apply to the outer objects (default: no transform applied)
        
        Returns:
            Iterable {From} -- Returns a `From` object wrapping a collection of `Joining` objects. Each `Joining` object contains
            the properties `inner` and `outer`. `inner` gives the inner object and `outer` is a collection of all outer object
            paired with said inner object.
        """


        def sequence():
            for innerObj in self:
                outerObjs = (
                    From(extension)
                        .where(lambda x: innerKey(innerObj) == outerKey(x))
                        .select(outerTransform)
                        .toList()
                )
                yield Joining(
                    innerTransform(innerObj), 
                    outerObjs
                )

        return From(x for x in sequence()) 
Example 40
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def join(self, extension, innerKey, outerKey, transform):
        """Joins the sequence of objects with another sequence of objects on the given keys and yields a
        a new sequence of objects according to the transform specified. Equivalent to INNER JOIN in SQL.
        
        Arguments:
            extension {Iterable} -- The sequence to join into the current one
            innerKey -- Expression determining which key to use on the current sequence
            outerKey -- Expression determining which key to use on the extending sequence
            transform -- Expression shaping the objects which to returns. Can be any
            lambda expression or function with two inputs, the inner object followed by the outer object.
        
        Raises:
            ValueError -- If the extension is not Iterable
        
        Returns:
            Iterable {From} -- `From` object wrapping the new sequence of objects.
        """


        if not isinstance(extension, collections.Iterable):
            raise ValueError("Object is not iterable")

        def sequence():
            for x in self:
                outerObjs = From(extension).where(lambda y: innerKey(x) == outerKey(y))
                for outerObj in outerObjs:
                    yield transform(x, outerObj)

        return From(x for x in sequence()) 
Example 41
Project: python-linq   Author: jakkes   File: core.py    MIT License 5 votes vote down vote up
def union(self, outer, key = lambda x: x, transform = lambda x: x):
        """Find the union of two sequences, i.e. all objects that are within either one the two sequences.
        Only unique objects (with respect to the key) are returned.
        
        Arguments:
            outer {Iterable} -- The other sequence
        
        Keyword Arguments:
            key -- Expression determining which key to use for comparison. Key must be hashable (default: uses the elements as is)
            transform -- Expression determining the transform of the elements selected (default: no transform applied)
        
        Raises:
            ValueError -- If `outer` is of instance Iterable
        
        Returns:
            Iterable {From} -- `From` object wrapping the new elements
        """


        if not isinstance(outer, collections.Iterable):
            raise ValueError("Object is not iterable")

        cache = set()

        def sequence():
            for x in self:
                if key(x) in cache:
                    continue
                else:
                    cache.add(key(x))
                    yield transform(x)

            for x in outer:
                if key(x) in cache:
                    continue
                else:
                    cache.add(key(x))
                    yield transform(x)

        return From(x for x in sequence()) 
Example 42
Project: automaton   Author: nazavode   File: automaton.py    Apache License 2.0 5 votes vote down vote up
def __new__(cls, source_states, dest_state):
        # Fix source states:
        if isinstance(source_states, str) or not isinstance(source_states, Iterable):
            source_states = (source_states,)
        instance = super().__new__(cls, source_states, dest_state)
        instance._event_name = None  # pylint: disable=protected-access
        instance._event_delegate = None  # pylint: disable=protected-access
        return instance 
Example 43
Project: thingpin   Author: mgk   File: pin.py    MIT License 5 votes vote down vote up
def __init__(self, observer, pin, sleep, debounce_delay=0, daemon=True):
        """
        Create daemon thread that reports pin changes to an observer callback.

        Args:
            observer (object): object to receive notifications. When a pin
                change is detected the `observer.update_pin(pin, reading)`
                method is called.
            pin (int): pin to watch
            sleep (float): how long to sleep in seconds in each polling loop.
                For testing this can also be an Iterable of floats in which
                case the Thread exits when the Iterable is complete.
            debounce_delay (float): how long a new pin reading has to hold
                steady before it is accepted (and passed to `update_pin()`)
            daemon (bool): whether to run as daemon. Mostly for testing.
        """
        super(Watcher, self).__init__(name='PinWatcher-{}'.format(pin))
        self.daemon = daemon
        self.observer = observer
        self.pin = pin

        if isinstance(sleep, Iterable):
            self.sleep_iter = sleep
        else:
            self.sleep_iter = itertools.repeat(sleep)

        self.debounce_delay = debounce_delay
        self.reading = None
        self.last_reading = None
        self.debounce_time = 0 
Example 44
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, size, interpolation=Image.BILINEAR):
        assert isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)
        self.size = size
        self.interpolation = interpolation 
Example 45
Project: bigquerylayers   Author: smandaric   File: _parameterized.py    GNU General Public License v3.0 5 votes vote down vote up
def _NonStringIterable(obj):
  return (isinstance(obj, collections_abc.Iterable) and not
          isinstance(obj, six.string_types)) 
Example 46
Project: robosuite   Author: StanfordVL   File: placement_sampler.py    MIT License 5 votes vote down vote up
def sample_quat(self):
        if self.z_rotation is None:
            rot_angle = np.random.uniform(high=2 * np.pi, low=0)
        elif isinstance(self.z_rotation, collections.Iterable):
            rot_angle = np.random.uniform(
                high=max(self.z_rotation), low=min(self.z_rotation)
            )
        else:
            rot_angle = self.z_rotation

        return [np.cos(rot_angle / 2), 0, 0, np.sin(rot_angle / 2)] 
Example 47
Project: RNASEqTool   Author: armell   File: mpld3_extra.py    MIT License 5 votes vote down vote up
def _determine_mpld3ids(self, plot_elements):
        """
        Helper function to get the mpld3_id for each
        of the specified elements.
        """
        mpld3_element_ids = []

        # There are two things being done here. First,
        # we make sure that we have a list of lists, where
        # each inner list is associated with a single legend
        # item. Second, in case of Line2D object we pass
        # the id for both the marker and the line.
        # on the javascript side we filter out the nulls in
        # case either the line or the marker has no equivalent
        # D3 representation.
        for entry in plot_elements:
            ids = []
            if isinstance(entry, collections.Iterable):
                for element in entry:
                    mpld3_id = get_id(element)
                    ids.append(mpld3_id)
                    if isinstance(element, matplotlib.lines.Line2D):
                        mpld3_id = get_id(element, 'pts')
                        ids.append(mpld3_id)
            else:
                ids.append(get_id(entry))
                if isinstance(entry, matplotlib.lines.Line2D):
                    mpld3_id = get_id(entry, 'pts')
                    ids.append(mpld3_id)
            mpld3_element_ids.append(ids)

        return mpld3_element_ids 
Example 48
Project: ACAN   Author: miraiaroha   File: transforms.py    MIT License 5 votes vote down vote up
def __init__(self, size, interpolation='nearest'):
        assert isinstance(size, int) or isinstance(size, float) or \
               (isinstance(size, collections.Iterable) and len(size) == 2)
        self.size = size
        self.interpolation = interpolation 
Example 49
Project: lung_nodule_classifier   Author: xairc   File: data.py    MIT License 5 votes vote down vote up
def collate(batch):
    if torch.is_tensor(batch[0]):
        return [b.unsqueeze(0) for b in batch]
    elif isinstance(batch[0], np.ndarray):
        return batch
    elif isinstance(batch[0], int):
        return torch.LongTensor(batch)
    elif isinstance(batch[0], collections.Iterable):
        transposed = zip(*batch)
        return [collate(samples) for samples in transposed] 
Example 50
Project: mpu   Author: MartinThoma   File: datastructures.py    MIT License 5 votes vote down vote up
def flatten(iterable, string_flattening=False):
    """
    Flatten an given iterable of iterables into one list.

    Parameters
    ----------
    iterable : iterable
    string_flattening : bool
        If this is False, then strings are NOT flattened

    Returns
    -------
    flat_list : List

    Examples
    --------
    >>> flatten([1, [2, [3]]])
    [1, 2, 3]

    >>> flatten(((1, 2), (3, 4), (5, 6)))
    [1, 2, 3, 4, 5, 6]

    >>> flatten(EList([EList([1, 2]), (3, [4, [[5]]])]))
    [1, 2, 3, 4, 5]
    """
    flat_list = []
    for item in iterable:
        is_iterable = isinstance(item, collections.Iterable) and (
            string_flattening or (not isinstance(item, str))
        )
        if is_iterable:
            flat_list.extend(flatten(item))
        else:
            flat_list.append(item)
    return flat_list 
Example 51
Project: specio   Author: paris-saclay-cds   File: format.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_meta_data(self, index):
            """Plugins must implement this.

            It should return the meta data as a dict, corresponding to the
            given index, or to the file's (global) meta data if index is None.

            """
            if index is not None and isinstance(self._data.meta, Iterable):
                return self._data.meta[index]
            else:
                return self._data.meta 
Example 52
Project: ns1-python   Author: ns1   File: records.py    MIT License 5 votes vote down vote up
def _getAnswersForBody(self, answers):
        realAnswers = []
        # simplest: they specify a single string ip
        if isinstance(answers, py_str):
            answers = [answers]
        # otherwise, we need an iterable
        elif not isinstance(answers, collections.Iterable):
            raise Exception('invalid answers format (must be str or iterable)')
        # at this point we have a list. loop through and build out the answer
        # entries depending on contents
        for a in answers:
            if isinstance(a, py_str):
                realAnswers.append({'answer': [a]})
            elif isinstance(a, (list, tuple)):
                realAnswers.append({'answer': a})
            elif isinstance(a, dict):
                realAnswers.append(a)
            else:
                raise Exception('invalid answers format: list must contain '
                                'only str, list, or dict')
        return realAnswers

    # filters must be a list of dict which can have two forms:
    # 1) simple: each item in list is a dict with a single key and value. the
    #            key is the name of the filter, the value is a dict of config
    #            values (which may be empty {})
    # 2) full: each item in the list is a dict of the full rest model for
    #          filters (documented elsewhere) which is passed through. use this
    #          for enabled/disabled or future fields not supported otherwise
    # 
Example 53
Project: DeepLung   Author: uci-cbcl   File: data.py    GNU General Public License v3.0 5 votes vote down vote up
def collate(batch):
    if torch.is_tensor(batch[0]):
        return [b.unsqueeze(0) for b in batch]
    elif isinstance(batch[0], np.ndarray):
        return batch
    elif isinstance(batch[0], int):
        return torch.LongTensor(batch)
    elif isinstance(batch[0], collections.Iterable):
        transposed = zip(*batch)
        return [collate(samples) for samples in transposed] 
Example 54
Project: DeepLung   Author: uci-cbcl   File: transforms.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, size, interpolation=Image.BILINEAR):
        # assert isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 3)
        self.size = size
        self.interpolation = interpolation 
Example 55
Project: DeepLung   Author: uci-cbcl   File: transforms.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, size, interpolation=Image.BILINEAR):
        assert isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 3)
        self.size = size
        self.interpolation = interpolation 
Example 56
Project: matchminer-engine   Author: dfci   File: cerberus.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _validate_maxlength(self, max_length, field, value):
        """ {'type': 'integer'} """
        if isinstance(value, Iterable) and len(value) > max_length:
                self._error(field, errors.MAX_LENGTH, len(value)) 
Example 57
Project: matchminer-engine   Author: dfci   File: cerberus.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _validate_minlength(self, min_length, field, value):
        """ {'type': 'integer'} """
        if isinstance(value, Iterable) and len(value) < min_length:
                self._error(field, errors.MIN_LENGTH, len(value)) 
Example 58
Project: matchminer-engine   Author: dfci   File: schema.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _validator_handler(self, field, value):
        if isinstance(value, Callable):
            return
        if isinstance(value, _str_type):
            if value not in self.target_validator.validators and \
                    value not in self.target_validator.coercers:
                self._error(field, '%s is no valid coercer' % value)
        elif isinstance(value, Iterable):
            for handler in value:
                self._validator_handler(field, handler) 
Example 59
Project: MultilayerGM-py   Author: MultilayerGM   File: dependency_tensors.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, n_nodes, n_layers, p):
        """
        :param n_nodes: number of physical nodes
        :param n_layers: number of layers
        :param p: copying probabilities (probability that a state-node obtains a new mesoset assingment by copying the
                  mesoset assignment of a different state-node during an update). This can either be a single probability
                  (in which case the copying probability is the same for each layer) or a list of probabilities of length
                  `n_layers-1` (in which case p[i-1] is the probability that a state node in layer i copies its mesoset
                  assignment from the corresponding state node in layer i-1).
        """
        super().__init__((n_nodes, n_layers), 'o')
        if isinstance(p, Iterable):
            self.p = p
        else:
            self.p = [p] * (n_layers-1) 
Example 60
Project: CrossLingualDepParser   Author: uclanlp   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def _ntuple(n):
    def parse(x):
        if isinstance(x, collections.Iterable):
            return x
        return tuple(repeat(x, n))
    return parse 
Example 61
Project: graphtimer   Author: Peilonrayz   File: graphtimer.py    MIT License 5 votes vote down vote up
def flat(i):
    if hasattr(i, 'flat'):
        return i.flat
    if not isinstance(i, collections.Iterable) or isinstance(i, str):
        return [i]
    list_ = i
    while True:
        if any(not isinstance(item, collections.Iterable) or isinstance(item, str) for item in list_):
            return list_
        list_ = [item for iterable in list_ for item in iterable] 
Example 62
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: bn.py    MIT License 5 votes vote down vote up
def _pair(x):
    if isinstance(x, Iterable):
        return x
    return tuple(repeat(x, 2)) 
Example 63
Project: python-tabulate   Author: cmck   File: tabulate.py    MIT License 5 votes vote down vote up
def _expand_numparse(disable_numparse, column_count):
    """
    Return a list of bools of length `column_count` which indicates whether
    number parsing should be used on each column.
    If `disable_numparse` is a list of indices, each of those indices are False,
    and everything else is True.
    If `disable_numparse` is a bool, then the returned list is all the same.
    """
    if isinstance(disable_numparse, Iterable):
        numparses = [True] * column_count
        for index in disable_numparse:
            numparses[index] = False
        return numparses
    else:
        return [not disable_numparse] * column_count 
Example 64
Project: Repobot   Author: Desgard   File: newstr.py    MIT License 5 votes vote down vote up
def startswith(self, prefix, *args):
        if isinstance(prefix, Iterable):
            for thing in prefix:
                if isnewbytes(thing):
                    raise TypeError(self.no_convert_msg.format(type(thing)))
        return super(newstr, self).startswith(prefix, *args) 
Example 65
Project: Repobot   Author: Desgard   File: newstr.py    MIT License 5 votes vote down vote up
def endswith(self, prefix, *args):
        # Note we need the decorator above as well as the isnewbytes()
        # check because prefix can be either a bytes object or e.g. a
        # tuple of possible prefixes. (If it's a bytes object, each item
        # in it is an int.)
        if isinstance(prefix, Iterable):
            for thing in prefix:
                if isnewbytes(thing):
                    raise TypeError(self.no_convert_msg.format(type(thing)))
        return super(newstr, self).endswith(prefix, *args) 
Example 66
Project: Repobot   Author: Desgard   File: jws.py    MIT License 5 votes vote down vote up
def _get_keys(key):

    try:
        key = json.loads(key)
    except Exception:
        pass

    # JWK Set per RFC 7517
    if 'keys' in key:
        return key['keys']

    # Individual JWK per RFC 7517
    elif 'kty' in key:
        return (key,)

    # Some other mapping. Firebase uses just dict of kid, cert pairs
    elif isinstance(key, Mapping):
        values = key.values()
        if values:
            return values
        return (key,)

    # Iterable but not text or mapping => list- or tuple-like
    elif (isinstance(key, Iterable) and
          not (isinstance(key, six.string_types) or isinstance(key, Mapping))):
        return key

    # Scalar value, wrap in tuple.
    else:
        return (key,) 
Example 67
Project: asyncloop   Author: dgkim5360   File: test_thread.py    MIT License 5 votes vote down vote up
def test_submit_many(aloop):
    jobs = (simple_job(.2) for i in range(5))
    futs = aloop.submit_many(jobs, simple_callback)
    assert isinstance(futs, collections.Iterable)

    assert all(fut.done() is False for fut in futs)
    time.sleep(.3)
    assert all(fut.done() for fut in futs) 
Example 68
Project: GeoPy   Author: aerler   File: nctools.py    GNU General Public License v3.0 5 votes vote down vote up
def coerceAtts(atts):
  ''' Convert an attribute dictionary to a NetCDF compatible format. '''
  if not isinstance(atts,dict): raise TypeError
  ncatts = dict()
  # loop over items
  for key,value in atts.items():
    if key in ('missing_value','fillValue','_FillValue'): pass
    # N.B.: these are special attributes that the NetCDF module will try to read
    elif isinstance(key,str) and key[0] == '_' : pass # skip (internal attributes)
    elif value is None: pass # skip (invalid value / not assigned)
    elif not isinstance(value,(str,np.ndarray,np.inexact,float,np.integer,int)):
      if 'name' in dir(value):
        ncatts[key] = value.name # mostly for datasets and variables
      elif isinstance(value,col.Iterable):
        if len(value) == 0: ncatts[key] = '' # empty attribute
        elif all([isinstance(val,(int,np.integer,float,np.inexact)) for val in value]):
          # N.B.: int & float are not part of their numpy equivalents
          ncatts[key] = np.array(value)         
        else:
          l = '(' # fake list representation
          for elt in value[0:-1]: l += '{0:s}, '.format(str(elt))
          l += '{0:s})'.format(str(value[-1]))
          ncatts[key] = l          
      else: ncatts[key] = str(value) 
    else: ncatts[key] = value
  return ncatts


## generic netcdf functions 
Example 69
Project: GeoPy   Author: aerler   File: netcdf.py    GNU General Public License v3.0 5 votes vote down vote up
def replaceVariable(self, oldvar, newvar=None, asNC=False, deepcopy=False):
    ''' Replace an existing Variable with a different one and transfer NetCDF reference and axes. '''
    if newvar is None: 
      newvar = oldvar; oldvar = newvar.name # i.e. replace old var with the same name
    # check var
    if not self.hasVariable(oldvar): raise VariableError
    # special treatment for VarNC: transfer of ownership of NetCDF variable
    if asNC or isinstance(newvar,VarNC):
      # resolve names
      if isinstance(oldvar,Variable): oldname = oldvar.name # just go by name
      else: oldname = oldvar
      oldvar = self.variables[oldname]
      if oldvar.shape != newvar.shape: raise AxisError # shape has to be the same!
      # N.B.: the shape of a variable in a NetCDF file can't change!
      # remove old variable from dataset...
      self.removeVariable(oldvar) # this is actually the ordinary Dataset class method that doesn't do anything to the NetCDF file
      # cast new variable as VarNC and transfer old ncvar reference and axes    
      newvar = asVarNC(var=newvar,ncvar=oldvar.ncvar, axes=oldvar.axes, mode=oldvar.mode, deepcopy=deepcopy)
      # ... and add new axis to dataset
      self.addVariable(newvar, copy=False, loverwrite=False)
    else: # no need for special treatment...
      super(DatasetNetCDF,self).replaceVariable(oldvar, newvar)
    # return status of variable
    return self.hasVariable(newvar)  
  
#   def load(self, **slices):
#     ''' Load all VarNC's and AxisNC's using the slices specified as keyword arguments. '''
#     # make slices
#     for key,value in slices.iteritems():
#       if isinstance(value,col.Iterable): 
#         slices[key] = slice(*value)
#       else: 
#         if not isinstance(value,(int,np.integer)): raise TypeError
#     # load variables
#     for var in self.variables.values():
#       if isinstance(var,VarNC):
#         idx = [slices.get(ax.name,slice(None)) for ax in var.axes]
#         var.load(data=idx) # load slice, along with relevant dimensions
#     # no return value...     
#     # return itself- this allows for some convenient syntax
#     return self 
Example 70
Project: pyfilter   Author: tingiskhan   File: utils.py    MIT License 5 votes vote down vote up
def _recursion_helper(a, f):
    """
    Helper for performing recursions.
    :param a: The object
    :type a: class
    :param f: The callable
    :type f: callable
    :rtype: object
    """

    if isinstance(a, torch.Tensor):
        if a._base is not None:
            return a
        elif isinstance(a, Parameter):
            _recursion_helper(a._prior, f)

        a.data = f(a.data)

        if a._grad is not None:
            a._grad.data = f(a._grad.data)

    elif isinstance(a, HelperMixin):
        a.apply(f)
    elif isinstance(a, Iterable) and not isinstance(a, str):
        for item in (a if not isinstance(a, dict) else a.values()):
            _recursion_helper(item, f)
    elif isinstance(a, (Distribution, Transform)):
        for _, at in _yield_objs(a):
            _recursion_helper(at, f)

    return a 
Example 71
Project: uplink   Author: prkumar   File: auth.py    MIT License 5 votes vote down vote up
def get_auth(auth_object=None):
    if auth_object is None:
        return utils.no_op
    elif isinstance(auth_object, collections.Iterable):
        return BasicAuth(*auth_object)
    elif callable(auth_object):
        return auth_object
    else:
        raise ValueError("Invalid authentication strategy: %s" % auth_object) 
Example 72
Project: chainerrl   Author: chainer   File: recurrent.py    MIT License 5 votes vote down vote up
def unchain_backward(state):
    """Call Variable.unchain_backward recursively."""
    if isinstance(state, collections.Iterable):
        for s in state:
            unchain_backward(s)
    elif isinstance(state, chainer.Variable):
        state.unchain_backward() 
Example 73
Project: LambdaML   Author: ianchute   File: lambda_utils.py    MIT License 5 votes vote down vote up
def prime(f, p, key, X, Y):
        """Numerically computes the first derivative of f with respect to p[key]"""
        if isinstance(p[key], Iterable):
            return np.array(list(map(
                lambda ix: LambdaUtils._prime(LambdaUtils.dependent_a(f,p,key,ix[0],X,Y), p[key][ix[0]]), 
                enumerate(p[key]))))
        else:
            return LambdaUtils._prime(LambdaUtils.dependent(f,p,key,X,Y), p[key]) 
Example 74
Project: LambdaML   Author: ianchute   File: lambda_utils.py    MIT License 5 votes vote down vote up
def l1_regularization(p):
        """Computes the L1 Regularization Term of the parameter set p"""
        total = 0
        for v in p.values():
            if isinstance(v, Iterable):
                total += v.sum()
            else:
                total += v
        return total 
Example 75
Project: LambdaML   Author: ianchute   File: lambda_utils.py    MIT License 5 votes vote down vote up
def l2_regularization(p):
        """Computes the L2 Regularization Term of the parameter set p"""
        total = 0
        for v in p.values():
            if isinstance(v, Iterable):
                total += np.square(v).sum()
            else:
                total += np.square(v)
        return total 
Example 76
Project: automaton   Author: nazavode   File: automaton.py    Apache License 2.0 4 votes vote down vote up
def __new__(mcs, class_name, class_bases, class_dict):
        cls = super().__new__(mcs, class_name, class_bases, class_dict)
        events = {}
        states = set()
        for attr in dir(cls):
            value = getattr(cls, attr)
            if isinstance(value, Event):
                # Important: bind event to its name as a class member
                value._bind(attr)  # pylint: disable=protected-access
                # Collect states
                states.update(value[0])  # Iterable of sources states, let's update the set
                states.add(value[1])
                # Collect events
                events[attr] = value
        if len(states) != 0:  # if we are dealing with an automaton definition class...
            # 1. Setup class members:
            cls.__states__ = states
            cls.__events__ = events
            if cls.__default_initial_state__ is not None \
                    and cls.__default_initial_state__ not in cls.__states__:
                raise DefinitionError("Default initial state '{}' unknown.".format(cls.__default_initial_state__))
            if cls.__default_accepting_states__ is not None:
                for state in cls.__default_accepting_states__:
                    if state not in cls.__states__:
                        raise DefinitionError("Default accepting state '{}' unknown.".format(state))
            # 2. Build graph
            graph = nx.MultiDiGraph()
            graph.add_nodes_from(states)
            graph.add_edges_from(chain.from_iterable(list(event.edges(data=True)) for event in events.values()))
            # 3. Check states graph consistency:
            if not nx.is_weakly_connected(graph):
                components = list(nx.weakly_connected_component_subgraphs(graph))
                raise DefinitionError(
                    "The state graph contains {} connected components: {}".format(
                        len(components), ", ".join("{}".format(c.nodes()) for c in components))
                )
            # 4. Save
            cls.__graph__ = nx.freeze(graph)
            # 5. Docstring substitution
            if cls.__doc__:
                cls.__doc__ = cls.__doc__.format(automaton=cls)

        return cls 
Example 77
Project: pyspark-cassandra   Author: TargetHolding   File: util.py    Apache License 2.0 4 votes vote down vote up
def as_java_object(gateway, obj):
    """
        Converts a limited set of types to their corresponding types in java. Supported are
        'primitives' (which aren't converted), datetime.datetime and the set-, dict- and
        iterable-like types.
    """

    if obj is None:
        return None

    t = type(obj)

    if issubclass(t, (bool, int, float, str)):
        return obj

    elif issubclass(t, UDT):
        field_names = as_java_array(gateway, "String", obj.keys())
        field_values = as_java_array(gateway, "Object", obj.values())
        udt = gateway.jvm.UDTValueConverter(field_names, field_values)
        return udt.toConnectorType()

    elif issubclass(t, datetime):
        timestamp = int(mktime(obj.timetuple()) * 1000)
        return gateway.jvm.java.util.Date(timestamp)

    elif issubclass(t, (dict, Mapping)):
        hash_map = gateway.jvm.java.util.HashMap()
        for (k, v) in obj.items(): hash_map[k] = v
        return hash_map

    elif issubclass(t, (set, Set)):
        hash_set = gateway.jvm.java.util.HashSet()
        for e in obj: hash_set.add(e)
        return hash_set

    elif issubclass(t, (list, Iterable)):
        array_list = gateway.jvm.java.util.ArrayList()
        for e in obj: array_list.append(e)
        return array_list

    else:
        return obj 
Example 78
Project: Repobot   Author: Desgard   File: oldstr.py    MIT License 4 votes vote down vote up
def __dir__(self):
        return [thing for thing in dir(_builtin_bytes) if thing != '__iter__']

    # def __new__(cls, *args, **kwargs):
    #     """
    #     From the Py3 bytes docstring:

    #     bytes(iterable_of_ints) -> bytes
    #     bytes(string, encoding[, errors]) -> bytes
    #     bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
    #     bytes(int) -> bytes object of size given by the parameter initialized with null bytes
    #     bytes() -> empty bytes object
    #     
    #     Construct an immutable array of bytes from:
    #       - an iterable yielding integers in range(256)
    #       - a text string encoded using the specified encoding
    #       - any object implementing the buffer API.
    #       - an integer
    #     """
    #     
    #     if len(args) == 0:
    #         return super(newbytes, cls).__new__(cls)
    #     # Was: elif isinstance(args[0], newbytes):
    #     # We use type() instead of the above because we're redefining
    #     # this to be True for all unicode string subclasses. Warning:
    #     # This may render newstr un-subclassable.
    #     elif type(args[0]) == newbytes:
    #         return args[0]
    #     elif isinstance(args[0], _builtin_bytes):
    #         value = args[0]
    #     elif isinstance(args[0], unicode):
    #         if 'encoding' not in kwargs:
    #             raise TypeError('unicode string argument without an encoding')
    #         ###
    #         # Was:   value = args[0].encode(**kwargs)
    #         # Python 2.6 string encode() method doesn't take kwargs:
    #         # Use this instead:
    #         newargs = [kwargs['encoding']]
    #         if 'errors' in kwargs:
    #             newargs.append(kwargs['errors'])
    #         value = args[0].encode(*newargs)
    #         ### 
    #     elif isinstance(args[0], Iterable):
    #         if len(args[0]) == 0:
    #             # What is this?
    #             raise ValueError('unknown argument type')
    #         elif len(args[0]) > 0 and isinstance(args[0][0], Integral):
    #             # It's a list of integers
    #             value = b''.join([chr(x) for x in args[0]])
    #         else:
    #             raise ValueError('item cannot be interpreted as an integer')
    #     elif isinstance(args[0], Integral):
    #         if args[0] < 0:
    #             raise ValueError('negative count')
    #         value = b'\x00' * args[0]
    #     else:
    #         value = args[0]
    #     return super(newbytes, cls).__new__(cls, value) 
Example 79
Project: Repobot   Author: Desgard   File: client.py    MIT License 4 votes vote down vote up
def send(self, data):
        """Send `data' to the server.
        ``data`` can be a string object, a bytes object, an array object, a
        file-like object that supports a .read() method, or an iterable object.
        """

        if self.sock is None:
            if self.auto_open:
                self.connect()
            else:
                raise NotConnected()

        if self.debuglevel > 0:
            print("send:", repr(data))
        blocksize = 8192
        # Python 2.7 array objects have a read method which is incompatible
        # with the 2-arg calling syntax below.
        if hasattr(data, "read") and not isinstance(data, array):
            if self.debuglevel > 0:
                print("sendIng a read()able")
            encode = False
            try:
                mode = data.mode
            except AttributeError:
                # io.BytesIO and other file-like objects don't have a `mode`
                # attribute.
                pass
            else:
                if "b" not in mode:
                    encode = True
                    if self.debuglevel > 0:
                        print("encoding file using iso-8859-1")
            while 1:
                datablock = data.read(blocksize)
                if not datablock:
                    break
                if encode:
                    datablock = datablock.encode("iso-8859-1")
                self.sock.sendall(datablock)
            return
        try:
            self.sock.sendall(data)
        except TypeError:
            if isinstance(data, collections.Iterable):
                for d in data:
                    self.sock.sendall(d)
            else:
                raise TypeError("data should be a bytes-like object "
                                "or an iterable, got %r" % type(data)) 
Example 80
Project: Repobot   Author: Desgard   File: request.py    MIT License 4 votes vote down vote up
def do_request_(self, request):
        host = request.host
        if not host:
            raise URLError('no host given')

        if request.data is not None:  # POST
            data = request.data
            if isinstance(data, str):
                msg = "POST data should be bytes or an iterable of bytes. " \
                      "It cannot be of type str."
                raise TypeError(msg)
            if not request.has_header('Content-type'):
                request.add_unredirected_header(
                    'Content-type',
                    'application/x-www-form-urlencoded')
            if not request.has_header('Content-length'):
                size = None
                try:
                    ### For Python-Future:
                    if PY2 and isinstance(data, array.array):
                        # memoryviews of arrays aren't supported
                        # in Py2.7. (e.g. memoryview(array.array('I',
                        # [1, 2, 3, 4])) raises a TypeError.)
                        # So we calculate the size manually instead:
                        size = len(data) * data.itemsize
                    ###
                    else:
                        mv = memoryview(data)
                        size = len(mv) * mv.itemsize
                except TypeError:
                    if isinstance(data, collections.Iterable):
                        raise ValueError("Content-Length should be specified "
                                "for iterable data of type %r %r" % (type(data),
                                data))
                else:
                    request.add_unredirected_header(
                            'Content-length', '%d' % size)

        sel_host = host
        if request.has_proxy():
            scheme, sel = splittype(request.selector)
            sel_host, sel_path = splithost(sel)
        if not request.has_header('Host'):
            request.add_unredirected_header('Host', sel_host)
        for name, value in self.parent.addheaders:
            name = name.capitalize()
            if not request.has_header(name):
                request.add_unredirected_header(name, value)

        return request