generator

what does yield without value do in context manager

不问归期 提交于 2020-01-01 04:13:27
问题 import contextlib import time @contextlib.contextmanager def time_print(task_name): t = time.time() try: yield finally: print task_name, "took", time.time() - t, "seconds." def doproc(): x=1+1 with time_print("processes"): [doproc() for _ in range(500)] # processes took 15.236166954 seconds. when does doproc get executed when using this decorator? 回答1: yield expression returns control to the whatever is using the generator. The generator pauses at this point, which means that the

Python-style generators in Go

好久不见. 提交于 2019-12-31 10:42:45
问题 I'm currently working through the Tour of Go, and I thought that goroutines have been used similarly to Python generators, particularly with Question 66. I thought 66 looked complex, so I rewrote it to this: package main import "fmt" func fibonacci(c chan int) { x, y := 1, 1 for { c <- x x, y = y, x + y } } func main() { c := make(chan int) go fibonacci(c) for i := 0; i < 10; i++ { fmt.Println(<-c) } } This seems to work. A couple of questions: If I turn up the buffer size on the channel to

Can I yield from an inner function?

▼魔方 西西 提交于 2019-12-31 08:58:52
问题 With ES6 generators, I see code like this: var trivialGenerator = function *(array) { var i,item; for(var i=0; i < array.length; i++){ item = array[i]; yield item; }; }; Is it possible to write something more like the code below instead? var trivialGenerator = function *(array) { array.forEach(function *(item){ yield item; }); }; I'm asking because the classic for loop is an abomination. 回答1: No, you can't use yield inside of the inner function. But in your case you don't need it. You can

Class Map Generator for Fluent NHibernate

三世轮回 提交于 2019-12-31 08:44:19
问题 Is there a Class Map generator for Fluent NHibernate? I need something like db2hbm but I want it to generate Fluent Class Maps instead of xml mappings. I am aware of AutoMapping for Fluent but that is not what I want. I want to be able to generate Class Map files from tables in database and push them to my src repository. 回答1: You can do this with NHibernate Mapping Generator. 回答2: You can do this with Visual NHibernate. Check the Fluent Nhibernate option on the Options screen to create FNH

Delete duplicate tuples independent of order with same elements in generator Python 3.5

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 07:43:47
问题 I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. 回答1: You can normalize the data by sorting it, then add it to a set to

Delete duplicate tuples independent of order with same elements in generator Python 3.5

心已入冬 提交于 2019-12-31 07:41:28
问题 I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. 回答1: You can normalize the data by sorting it, then add it to a set to

Reading infinite stream - tail

北战南征 提交于 2019-12-31 03:12:34
问题 Problem: Program to read the lines from infinite stream starting from its end of file. #Solution: import time def tail(theFile): theFile.seek(0,2) # Go to the end of the file while True: line = theFile.readline() if not line: time.sleep(10) # Sleep briefly for 10sec continue yield line if __name__ == '__main__': fd = open('./file', 'r+') for line in tail(fd): print(line) readline() is a non-blocking read, with if check for every line. Question: It does not make sense for my program running to

Drain or discard a generator without looping?

烈酒焚心 提交于 2019-12-31 01:52:10
问题 In an exception handler for a CSP style process, I need to read and discard the entire contents of a channel in order to allow other processes that are blocking to send to it to complete. The interface presents a generator for receiving, is there a faster way to consume and discard the entire contents of a generator than the following? for _ in chan: pass 回答1: There is a way that is slightly faster: collections.deque(chan, maxlen=0) Your code makes the intention much clearer, though, so you

using filter and generator to generator endless prime number in python

我们两清 提交于 2019-12-30 07:41:44
问题 Below is a python program I found to find prime numbers using Sieve of Eratosthenes. It uses filter and generator. I'm not able to understand it. def _odd_iter(): n = 1 while True: n = n + 2 yield n def _not_divisible(n): return lambda x: x % n > 0 def primes(): yield 2 it = _odd_iter() while True: n = next(it) yield n it = filter(_not_divisible(n), it) for n in primes(): if n < 1000: print(n) else: break What I don't understand is it = filter(_not_divisible(n), it) . For example for number

Recursive Generators in JavaScript

老子叫甜甜 提交于 2019-12-30 05:37:19
问题 I am trying to write a recursive generator for an in order traversal. class Tree { *inOrderTraversal() { function* helper(node) { if (node.left !== null) { // this line is executed, but helper is not being called helper(node.left); } yield node.value; if (node.right !== null) { helper(node.right); } } for (let i of helper(this.root)) { yield i; } } // other methods omitted } And I am calling the generator like so: const tree = new Tree(); tree.add(2); tree.add(1); tree.add(3); for (let i of