Questions tagged [yield]
yield is (1) a keyword that facilitates creation of generator functions, (2) a Ruby statement to transfer control from one coroutine to another, (3) a Java statement used to yield a value from a switch expression.
1,702
questions
0
votes
0
answers
37
views
Why is yield "blocking" all other functionality?
I decided to finally learn yield to make generators and came across this weird thing:
If I run this code, it prints out <generator ...>. It doesn't print num or stop execution at 3.1...
If I ...
0
votes
0
answers
30
views
tf.data.Dataset.from_generator and "Cannot convert the argument `type_value`: TensorSpec() to a TensorFlow DType."
I am trying to create a dataset using tf.data.Dataset.from_generator and I have a couple of questions.
This is the function working as a generator:
sequence_lengh=60
def sequence_xx(train_dataset):
...
29
votes
2
answers
1k
views
yield from vs yield in for-loop
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 ...
0
votes
0
answers
39
views
Fetching sessions saga refactor to RXJS epic
trying to refactor the saga code into rxjs epic. It's fetching sessions from the server
yield race([
call(fetchSessions, sessionsList), //complete retrieving sessions
call(sessionWatchdog,...
0
votes
0
answers
29
views
TypeError: 'filter' object is not iterable when using lambda function with yield
I'm trying to use a lambda function with yield inside a generator function to filter a sequence of numbers and return only the even ones. However, I'm getting a TypeError when I try to yield from the ...
1
vote
0
answers
40
views
How to get the item name and not its address when requesting with a "condition event" from a Simpy-Store
A Simpy-Store is filled with elements {0}...{9}
car_Fleet = simpy.Store(env, capacity=10)
i = 0
for i in range(10):
car_Fleet.put({i})
i += 1
When requesting an item from that store
...
1
vote
1
answer
25
views
how to connect three dataloaders together in pytorch - parallel not chained
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:
...
0
votes
1
answer
51
views
Can generator function execution continue after an error is thrown in this pattern?
These docs make me think the answer could potentially be yes - but I always thought no and my tests show the same.
The reason for my question is that a generator function seems to have done exactly ...
4
votes
1
answer
124
views
Is there a guarantee that code after yield will be executed?
Let's say we have a fixture that allocated some unmanaged resources and releases them like in the following example:
@pytest.fixture
def resource():
res = driver.Instance()
yield res
res....
0
votes
1
answer
88
views
How to yield a db connection in a python sqlalchemy function similar to how it is done in FastAPI?
In FastAPI I had the following function that I used to open and close a DB session:
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
And within the ...
5
votes
1
answer
159
views
How to test an element from a generator without consuming it
I have a generator gen, with the following properties:
it's quite expensive to make it yield (more expensive than creating the generator)
the elements take up a fair amount of memory
sometimes all of ...
0
votes
0
answers
50
views
How to publish messages over gRPC stream from another thread in Python?
I want to use gRPC to publish a stream of messages, but I also want to abstract away gRPC from the rest of my code. Ideally, I would have a thread in my main program that calls publish(message), and ...
0
votes
0
answers
27
views
How to change a value in generator function
I have a generator function:
def emit_gel() -> Generator[int, int, None]:
number: int = 50
step: int = 10
while True:
yield number
number = number + random.randint(0, ...
1
vote
2
answers
59
views
Python generator yield behaviour
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 ...
0
votes
1
answer
80
views
yield + generator in python in class
I am very new in Python and I wanna create a generator object that yields two lists for Fibonacci sequence. First list of number and second list of fibonacci.
The function define in the class. Before ...
0
votes
0
answers
58
views
Pytest session scoped fixture yields multiple times - help me understand
Below is an example of a pytest session and function scoped fixtures.
The first fixture sets up a single db connection, whilst the second one deletes the DB state to have a clean slate for each test ...
0
votes
2
answers
52
views
How to get two synchronised generators from a function
i have a nested tuple like this one :
this_one = (w,h,(1,2,4,8,16),(0,2,3,4,6),("0"),("0"),(0,1))
It will be used to feed:
itertools.product(*this_one)
w and h have to be both ...
0
votes
2
answers
140
views
Invalid yield, SyntaxError in Ruby
I am absolutely NOT a developer, but I am trying to get this abandoned app to work
This is the code in a file called layout.erb which is HTML
<div class="container">
<%= yield %>
...
0
votes
1
answer
41
views
Other ways to get the return values from "yield" in "async" functions in Python?
When using async for statement in async def call_test() as shown below:
import asyncio
async def test():
yield "One"
yield "Two"
yield "Three"
async def ...
0
votes
0
answers
26
views
How to get the value from "return" when using "yield" with it in Python? [duplicate]
I'm trying to get the value from return when using yield with it as shown below:
def test():
yield 'One'
yield 'Two'
yield 'Three'
return 'Four' # Here
for x in test():
print(...
0
votes
0
answers
34
views
How to yield the value from the loop into a specific numpy array
I was trying to use the forloop to yield the new value into the a numpy array, then used the generated arry to make an animation of points' movement. However, there are two bugs keep showing up:
I ...
0
votes
1
answer
23
views
tkinter yielding in a while loop
In the while loop below, I am trying to wait to display the CPUs move and then move on. Normally in this situation I would use time.sleep however since I am using tkinter I cannot do this. I have then ...
1
vote
1
answer
80
views
Asyncio yielding results from multiple futures as they arrive
I currently have a function, which yields results from an external source, which may arrive over a long time period.
async def run_command(data):
async with httpx.AsyncClient() as client:
...
1
vote
0
answers
93
views
How do receive stream from flask server (sent via yield) with axios (react frontend)
I'm sending the stream of data from flask server via yield. I'm able to see this stream if I go directly to api url. However I don't know how to receive it on frontend. I would appreciate the help. ...
0
votes
1
answer
32
views
convert normal function to generator and keeping code structure
Generators are elegant and memory-efficient, so I'd like to convert normal functions producing sequences to generators. But I haven't found an easy conversion method, by easy I mean it is best to be ...
0
votes
1
answer
58
views
Python, append list and yield result to a file, scrapy
New to python, coming from javascript.
I am trying to append to the plates list using a for-loop and then yield the result to a file - but I think that the yield keyword is firing before the appending ...
1
vote
2
answers
41
views
Convert blocks into arrays
I have a text file containing data in form of blocks. Something like:
File description
Used units
Additional info
T[K]
50 75 100
125 150 175
200 225 250
Field_1
0.1 0.2 0.3
0.4 0.5 0.6
0.7 0.8 0.9
...
0
votes
2
answers
235
views
How can i solve the yield and the * for Redux/Saga
how can i solve this situation below?
import { all } from 'redux-saga/effects'
import cart from './cart/sagas'
export default function* rootSaga() {
return yield all([
cart,
])
}
This error keeps ...
0
votes
1
answer
133
views
when I using yield in fixture the yield value return null
I was asked to write a log that shows an error after a test has taken place, for this I used
fixture in conftest.py file.
My code:
pytest.fixture(autouse=True)
def log_during_fails(request):
...
0
votes
0
answers
44
views
calling .next() recursively from a generator
I'm going through Kyle Simpson's Rethinking Async on Frontend Masters, and in the lecture on async generators, there is an example of calling .next() recursively. The gist of it goes as follows:
...
0
votes
2
answers
74
views
use yield inside another yield
I would like to use generator to print the content of an array, however yield inside another yield is confusing to me
let myArray = ["a", "b", "c"]
function* line(array){
yield "my array"
...
-2
votes
1
answer
58
views
C# - After executing the remove statement , list stays the same
Everyone , I tried to remove item based on condition but it seems that after executing the remove statement my list stays the same .
class Menu
public class Menu : Entity
{
/// <summary>
...
0
votes
1
answer
52
views
Why can't I get Typescript yield to work with custom hooks?
So I have this function where "useCustom" is my custom Hook on Typescript which basically replaces a string using a json file.
import { Stringable } from './types';
export declare const ...
0
votes
0
answers
17
views
dataset related to ndvi values and yield prediction?
Hi can anyone know any site or availability of dataset which includes ndvi values , areas,production of yield..I am doing yield prediction on the basis of ndvi values.If anyone have data or resources ...
0
votes
1
answer
21
views
How to check if a yield has been triggered within a method?
In the below method, if a certain condition is met, I'm using yield to return a generator. Within that same method, I'd like to check if the yield has already been triggered (thus the generator ...
0
votes
1
answer
49
views
Why does the iteration stop at the first yield?
I'm trying to walk an AST and yield all the matching nodes. My iteration stops at the first match, instead of going trough all the nodes. What am I missing?
import unittest
test_ast = {
'body': [
...
0
votes
1
answer
58
views
Stream<List<T>> return an empty list after getting data from firestore
I am trying to stream data fetched from Firestore, but it's returned an empty list.
The structure of the database look like this:
collection('users').doc().collection('products').doc()
I had ...
0
votes
1
answer
23
views
Getting "object undefined" when using yield in js
var pi = document.getElementById("pi");
function * calcpi(){
let q = 1;
let r = 0;
let t = 1;
let k = 1;
let n = 3;
let l = 3;
while (true){
if (4*q+r-t < n*t){
...
0
votes
1
answer
60
views
C# How to Serialize/Deserialize an IEnumerator from a Yield function (to Json?)
I'm trying to serialize/deserialize an IEnumerator generated from a function using yield.
I would like to serialize the IEnumerator at any iteration, I don't want to force it to generate all of it ...
0
votes
0
answers
22
views
Python function that give me result based on previous functions (func2 must be in func1 loop). Exist better way than my example to obtain this?
I want to say, it's my beginning with OOP. Can someone tell me, exist better way than my examples to obtain the same? I can't paste here the full code of my program, because it's university project, ...
1
vote
1
answer
67
views
SimPy Simulation of RentalService stops before all yield statements of all processes are fulfilled
Multiple identical Items can be "rented" from a SimPy store element.
The availability of Items is limited to a number lower than the actual demand.
By using yield statements other processes ...
-3
votes
1
answer
37
views
yield keyword Question (run functions tough different files)
I'm not sure if yield is what i need if i want to implement following logic
def function():
items = ["1", "2", "3"]
for item in items:
print(item)
function()
Now ...
0
votes
0
answers
10
views
How can I print all yield output inside of a dunder method in a class?
class Number:
def init(self, no):
self.no = no
def __mul__(self,another):
yield self.no *another.no
yield self.no *another.no +1
a = Number(10)
b = Number(5)
print(a*b)
0
votes
1
answer
177
views
Recursive search using yield, returning list of parents
I need to do a recursive search over a tree and as a result I would like an IEnumerable containing the parents between a specified root and a target node.
Example data:
- ROOT
- A
- B
...
0
votes
0
answers
200
views
Understanding the event channel in redux saga
I'm pretty new to redux saga and I was wondering if someone could confirm the difference between these two implementations of an event channel for a BLE device scan. This first example seems to stop ...
0
votes
3
answers
91
views
how to use a generator in python to yield results from dfs?
I have a dfs function (permutation) in which it takes long time to compute all possible values; I wish to create a generator such that every time I call a function get_value it will provide a possible ...
1
vote
0
answers
28
views
yield and custom __await__ results in builtins.RuntimeError: Task got bad yield
I am writing a class that has its own implemented __await__ with a calling function to retrieve the value. The __await__ is intentionally including a yield and a return but I am continuously thrown a ...
5
votes
1
answer
55
views
Use yield as expression for a quick in-database filter
I'm studying about coroutines and I came across an explanation, about how yield as an expression, locks the function waiting to receive a value. Thinking about it, I ended up having an idea, which is ...
0
votes
0
answers
41
views
How does Python return a generator and continue in unknown functions
When I have a function such as
def gen_5():
for i in range(5):
yield i
This can be utilized as
for i in gen_5():
print(i)
When the function gen_5 hits the keyword yield, a generator ...