yield

python generator yield statement not yield

跟風遠走 提交于 2021-02-19 01:49:20
问题 Here is code I am running: def infinite_Third() -> Generator: num = 1 while True: if num % 3 ==0: i = (yield num) if i is not None: num = i num += 1 if __name__=='__main__': third_gen = infinite_Third() for i in third_gen: print(f"it is {i}") if i>1000: break third_gen.send(10*i+1) I am expecting to see results as: it is 3 it is 33 it is 333 it is 3333 However, what I really get is: it is 3 it is 36 it is 366 it is 3666 I think this might be related to using send in the main code, but couldn

Different results from yield vs return

半城伤御伤魂 提交于 2021-02-07 15:27:47
问题 I don't really understand how yield statement works in this situation. The problem says that given an expression without parentheses, write a function to generate all possible fully parenthesized (FP) expressions. Say, the input is '1+2+3+4' which should be generated to 5 FP expressions: (1+(2+(3+4))) (1+((2+3)+4)) ((1+2)+(3+4)) ((1+(2+3))+4) (((1+2)+3)+4) My code is as follows. OPS = ('+', '-', '*', '/') def f(expr): """ Generates FP exprs Recursive formula: f(expr1[op]expr2) = (f(expr1) [op

TypeError: 'generator' object is not callable

天大地大妈咪最大 提交于 2021-02-07 11:23:21
问题 I have a generator defined like this: def lengths(x): for k, v in x.items(): yield v['time_length'] And it works, calling it with for i in lengths(x): print i produces: 3600 1200 3600 300 which are the correct numbers. However, when I call it like so: somefun(lengths(x)) where somefun() is defined as: def somefun(lengths): for length in lengths(): # <--- ERROR HERE if not is_blahblah(length): return False I get this error message: TypeError: 'generator' object is not callable What am I

Yielding partitions of a multiset with Ruby

社会主义新天地 提交于 2021-02-05 07:16:16
问题 I would like to get all the possible partitions (disjoint subsets of a set which union is the original set) of a multiset (some elements are equal and non-distinguishable from each other). Simpler case when one would like to yield the partitions of a simple set, in which there are no elements with multiplicity, in other words all elements are different. For this scenario I found this Ruby code on StackOwerflow which is very efficient, as not storing all the possible partitions, but yielding

Making python generator via c++20 coroutines

…衆ロ難τιáo~ 提交于 2021-02-04 13:00:07
问题 Let's say I have this python code: def double_inputs(): while True: x = yield yield x * 2 gen = double_inputs() next(gen) print(gen.send(1)) It prints "2", just as expected. I can make a generator in c++20 like that: #include <coroutine> template <class T> struct generator { struct promise_type; using coro_handle = std::coroutine_handle<promise_type>; struct promise_type { T current_value; auto get_return_object() { return generator{coro_handle::from_promise(*this)}; } auto initial_suspend()

How to wrap or embed generators?

狂风中的少年 提交于 2020-12-26 07:42:08
问题 I'm trying to provide a unified interface for retrieving all files from a single directory or a list of directories. def get_files(dir_or_dirs): def helper(indir): file_list = glob.glob("*.txt") for file in file_list: yield file if type(dir_or_dirs) is list: # a list of source dirs for dir in dir_or_dirs: yield helper(dir) else: # a single source dir yield helper(dir_or_dirs) def print_all_files(file_iter): for file in file_iter: print(file) # error here! Questions: The error says 'file' is

sleep与yield异同

≯℡__Kan透↙ 提交于 2020-12-13 10:46:08
sleep:是Thread类的一个静态方法,该方法会让当前正在 执行的线程暂停执行,从而将执行机会让给其他线程执行。sleep(long mills)参数指定当前线程暂停执行的时间,经过这段阻塞时间后,该线程会进入就绪状态,等候线程调度器的调度。sleep方法声明抛出了InterruptedException异常,所以调用sleep方法时要么在方法开始处抛出异常要么使用try{}..catch{}块进行捕获。 yield方法只会给优先级相同或更高优先级的线程执行机会。yield不会将线程转入阻塞状态,只是强制当前线程进入就绪状态。因此完全有可能某个线程调用yield方法暂停后,立即又获得处理器资源被执行。yield方法没有声明抛出任何异常。 sleep比yield方法有更好的可移植性, 通常,不要依靠yield控制并发线程的执行。 code: sleep public class SleepDemo extends Thread { public SleepDemo(String name) { super(name); } public void run() { for(int i=0;i<50;i++) { System.out.println(getName()+"----"+i); if(i==20) { try { Thread.sleep(1000*10); }

Is it safe to combine 'with' and 'yield' in python?

倖福魔咒の 提交于 2020-11-30 04:32:10
问题 It's a common idiom in python to use context manager to automatically close files: with open('filename') as my_file: # do something with my_file # my_file gets automatically closed after exiting 'with' block Now I want to read contents of several files. Consumer of the data does not know or care if data comes from files or not-files. It does not want to check if the objects it received can be open or not. It just wants to get something to read lines from. So I create an iterator like this:

Is it safe to combine 'with' and 'yield' in python?

只愿长相守 提交于 2020-11-30 04:32:01
问题 It's a common idiom in python to use context manager to automatically close files: with open('filename') as my_file: # do something with my_file # my_file gets automatically closed after exiting 'with' block Now I want to read contents of several files. Consumer of the data does not know or care if data comes from files or not-files. It does not want to check if the objects it received can be open or not. It just wants to get something to read lines from. So I create an iterator like this: