Python itertools.count() Examples
The following are 30
code examples of itertools.count().
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 also want to check out all available functions/classes of the module
itertools
, or try the search function
.
Example #1
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #2
Source File: other_codecs.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
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 #3
Source File: storage.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
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 #4
Source File: storage.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
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 #5
Source File: common_slow.py From pyscf with Apache License 2.0 | 6 votes |
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 #6
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #7
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #8
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #9
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #10
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #11
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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
Source File: method_of_simulated_moments.py From respy with MIT License | 6 votes |
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 #13
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
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 #14
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #15
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #16
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #17
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #19
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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 #20
Source File: sequentialfile.py From baseband with GNU General Public License v3.0 | 6 votes |
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 #21
Source File: parser.py From cronosparser with MIT License | 6 votes |
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 #22
Source File: test_http_client.py From jsonrpcclient with MIT License | 5 votes |
def setup_method(self): # Patch Request.id_generator to ensure the id is always 1 Request.id_generator = itertools.count(1)
Example #23
Source File: id_generators.py From jsonrpcclient with MIT License | 5 votes |
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 #24
Source File: test_http_client.py From jsonrpcclient with MIT License | 5 votes |
def setup_method(self): # Patch Request.id_iterator to ensure the id is always 1 Request.id_iterator = itertools.count(1)
Example #25
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def subtract(*args, **kwds): '''Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts. Source can be an iterable, a dictionary, or another Counter instance. >>> c = Counter('which') >>> c.subtract('witch') # subtract elements from another iterable >>> c.subtract(Counter('watch')) # subtract elements from another counter >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch 0 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch -1 ''' if not args: raise TypeError("descriptor 'subtract' 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)) iterable = args[0] if args else None if iterable is not None: self_get = self.get if isinstance(iterable, Mapping): for elem, count in iterable.items(): self[elem] = self_get(elem, 0) - count else: for elem in iterable: self[elem] = self_get(elem, 0) - 1 if kwds: self.subtract(kwds)
Example #26
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def update(*args, **kwds): '''Like dict.update() but add counts instead of replacing them. Source can be an iterable, a dictionary, or another Counter instance. >>> c = Counter('which') >>> c.update('witch') # add elements from another iterable >>> d = Counter('watch') >>> c.update(d) # add elements from another counter >>> c['h'] # four 'h' in which, witch, and watch 4 ''' # The regular dict.update() operation makes no sense here because the # replace behavior results in the some of original untouched counts # being mixed-in with all of the other counts for a mismash that # doesn't have a straight-forward interpretation in most counting # contexts. Instead, we implement straight-addition. Both the inputs # and outputs are allowed to contain zero and negative counts. if not args: raise TypeError("descriptor 'update' 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)) iterable = args[0] if args else None if iterable is not None: if isinstance(iterable, Mapping): if self: self_get = self.get for elem, count in iterable.items(): self[elem] = count + self_get(elem, 0) else: super(Counter, self).update(iterable) # fast path when counter is empty else: _count_elements(self, iterable) if kwds: self.update(kwds)
Example #27
Source File: test_zeromq_client.py From jsonrpcclient with MIT License | 5 votes |
def setup_method(self): # Patch Request.id_generator to ensure the request id is always 1 Request.id_generator = itertools.count(1)
Example #28
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def __missing__(self, key): 'The count of elements not in the Counter is zero.' # Needed so that self[missing_item] does not raise KeyError return 0
Example #29
Source File: method_of_simulated_moments.py From respy with MIT License | 5 votes |
def _create_tidy_data(data, moment_set_labels): """Create tidy data from list of pandas.DataFrames.""" counter = itertools.count() tidy_data = [] for series_or_df, label in zip(data, moment_set_labels): # Join index levels for MultiIndex objects. if isinstance(series_or_df.index, pd.MultiIndex): series_or_df = series_or_df.rename(index=str) series_or_df.index = series_or_df.index.to_flat_index().str.join("_") # If moments are a pandas.Series, convert into pandas.DataFrame. if isinstance(series_or_df, pd.Series): # Unnamed pandas.Series receive a name based on a counter. if series_or_df.name is None: series_or_df = series_or_df.to_frame(name=next(counter)) else: series_or_df = series_or_df.to_frame() # Create pandas.DataFrame in tidy format. tidy_df = series_or_df.unstack() tidy_df.index.names = ("moment_column", "moment_index") tidy_df.rename("value", inplace=True) tidy_df = tidy_df.reset_index() tidy_df["moment_set"] = label tidy_data.append(tidy_df) return pd.concat(tidy_data, ignore_index=True)
Example #30
Source File: sequentialfile.py From baseband with GNU General Public License v3.0 | 5 votes |
def memmap(self, dtype=np.uint8, mode=None, offset=None, shape=None, order='C'): """Map part of the file in memory. Note that the map cannnot span multiple underlying files. Parameters are as for `~numpy.memmap`. """ if self.closed: raise ValueError('memmap of closed file.') dtype = np.dtype(dtype) if mode is None: mode = self.mode.replace('b', '') if offset is not None and offset != self.tell(): # seek will fail for SequentialFileWriter, so we try to avoid it. self.seek(offset) elif self.fh.tell() == self._file_sizes[self.file_nr]: self._open(self.file_nr + 1) if shape is None: count = self.size - self.tell() if count % dtype.itemsize: raise ValueError("size of available data is not a " "multiple of the data-type size.") shape = (count // dtype.itemsize,) else: if not isinstance(shape, tuple): shape = (shape,) count = dtype.itemsize for k in shape: count *= k if self.fh.tell() + count > self._file_sizes[self.file_nr]: raise ValueError('mmap length exceeds individual file size') file_offset = self.fh.tell() mm = np.memmap(self.fh, dtype, mode, file_offset, shape, order) self.fh.seek(file_offset + count) return mm