iterable

Chain dynamic iterable of context managers to a single with statement

柔情痞子 提交于 2019-12-04 04:04:24
问题 I have a bunch of context managers that I want to chain. On the first glance, contextlib.nested looked like a fitting solution. However, this method is flagged as deprecated in the documentation which also states that the latest with statement allows this directly: Deprecated since version 2.7: The with-statement now supports this functionality directly (without the confusing error prone quirks). However I could not get Python 3.4.3 to use a dynamic iterable of context managers: class Foo():

Lazily transpose a list in Python

浪子不回头ぞ 提交于 2019-12-04 02:34:25
So, I have an iterable of 3-tuples, generated lazily. I'm trying to figure out how to turn this into 3 iterables, consisting of the first, second, and third elements of the tuples, respectively. However, I wish this to be done lazily. So, for example, I wish [(1, 2, 3), (4, 5, 6), (7, 8, 9)] to be turned into [1, 4, 7] , [2, 5, 8] , [3, 6, 9] . (Except I want iterables not lists.) The standard zip(*data) idiom doesn't work, because the argument unpacking expands the entire iterable. (You can verify this by noting that zip(*((x, x+1, x+2) for x in itertools.count(step=3))) hangs.) The best I've

Does Rust have an equivalent to Python's list comprehension syntax?

帅比萌擦擦* 提交于 2019-12-03 23:33:42
问题 Python list comprehension is really simple: >>> l = [x for x in range(1, 10) if x % 2 == 0] >>> [2, 4, 6, 8] Does Rust have an equivalent syntax like: let vector = vec![x for x in (1..10) if x % 2 == 0] // [2, 4, 6, 8] 回答1: You can just use iterators: fn main() { let v1 = (0u32..9).filter(|x| x % 2 == 0).map(|x| x.pow(2)).collect::<Vec<_>>(); let v2 = (1..10).filter(|x| x % 2 == 0).collect::<Vec<u32>>(); println!("{:?}", v1); // [0, 4, 16, 36, 64] println!("{:?}", v2); // [2, 4, 6, 8] } 回答2:

Why is there a method iterator() on java.util.Collection

拥有回忆 提交于 2019-12-03 23:29:44
Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined. I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level. Any other ideas? Backwards compatibility. Iterable was not introducted until 1.5 with the for(Object o : iterable) construct. Previously, all collections had to provide a means to iterate them. I suspect it was just to avoid the appearance of removing a method from a documentation point of view. Although

Why is the iterator method present in both Iterable and Collection interfaces? [duplicate]

浪尽此生 提交于 2019-12-03 16:44:14
This question already has an answer here : Closed last year . Method iterator() declared in java.util.Collection and in java.lang.Iterable, its superinterface? (1 answer) The Iterable interface has the method below: Iterator<T> iterator(); The Collection interface extends Iterable , and it also declares the same method. I am doubtful what was the need of putting the same method declaration twice while designing Java collections? The main reason is Java 5. java.util.Collection with its iterator() method and java.util.Iterator exist since Java 1.2. When they wanted to introduce the enhanced for

Scala: Exposing a JDBC ResultSet through a generator (iterable)

最后都变了- 提交于 2019-12-03 16:18:32
问题 I've got a set of rows in a database, and I'd like to provide an interface to spin through them like this: def findAll: Iterable[MyObject] Where we don't require having all the instances in memory at once. In C# you can easily create generators like this using yield, the compiler takes care of converting code that loops through the recordset into an iterator (sort of inverting it). My current code looks like this: def findAll: List[MyObject] = { val rs = getRs val values = new ListBuffer

Emulating membership-test in Python: delegating __contains__ to contained-object correctly

夙愿已清 提交于 2019-12-03 14:54:34
I am used to that Python allows some neat tricks to delegate functionality to other objects. One example is delegation to contained objects. But it seams, that I don't have luck, when I want to delegate __contains __: class A(object): def __init__(self): self.mydict = {} self.__contains__ = self.mydict.__contains__ a = A() 1 in a I get: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument of type 'A' is not iterable What I am making wrong? When I call a.__contains __(1), everything goes smooth. I even tried to define an __iter __ method in A to make A more

iter() not working with datetime.now()

对着背影说爱祢 提交于 2019-12-03 11:27:16
问题 A simple snippet in Python 3.6.1: import datetime j = iter(datetime.datetime.now, None) next(j) returns: Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration instead of printing out the classic now() behavior with each next() . I've seen similar code working in Python 3.3, am I missing something or has something changed in version 3.6.1? 回答1: This is definitely a bug introduced in Python 3.6.0b1. The iter() implementation recently switched to using _PyObject

How to check if an object is iterable in Python? [duplicate]

会有一股神秘感。 提交于 2019-12-03 06:50:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: In Python, how do I determine if an object is iterable? How does one check if a Python object supports iteration, a.k.a an iterable object (see definition Ideally I would like function similar to isiterable(p_object) returning True or False (modelled after isinstance(p_object, type) ). 回答1: You can check for this using isinstance and collections.Iterable >>> from collections import Iterable >>> l = [1, 2, 3, 4]

Is there any official contract for the Iterable interface with respect to multiple usage?

天涯浪子 提交于 2019-12-03 06:12:40
Since Java 5, we have the new java.lang.Iterable type that can be used in foreach loops as such: for (Object element : iterable); The Iterable contract does not specify whether its iterator() method can be called more than once before disposing of the Iterable. I.e., it is not clear whether the following can be expected to work for all Iterables : for (Object element : iterable); for (Object element : iterable); For instance, an Iterator wrapping implementation cannot be used twice: public class OneShotIterable<T> implements Iterable<T> { private final Iterator<T> it; public OneShotIterable