Python itertools.count() Examples

The following are 30 code examples for showing how to use itertools.count(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module itertools , or try the search function .

Example 1
Project: imgcomp-cvpr   Author: fab-jul   File: other_codecs.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _jpeg_content_length(p):
    """
    Determines the length of the content of the JPEG file stored at `p` in bytes, i.e., size of the file without the
    header. Note: Note sure if this works for all JPEGs...
    :param p: path to a JPEG file
    :return: length of content
    """
    with open(p, 'rb') as f:
        last_byte = ''
        header_end_i = None
        for i in itertools.count():
            current_byte = f.read(1)
            if current_byte == b'':
                break
            # some files somehow contain multiple FF DA sequences, don't know what that means
            if header_end_i is None and last_byte == b'\xff' and current_byte == b'\xda':
                header_end_i = i
            last_byte = current_byte
        # at this point, i is equal to the size of the file
        return i - header_end_i - 2  # minus 2 because all JPEG files end in FF D0 
Example 2
Project: pywren-ibm-cloud   Author: pywren   File: storage.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, pywren_config, storage_backend):
        self.pywren_config = pywren_config
        self.backend = storage_backend

        self._created_cobjects_n = itertools.count()

        try:
            module_location = 'pywren_ibm_cloud.storage.backends.{}'.format(self.backend)
            sb_module = importlib.import_module(module_location)
            storage_config = self.pywren_config[self.backend]
            storage_config['user_agent'] = 'pywren-ibm-cloud/{}'.format(__version__)
            StorageBackend = getattr(sb_module, 'StorageBackend')
            self.storage_handler = StorageBackend(storage_config)
        except Exception as e:
            raise NotImplementedError("An exception was produced trying to create the "
                                      "'{}' storage backend: {}".format(self.backend, e)) 
Example 3
Project: pywren-ibm-cloud   Author: pywren   File: storage.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, storage_config, executor_id=None):
        self.config = storage_config
        self.backend = self.config['backend']
        self.bucket = self.config['bucket']
        self.executor_id = executor_id

        self._created_cobjects_n = itertools.count()

        try:
            module_location = 'pywren_ibm_cloud.storage.backends.{}'.format(self.backend)
            sb_module = importlib.import_module(module_location)
            StorageBackend = getattr(sb_module, 'StorageBackend')
            self.storage_handler = StorageBackend(self.config[self.backend],
                                                  bucket=self.bucket,
                                                  executor_id=self.executor_id)
        except Exception as e:
            raise NotImplementedError("An exception was produced trying to create the "
                                      "'{}' storage backend: {}".format(self.backend, e)) 
Example 4
Project: pyscf   Author: pyscf   File: common_slow.py    License: Apache License 2.0 6 votes vote down vote up
def format_mask(x):
    """
    Formats a mask into a readable string.
    Args:
        x (ndarray): an array with the mask;

    Returns:
        A readable string with the mask.
    """
    x = numpy.asanyarray(x)
    if len(x) == 0:
        return "(empty)"
    if x.dtype == bool:
        x = numpy.argwhere(x)[:, 0]
    grps = tuple(list(g) for _, g in groupby(x, lambda n, c=count(): n-next(c)))
    return ",".join("{:d}-{:d}".format(i[0], i[-1]) if len(i) > 1 else "{:d}".format(i[0]) for i in grps) 
Example 5
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(*args, **kwds):
        '''Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.

        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args

        '''
        if not args:
            raise TypeError("descriptor '__init__' of 'Counter' object "
                            "needs an argument")
        self = args[0]
        args = args[1:]
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        super(Counter, self).__init__()
        self.update(*args, **kwds) 
Example 6
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.items()))

    # Override dict methods where necessary 
