generator

Why it seems contradictory when yield meet recursion in python?

Why it seems contradictory when yield meet recursion in python? Question: I tried to implement full permutation via python generator & recursion, but the output from ‘print’ is different from any other form of generator utility. def gen_perms(seq): seq_len = len(seq) def swap(i,j): x = seq[i] seq[i] = seq[j] seq[j] = x def perms(n): if …

Total answers: 1

Python: await the generator end

Python: await the generator end Question: Current versions of Python (Dec 2022) still allow using @coroutine decorator and a generation can be as: import asyncio asyncify = asyncio.coroutine data_ready = False # Status of a pipe, just to test def gen(): global data_ready while not data_ready: print("not ready") data_ready = True # Just to test …

Total answers: 1

yield from vs yield in for-loop

yield from vs yield in for-loop Question: My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def __init__(self, gen): self.gen = gen def __iter__(self): for el in self.gen: yield el and Class2 …

Total answers: 2

Generator with For In only returning first character instead of the whole string

Generator with For In only returning first character instead of the whole string Question: I want this generator to return these characters in a loop: "><><><" instead it’s only returning the first character ">>>>>>" if I replace yield with print it works correctly, why is it only returning the first character? def gn(): while True: …

Total answers: 1

how to connect three dataloaders together in pytorch – parallel not chained

how to connect three dataloaders together in pytorch – parallel not chained Question: i have three same length dataloaders (A,B,C) that load images (a,b,c), i want to create a new dataloader D that loads a dict of images, some syntax for clarity: usually the dataloader works like this: for a in A: a -> an …

Total answers: 1

Using the same iterator in nested for loops

Using the same iterator in nested for loops Question: Consider the following code: from itertools import chain lst = [‘a’, 1, 2, 3, ‘b’, 4, 5, ‘c’, 6] def nestedForLoops(): it = iter(lst) for item0 in it: if isinstance(item0, str): print(item0) else: # this shouldn’t happen because of # 1. lst[0] is a str, and …

Total answers: 2

Cutting an array into consistent pieces of any size, with recursion

Cutting an array into consistent pieces of any size, with recursion Question: The problem is to, given an array, write a generator function that will yield all combinations of cutting the array into consistent pieces(arrays of elements that are consecutive in the given array) of any size and which together make up the whole given …

Total answers: 1

Print the nth step of a Generator in an easy way

Print the nth step of a Generator in an easy way Question: I want to know if there is a better and cleaner way of printing the 3rd step of a generator function. Currently I have written the following code def imparesgen(): n = 0 while n<200: n=n+2 yield n gen = imparesgen() y = …

Total answers: 4

Using yield to run code after method of function execution

Using yield to run code after method of function execution Question: I’m trying to create a class method that can run some code after its execution. In pytest we have this functionality with fixtures: @pytest.fixture def db_connection(conn_str: str): connection = psycopg2.connect(conn_str) yield connection connection.close() # this code will be executed after the test is done …

Total answers: 2

Python generator yield behaviour

Python generator yield behaviour Question: So I have the following generator function: def gen(n=5): for i in range(n): n = yield n for i in gen(3): print(i) The result: 3 None None I understand the first result of yield is 3. Because I assigned 3 to function argument n. But where are the None in …

Total answers: 2