Python collections.Iterator() Examples
The following are 30
code examples of collections.Iterator().
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
collections
, or try the search function
.
Example #1
Source File: test_pathlib.py From Fluid-Designer with GNU General Public License v3.0 | 7 votes |
def test_rglob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.rglob("fileA") self.assertIsInstance(it, collections.Iterator) # XXX cannot test because of symlink loops in the test setup #_check(it, ["fileA"]) #_check(p.rglob("fileB"), ["dirB/fileB"]) #_check(p.rglob("*/fileA"), [""]) #_check(p.rglob("*/fileB"), ["dirB/fileB"]) #_check(p.rglob("file*"), ["fileA", "dirB/fileB"]) # No symlink loops here p = P(BASE, "dirC") _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"])
Example #2
Source File: test_pathlib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_glob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.glob("fileA") self.assertIsInstance(it, collections.Iterator) _check(it, ["fileA"]) _check(p.glob("fileB"), []) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) if symlink_skip_reason: _check(p.glob("*A"), ['dirA', 'fileA']) else: _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) if symlink_skip_reason: _check(p.glob("*B/*"), ['dirB/fileB']) else: _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', 'linkB/fileB', 'linkB/linkD']) if symlink_skip_reason: _check(p.glob("*/fileB"), ['dirB/fileB']) else: _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
Example #3
Source File: test_pathlib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_rglob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.rglob("fileA") self.assertIsInstance(it, collections.Iterator) _check(it, ["fileA"]) _check(p.rglob("fileB"), ["dirB/fileB"]) _check(p.rglob("*/fileA"), []) if symlink_skip_reason: _check(p.rglob("*/fileB"), ["dirB/fileB"]) else: _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB", "linkB/fileB", "dirA/linkC/fileB"]) _check(p.rglob("file*"), ["fileA", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD"]) p = P(BASE, "dirC") _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"])
Example #4
Source File: test_pathlib.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_rglob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.rglob("fileA") self.assertIsInstance(it, collections.Iterator) _check(it, ["fileA"]) _check(p.rglob("fileB"), ["dirB/fileB"]) _check(p.rglob("*/fileA"), []) if symlink_skip_reason: _check(p.rglob("*/fileB"), ["dirB/fileB"]) else: _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB", "linkB/fileB", "dirA/linkC/fileB"]) _check(p.rglob("file*"), ["fileA", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD"]) p = P(BASE, "dirC") _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"])
Example #5
Source File: test_pathlib.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_glob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.glob("fileA") self.assertIsInstance(it, collections.Iterator) _check(it, ["fileA"]) _check(p.glob("fileB"), []) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) if symlink_skip_reason: _check(p.glob("*A"), ['dirA', 'fileA']) else: _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) if symlink_skip_reason: _check(p.glob("*B/*"), ['dirB/fileB']) else: _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', 'linkB/fileB', 'linkB/linkD']) if symlink_skip_reason: _check(p.glob("*/fileB"), ['dirB/fileB']) else: _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
Example #6
Source File: mparray.py From mpnum with BSD 3-Clause "New" or "Revised" License | 6 votes |
def axis_iter(self, axes=0): """Returns an iterator yielding Sub-MPArrays of ``self`` by iterating over the specified physical axes. **Example:** If ``self`` represents a bipartite (i.e. length 2) array with 2 physical dimensions on each site ``A[(k,l), (m,n)]``, ``self.axis_iter(0)`` is equivalent to:: (A[(k, :), (m, :)] for m in range(...) for k in range(...)) :param axes: Iterable or int specifiying the physical axes to iterate over (default 0 for each site) :returns: Iterator over :class:`.MPArray` """ if not isinstance(axes, collections.Iterable): axes = it.repeat(axes, len(self)) ltens_iter = it.product(*(iter(np.rollaxis(lten, i + 1)) for i, lten in zip(axes, self.lt))) return (MPArray(ltens) for ltens in ltens_iter) ########################## # Algebraic operations # ##########################
Example #7
Source File: test_pathlib.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_glob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) P = self.cls p = P(BASE) it = p.glob("fileA") self.assertIsInstance(it, collections.Iterator) _check(it, ["fileA"]) _check(p.glob("fileB"), []) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) if symlink_skip_reason: _check(p.glob("*A"), ['dirA', 'fileA']) else: _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) if symlink_skip_reason: _check(p.glob("*B/*"), ['dirB/fileB']) else: _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', 'linkB/fileB', 'linkB/linkD']) if symlink_skip_reason: _check(p.glob("*/fileB"), ['dirB/fileB']) else: _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
Example #8
Source File: multipart.py From pykit with MIT License | 6 votes |
def _standardize_value(self, value): reader, fsize, fname = (value + [None, None])[:3] if isinstance(reader, file): reader = self._make_file_reader(reader) elif isinstance(reader, str): reader = self._make_str_reader(reader) fsize = len(value[0]) elif isinstance(reader, Iterator): pass else: raise InvalidArgumentTypeError('type of value[0] {x}' 'is invalid'.format(x=type(value[0]))) return reader, fsize, fname
Example #9
Source File: Iterator.py From PyDesignPattern with GNU General Public License v3.0 | 6 votes |
def testIsIterator(): print("是否为Iterable对象:") print(isinstance([], Iterable)) print(isinstance({}, Iterable)) print(isinstance((1, 2, 3), Iterable)) print(isinstance(set([1, 2, 3]), Iterable)) print(isinstance("string", Iterable)) print(isinstance(gen, Iterable)) print(isinstance(fibonacci(10), Iterable)) print("是否为Iterator对象:") print(isinstance([], Iterator)) print(isinstance({}, Iterator)) print(isinstance((1, 2, 3), Iterator)) print(isinstance(set([1, 2, 3]), Iterator)) print(isinstance("string", Iterator)) print(isinstance(gen, Iterator)) print(isinstance(fibonacci(10), Iterator))
Example #10
Source File: mparray.py From mpnum with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _ltens_to_array(ltens): """Computes the full array representation from an iterator yielding the local tensors. Note that it does not get rid of virtual legs. :param ltens: Iterator over local tensors :returns: numpy.ndarray representing the contracted MPA """ ltens = ltens if isinstance(ltens, collections.Iterator) else iter(ltens) res = first = next(ltens) for tens in ltens: res = matdot(res, tens) if res is first: # Always return a writable array, even if len(ltens) == 1. res = res.copy() return res ################################################ # Helper methods for variational compression # ################################################
Example #11
Source File: mparray.py From mpnum with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _extract_factors(tens, ndims): """Extract iteratively the leftmost MPO tensor with given number of legs by a qr-decomposition :param np.ndarray tens: Full tensor to be factorized :param ndims: Number of physical legs per site or iterator over number of physical legs :returns: List of local tensors with given number of legs yielding a factorization of tens """ current = next(ndims) if isinstance(ndims, collections.Iterator) else ndims if tens.ndim == current + 2: return [tens] elif tens.ndim < current + 2: raise AssertionError("Number of remaining legs insufficient.") else: unitary, rest = qr(tens.reshape((np.prod(tens.shape[:current + 1]), -1))) unitary = unitary.reshape(tens.shape[:current + 1] + rest.shape[:1]) rest = rest.reshape(rest.shape[:1] + tens.shape[current + 1:]) return [unitary] + _extract_factors(rest, ndims)
Example #12
Source File: __init__.py From CogAlg with MIT License | 5 votes |
def safe_first_element(obj): if isinstance(obj, collections.abc.Iterator): # needed to accept `array.flat` as input. # np.flatiter reports as an instance of collections.Iterator # but can still be indexed via []. # This has the side effect of re-setting the iterator, but # that is acceptable. try: return obj[0] except TypeError: pass raise RuntimeError("matplotlib does not support generators " "as input") return next(iter(obj))
Example #13
Source File: test_collections.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 #14
Source File: test_collections.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_Iterator(self): non_samples = [None, 42, 3.14, 1j, "".encode('ascii'), "", (), [], {}, set()] for x in non_samples: self.assertNotIsInstance(x, Iterator) self.assertFalse(issubclass(type(x), Iterator), repr(type(x))) samples = [iter(str()), iter(tuple()), iter(list()), iter(dict()), iter(set()), iter(frozenset()), iter(dict().keys()), iter(dict().items()), iter(dict().values()), (lambda: (yield))(), (x for x in []), ] for x in samples: self.assertIsInstance(x, Iterator) self.assertTrue(issubclass(type(x), Iterator), repr(type(x))) self.validate_abstract_methods(Iterator, 'next', '__iter__') # Issue 10565 class NextOnly: def __next__(self): yield 1 raise StopIteration self.assertNotIsInstance(NextOnly(), Iterator) class NextOnlyNew(object): def __next__(self): yield 1 raise StopIteration self.assertNotIsInstance(NextOnlyNew(), Iterator)
Example #15
Source File: __init__.py From coffeegrindsize with MIT License | 5 votes |
def safe_first_element(obj): if isinstance(obj, collections.abc.Iterator): # needed to accept `array.flat` as input. # np.flatiter reports as an instance of collections.Iterator # but can still be indexed via []. # This has the side effect of re-setting the iterator, but # that is acceptable. try: return obj[0] except TypeError: pass raise RuntimeError("matplotlib does not support generators " "as input") return next(iter(obj))
Example #16
Source File: builtins.py From mochi with MIT License | 5 votes |
def cons(head, tail): if isinstance(tail, Iterator): return chain((head,), tail) if isinstance(tail, cell): return cell(head, tail) if tail is None: return cell(head, None) return (head,) + tail
Example #17
Source File: test_futures.py From mentor with Apache License 2.0 | 5 votes |
def test_map(resources): with MesosPoolExecutor(name='futures-pool') as executor: it = executor.map(operator.add, range(10), range(10), resources=resources) assert isinstance(it, Iterator) for i, v in enumerate(it): assert i + i == v
Example #18
Source File: feature_column.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _clean_feature_columns(feature_columns): """Verifies and normalizes `feature_columns` input.""" if isinstance(feature_columns, _FeatureColumn): feature_columns = [feature_columns] if isinstance(feature_columns, collections.Iterator): feature_columns = list(feature_columns) if isinstance(feature_columns, dict): raise ValueError('Expected feature_columns to be iterable, found dict.') for column in feature_columns: if not isinstance(column, _FeatureColumn): raise ValueError('Items of feature_columns must be a _FeatureColumn. ' 'Given (type {}): {}.'.format(type(column), column)) if not feature_columns: raise ValueError('feature_columns must not be empty.') name_to_column = dict() for column in feature_columns: if column.name in name_to_column: raise ValueError('Duplicate feature column name found for columns: {} ' 'and {}. This usually means that these columns refer to ' 'same base feature. Either one must be discarded or a ' 'duplicated but renamed item must be inserted in ' 'features dict.'.format(column, name_to_column[column.name])) name_to_column[column.name] = column return feature_columns
Example #19
Source File: test_collections.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
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 #20
Source File: __init__.py From twitter-stock-recommendation with MIT License | 5 votes |
def safe_first_element(obj): if isinstance(obj, collections.Iterator): # needed to accept `array.flat` as input. # np.flatiter reports as an instance of collections.Iterator # but can still be indexed via []. # This has the side effect of re-setting the iterator, but # that is acceptable. try: return obj[0] except TypeError: pass raise RuntimeError("matplotlib does not support generators " "as input") return next(iter(obj))
Example #21
Source File: itercompat.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def is_iterator(x): """An implementation independent way of checking for iterators Python 2.6 has a different implementation of collections.Iterator which accepts anything with a `next` method. 2.7+ requires and `__iter__` method as well. """ if sys.version_info >= (2, 7): return isinstance(x, collections.Iterator) return isinstance(x, collections.Iterator) and hasattr(x, '__iter__')
Example #22
Source File: test_collections.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_Iterator(self): non_samples = [None, 42, 3.14, 1j, "".encode('ascii'), "", (), [], {}, set()] for x in non_samples: self.assertNotIsInstance(x, Iterator) self.assertFalse(issubclass(type(x), Iterator), repr(type(x))) samples = [iter(str()), iter(tuple()), iter(list()), iter(dict()), iter(set()), iter(frozenset()), iter(dict().keys()), iter(dict().items()), iter(dict().values()), (lambda: (yield))(), (x for x in []), ] for x in samples: self.assertIsInstance(x, Iterator) self.assertTrue(issubclass(type(x), Iterator), repr(type(x))) self.validate_abstract_methods(Iterator, 'next', '__iter__') # Issue 10565 class NextOnly: def __next__(self): yield 1 raise StopIteration self.assertNotIsInstance(NextOnly(), Iterator) class NextOnlyNew(object): def __next__(self): yield 1 raise StopIteration self.assertNotIsInstance(NextOnlyNew(), Iterator)
Example #23
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def _item_to_table(iterator, resource): """Convert a JSON table to the native object. Args: iterator (google.api_core.page_iterator.Iterator): The iterator that is currently in use. resource (Dict): An item to be converted to a table. Returns: google.cloud.bigquery.table.Table: The next table in the page. """ return TableListItem(resource)
Example #24
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def _item_to_routine(iterator, resource): """Convert a JSON model to the native object. Args: iterator (google.api_core.page_iterator.Iterator): The iterator that is currently in use. resource (Dict): An item to be converted to a routine. Returns: google.cloud.bigquery.routine.Routine: The next routine in the page. """ return Routine.from_api_repr(resource)
Example #25
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def _item_to_job(iterator, resource): """Convert a JSON job to the native object. Args: iterator (google.api_core.page_iterator.Iterator): The iterator that is currently in use. resource (Dict): An item to be converted to a job. Returns: job instance: The next job in the page. """ return iterator.client.job_from_resource(resource)
Example #26
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def _item_to_dataset(iterator, resource): """Convert a JSON dataset to the native object. Args: iterator (google.api_core.page_iterator.Iterator): The iterator that is currently in use. resource (Dict): An item to be converted to a dataset. Returns: google.cloud.bigquery.dataset.DatasetListItem: The next dataset in the page. """ return DatasetListItem(resource)
Example #27
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def _item_to_project(iterator, resource): """Convert a JSON project to the native object. Args: iterator (google.api_core.page_iterator.Iterator): The iterator that is currently in use. resource (Dict): An item to be converted to a project. Returns: google.cloud.bigquery.client.Project: The next project in the page. """ return Project.from_api_repr(resource) # pylint: enable=unused-argument
Example #28
Source File: client.py From python-bigquery with Apache License 2.0 | 5 votes |
def list_projects( self, max_results=None, page_token=None, retry=DEFAULT_RETRY, timeout=None ): """List projects for the project associated with this client. See https://cloud.google.com/bigquery/docs/reference/rest/v2/projects/list Args: max_results (Optional[int]): Maximum number of projects to return, If not passed, defaults to a value set by the API. page_token (Optional[str]): Token representing a cursor into the projects. If not passed, the API will return the first page of projects. The token marks the beginning of the iterator to be returned and the value of the ``page_token`` can be accessed at ``next_page_token`` of the :class:`~google.api_core.page_iterator.HTTPIterator`. retry (Optional[google.api_core.retry.Retry]): How to retry the RPC. timeout (Optional[float]): The number of seconds to wait for the underlying HTTP transport before using ``retry``. Returns: google.api_core.page_iterator.Iterator: Iterator of :class:`~google.cloud.bigquery.client.Project` accessible to the current client. """ return page_iterator.HTTPIterator( client=self, api_request=functools.partial(self._call_api, retry, timeout=timeout), path="/projects", item_to_value=_item_to_project, items_key="projects", page_token=page_token, max_results=max_results, )
Example #29
Source File: ec2.py From toil with Apache License 2.0 | 5 votes |
def wait_instances_running(ec2, instances): """ Wait until no instance in the given iterable is 'pending'. Yield every instance that entered the running state as soon as it does. :param boto.ec2.connection.EC2Connection ec2: the EC2 connection to use for making requests :param Iterator[Instance] instances: the instances to wait on :rtype: Iterator[Instance] """ running_ids = set() other_ids = set() while True: pending_ids = set() for i in instances: if i.state == 'pending': pending_ids.add(i.id) elif i.state == 'running': assert i.id not in running_ids running_ids.add(i.id) yield i else: assert i.id not in other_ids other_ids.add(i.id) yield i log.info('%i instance(s) pending, %i running, %i other.', *map(len, (pending_ids, running_ids, other_ids))) if not pending_ids: break seconds = max(a_short_time, min(len(pending_ids), 10 * a_short_time)) log.info('Sleeping for %is', seconds) time.sleep(seconds) for attempt in retry_ec2(): with attempt: instances = ec2.get_only_instances(list(pending_ids))
Example #30
Source File: __init__.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
def _get_choices(self): if isinstance(self._choices, collections.Iterator): choices, self._choices = tee(self._choices) return choices else: return self._choices