Questions tagged [yield-from]

"yield from" is an expression in Python for delegating to a subgenerator.

The expression yield from was added in Python-3.3 based on PEP 380: Syntax for Delegating to a Subgenerator.

In the simplest case it can be used as a shorthand for iterating over a subgenerator, i.e.

for item in sth:
    yield item

can be replaced with:

yield from sth
59 questions
0
votes
1 answer

Recursing python dictionaries with yield from to generate list of nested dictionary keys

I want to generate a list of unique nested keys for each value in a dictionary such that: input_dict = {"a": {"b": "c", "d": {"e": "f"}}, "f": "g"} expected_result = [["a", "b"], ["a", "d", "e"], ["f"]] I thought something along these lines would…
wab
  • 797
  • 6
  • 19
0
votes
1 answer

How to use os.scandir() to also get back empty and non-empty directory names

In https://stackoverflow.com/a/33135143 the solution for returning recursively all file names in a directory structure looks like depicted below. I need also the information about each sub directory in the directory structure and the full path name…
7824238
  • 388
  • 4
  • 14
0
votes
2 answers

Why does this recursive ```yield from``` function not raise an error?

def prefixes(s): if s: yield from prefixes(s[:-1]) yield s t = prefixes('both') next(t) The next(t) returns 'b'. I'm just confused as to why this is because if we follow down the yield from statement, we will eventually end at…
WeyX
  • 11
0
votes
0 answers

`send`ing into `zip`ped generators with `yield from`

I am using Python 3.6 where you can nicely zip individual generators of one kind to get a multidimensional generator of the same kind. Take the following example, where get_random_sequence is a generator that yields an infinite random number…
wehnsdaefflae
  • 826
  • 2
  • 12
  • 27
0
votes
0 answers

Is it possible to write arbitrary depth delegated generators in Python?

I would like to write a class with the following interface. class Automaton: """ A simple automaton class """ def iterate(self, something): """ yield something and expects some result in return """ print("Yielding",…
0
votes
1 answer

Any alternatives for using "yield from" inside a by-reference generator?

I want to use some methods which use Generators as their return type and by-reference. However, the problem is that a method gets another method's return value, that is returned by-reference and is a Generator, and returns it by-reference. Plus,…
MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
0
votes
1 answer

'yield from' inside async function Python 3.6.5 aiohttp

SyntaxError: 'yield from' inside async function async def handle(request): for m in (yield from request.post()): print(m) return web.Response() Used python3.5 before, found pep525, install python3.6.5 and still receive this error.
TheUnity
  • 19
  • 2
0
votes
0 answers

`yield` throughout parsing in flask

First, I use flask. Second, I yield values like the code below. def upload_file(): if request.method == 'POST': f = request.files['file'] def generate(): yield '
'
            yield "Processing...\n"
           …
netdigger
  • 3,659
  • 3
  • 26
  • 49
0
votes
0 answers

Breaking into the iteration process of os.walk()

My goal is to store all files and directories in a structured data tree, where each: directory is a node file is a leaf My code below works fine. However, I only take one step at a time and interrupt/restart the walking process for every…
NewNewton
  • 1,015
  • 1
  • 10
  • 22
0
votes
2 answers

simpy using yield inside callback

I am trying to append callbacks to various events in a SimPy simulation, and I am finding that you can't use the yield keyword inside a callback. Is there a way to correctly do this, or do you have to only use callbacks within callbacks? For…
Allen Wang
  • 2,426
  • 2
  • 24
  • 48
0
votes
0 answers

How do I get the result of a yield from in Python 2

I've got code that looks somewhat like: @asyncio.coroutine def myfunction(): result = yield from bot.whois(something) # returns an asyncio.Future() return result['account'] How would I convert this to something that works in Python 2.7? Is…
Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
0
votes
1 answer

Why does this Python program using yield from not work?

I started with the following base code that works (Like the sample code at http://aaugustin.github.io/websockets/): import asyncio import websockets @asyncio.coroutine def servePlayer(websocket, path): yield from websocket.send("Hello…
-1
votes
1 answer

What happens if I do not yield anything to `yield from`?

What happens if I do not yield anything to yield from? This is an example: def inner(x): if x > 0: yield "Greater than zero" elif x==0: yield "Zero" def outer(): yield from inner(x) In this case inner(x) yields…
albertopasqualetto
  • 87
  • 1
  • 1
  • 11
-1
votes
1 answer

strange result from yield from in python

I have a depth-first search example code in python as below. def DFS_paths_recursive(self, start, end, path = None): if path == None: path = [start] if start == end: yield path else: unvisited =…
Sissi
  • 65
  • 1
  • 10
1 2 3
4