Example 7
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __add__(self, other):
        '''Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count + other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
Example 8
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __sub__(self, other):
        ''' Subtract count, but keep only results with positive counts.

        >>> Counter('abbbc') - Counter('bccd')
        Counter({'b': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count - other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count < 0:
                result[elem] = 0 - count
        return result 
Example 9
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __or__(self, other):
        '''Union is the maximum of value in either of the input counters.

        >>> Counter('abbb') | Counter('bcc')
        Counter({'b': 3, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            other_count = other[elem]
            newcount = other_count if count < other_count else count
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
Example 10
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __and__(self, other):
        ''' Intersection is the minimum of corresponding counts.

        >>> Counter('abbb') & Counter('bcc')
        Counter({'b': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            other_count = other[elem]
            newcount = count if count < other_count else other_count
            if newcount > 0:
                result[elem] = newcount
        return result 
Example 11
Project: verge3d-blender-addon   Author: Soft8Soft   File: misc.py    License: GNU General Public License v3.0 6 votes vote down vote up
def count(start=0, step=1):
    """
    ``itertools.count`` in Py 2.6 doesn't accept a step
    parameter. This is an enhanced version of ``itertools.count``
    for Py2.6 equivalent to ``itertools.count`` in Python 2.7+.
    """
    while True:
        yield start
        start += step


########################################################################
###  ChainMap (helper for configparser and string.Template)
###  From the Py3.4 source code. See also:
###    https://github.com/kkxue/Py2ChainMap/blob/master/py2chainmap.py
######################################################################## 
Example 12
Project: cronosparser   Author: occrp   File: parser.py    License: MIT License 6 votes vote down vote up
def parse_columns(text, base, count):
    # Parse the columns from the table definition. Columns start with
    # a short record length indicator, followed by type and sequence
    # information (each a short), and the name (prefixed by the length).
    columns = []
    for i in range(count):
        if len(text[base:]) < 8:
            break
        col_len, = struct.unpack_from('H', text, base)
        base = base + 2
        if len(text[base:]) < col_len:
            break
        col_data = text[base - 1:base - 1 + col_len]
        type_, col_id = struct.unpack_from('>HH', col_data, 0)
        text_len, = struct.unpack_from('>I', col_data, 4)
        col_name = decode_text(col_data[8:8 + text_len])
        if col_name is None:
            continue
        columns.append({
            'id': col_id,
            'name': col_name,
            'type': type_
        })
        base = base + col_len
    return columns 
Example 13
Project: respy   Author: OpenSourceEconomics   File: method_of_simulated_moments.py    License: MIT License 6 votes vote down vote up
def _flatten_index(data):
    """Flatten the index as a combination of the former index and the columns."""
    data = copy.deepcopy(data)
    data_flat = []
    counter = itertools.count()

    for series_or_df in data:
        series_or_df.index = series_or_df.index.map(str)
        # Unstack pandas.DataFrames and pandas.Series to add
        # columns/name to index.
        if isinstance(series_or_df, pd.DataFrame):
            df = series_or_df.rename(columns=str)
        # pandas.Series without a name are named using a counter to avoid duplicate
        # indexes.
        elif isinstance(series_or_df, pd.Series) and series_or_df.name is None:
            df = series_or_df.to_frame(name=str(next(counter)))
        else:
            df = series_or_df.to_frame(str(series_or_df.name))

        # Columns to the index.
        df = df.unstack()
        df.index = df.index.to_flat_index().str.join("_")
        data_flat.append(df)

    return pd.concat(data_flat) 
Example 14
Project: baseband   Author: mhvk   File: sequentialfile.py    License: GNU General Public License v3.0 6 votes vote down vote up
def read(self, count=None):
        if self.closed:
            raise ValueError('read of closed file.')

        if count is None or count < 0:
            count = max(self.size - self.tell(), 0)

        data = b''
        while count > 0:
            # Go to current offset, possibly opening new file.
            self.seek(0, 1)
            extra = self.fh.read(count)
            if not extra:
                break
            count -= len(extra)
            if not data:  # avoid copies for first read.
                data = extra
            else:
                data += extra

        return data 
Example 15
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(*args, **kwds):
        '''Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.

        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args

        '''
        if not args:
            raise TypeError("descriptor '__init__' of 'Counter' object "
                            "needs an argument")
        self = args[0]
        args = args[1:]
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        super(Counter, self).__init__()
        self.update(*args, **kwds) 
Example 16
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.items()))

    # Override dict methods where necessary 
Example 17
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __add__(self, other):
        '''Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count + other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
Example 18
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __sub__(self, other):
        ''' Subtract count, but keep only results with positive counts.

        >>> Counter('abbbc') - Counter('bccd')
        Counter({'b': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count - other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count < 0:
                result[elem] = 0 - count
        return result 
Example 19
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __or__(self, other):
        '''Union is the maximum of value in either of the input counters.

        >>> Counter('abbb') | Counter('bcc')
        Counter({'b': 3, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            other_count = other[elem]
            newcount = other_count if count < other_count else count
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
Example 20
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(*args, **kwds):
        '''Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.

        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args

        '''
        if not args:
            raise TypeError("descriptor '__init__' of 'Counter' object "
                            "needs an argument")
        self = args[0]
        args = args[1:]
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        super(Counter, self).__init__()
        self.update(*args, **kwds) 
Example 21
Project: misp42splunk   Author: remg427   File: misc.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __add__(self, other):
        '''Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count + other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
Example 22
Project: cherrypy   Author: cherrypy   File: test_refleaks.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_threadlocal_garbage(self):
        if platform.system() == 'Darwin':
            self.skip('queue issues; see #1474')
        success = itertools.count()

        def getpage():
            host = '%s:%s' % (self.interface(), self.PORT)
            if self.scheme == 'https':
                c = HTTPSConnection(host)
            else:
                c = HTTPConnection(host)
            try:
                c.putrequest('GET', '/')
                c.endheaders()
                response = c.getresponse()
                body = response.read()
                self.assertEqual(response.status, 200)
                self.assertEqual(body, b'Hello world!')
            finally:
                c.close()
            next(success)

        ITERATIONS = 25

        ts = [
            threading.Thread(target=getpage)
            for _ in range(ITERATIONS)
        ]

        for t in ts:
            t.start()

        for t in ts:
            t.join()

        self.assertEqual(next(success), ITERATIONS) 
Example 23
Project: python-clean-architecture   Author: pcah   File: in_memory.py    License: MIT License 5 votes vote down vote up
def __init__(self, initial_content: BatchOfKwargs = None):
        self._register: t.Dict[int, Dto] = {}
        self._id_generator = count(1)
        if initial_content:
            self.batch_insert(initial_content) 
Example 24
Project: python-clean-architecture   Author: pcah   File: entity.py    License: MIT License 5 votes vote down vote up
def __init__(self):
        from itertools import count
        self._counter = count(1)
        super().__init__(generator=lambda: next(self._counter)) 
Example 25
Project: jsonrpcclient   Author: bcb   File: id_generators.py    License: MIT License 5 votes vote down vote up
def decimal(start: int = 1) -> Iterator[int]:
    """
    Increments from `start`.

    e.g. 1, 2, 3, .. 9, 10, 11, etc.

    Args:
        start: The first value to start with.
    """
    return itertools.count(start) 
Example 26
Project: jsonrpcclient   Author: bcb   File: test_http_client.py    License: MIT License 5 votes vote down vote up
def setup_method(self):
        # Patch Request.id_generator to ensure the id is always 1
        Request.id_generator = itertools.count(1) 
Example 27
Project: jsonrpcclient   Author: bcb   File: test_http_client.py    License: MIT License 5 votes vote down vote up
def setup_method(self):
        # Patch Request.id_iterator to ensure the id is always 1
        Request.id_iterator = itertools.count(1) 
Example 28
Project: jsonrpcclient   Author: bcb   File: test_zeromq_client.py    License: MIT License 5 votes vote down vote up
def setup_method(self):
        # Patch Request.id_generator to ensure the request id is always 1
        Request.id_generator = itertools.count(1) 
Example 29
Project: jsonrpcclient   Author: bcb   File: test_tornado_client.py    License: MIT License 5 votes vote down vote up
def setup_method(self, *_):
        # Patch Request.id_generator to ensure the id is always 1
        Request.id_generator = itertools.count(1) 
Example 30
Project: BAMnet   Author: hugochan   File: build_data.py    License: Apache License 2.0 5 votes vote down vote up
def build_vocab(data, freebase, used_fbkeys=None, min_freq=1):
    entities, entity_types, relations, kb_vocabs = build_kb_data(freebase, used_fbkeys)

    # Entity
    all_entities = set({ent for ent in entities if entities[ent] >= min_freq})
    entity2id = dict(zip(all_entities, range(len(config.RESERVED_ENTS), len(all_entities) + len(config.RESERVED_ENTS))))
    for ent, idx in config.RESERVED_ENTS.items():
        entity2id.update({ent: idx})

    # Entity type
    all_ent_types = set({ent_type for ent_type in entity_types if entity_types[ent_type] >= min_freq})
    all_ent_types.update(config.extra_ent_types)
    entityType2id = dict(zip(all_ent_types, range(len(config.RESERVED_ENT_TYPES), len(all_ent_types) + len(config.RESERVED_ENT_TYPES))))
    for ent_type, idx in config.RESERVED_ENT_TYPES.items():
        entityType2id.update({ent_type: idx})

    # Relation
    all_relations = set({rel for rel in relations if relations[rel] >= min_freq})
    all_relations.update(config.extra_rels)
    relation2id = dict(zip(all_relations, range(len(config.RESERVED_RELS), len(all_relations) + len(config.RESERVED_RELS))))
    for rel, idx in config.RESERVED_RELS.items():
        relation2id.update({rel: idx})

    # Vocab
    vocabs = build_qa_vocab(data)
    for token, count in kb_vocabs.items():
        vocabs[token] += count
    # sorted_vocabs = sorted(vocabs.items(), key=lambda d:d[1], reverse=True)
    all_tokens = set({token for token in vocabs if vocabs[token] >= min_freq})
    all_tokens.update(config.extra_vocab_tokens)
    vocab2id = dict(zip(all_tokens, range(len(config.RESERVED_TOKENS), len(all_tokens) + len(config.RESERVED_TOKENS))))
    for token, idx in config.RESERVED_TOKENS.items():
        vocab2id.update({token: idx})

    print('Num of entities: %s' % len(entity2id))
    print('Num of entity_types: %s' % len(entityType2id))
    print('Num of relations: %s' % len(relation2id))
    print('Num of vocabs: %s' % len(vocab2id))
    return entity2id, entityType2id, relation2id, vocab2id