Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reverse generator
#1
i like the simple way a generator can be created in python.  but i need something that is the opposite.   what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator).  a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

is this possible in python?

my internet connectivity is very flaky tonight, the DSL is up and down (more down than up) and i can't do a decent search (probably this is due to the extreme weather - at least i am staying inside and can do coding, but no testing).  and it is good that this forum allows a huge time span between opening a window for posting and doing the actual posting.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(Jan-10-2017, 09:28 AM)Skaperen Wrote: i like the simple way a generator can be created in python.  but i need something that is the opposite.   what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator).  a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

is this possible in python?

my internet connectivity is very flaky tonight, the DSL is up and down (more down than up) and i can't do a decent search (probably this is due to the extreme weather - at least i am staying inside and can do coding, but no testing).  and it is good that this forum allows a huge time span between opening a window for posting and doing the actual posting.
A class with a class member which is a list?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Generators are built to support this. Otherwise, yeah, you can roll your own class, since the example you have seems weird for Python's built-in generators which have to yield something every time a new value is passed into them.
Reply
#4
(Jan-10-2017, 09:28 AM)Skaperen Wrote: what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator). a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

How is what you're asking for different from a list? A list can do all of that. Use pop to yield data, append to send data (multiple items one at a time), it builds a list, and is a list whenever you're done.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Reverse generator do you mean coroutine ? http://www.dabeaz.com/coroutines/coprocess.py
Reply
#6
(Jan-10-2017, 07:10 PM)micseydel Wrote: Generators are built to support this. Otherwise, yeah, you can roll your own class, since the example you have seems weird for Python's built-in generators which have to yield something every time a new value is passed into them.

sorry, i went to the tutorial document to learn about generators.  it didn't even hint that there was more to learn, later.  it came across as complete.  and since i had done more in C, that came across as an area to expand on.  i've done some nasty coroutines in C such as one which ran just 2 CPU instructions per step, with one of them ("BALR  12,12" for IBM 360/370/390 systems).

but, at least, it told me about generator expressions.  interesting!  was that how you were suggesting to roll my own?


Quote:When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value.

so there is another way to start the generator?  hmmm.

(Jan-10-2017, 08:02 PM)ichabod801 Wrote:
(Jan-10-2017, 09:28 AM)Skaperen Wrote: what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator).  a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

How is what you're asking for different from a list? A list can do all of that. Use pop to yield data, append to send data (multiple items one at a time), it builds a list, and is a list whenever you're done.

this is why i don't like giving example uses, especially simple ones.  people often address the example rather than the issue.  i could give better examples.  but even most of these could be directly addressed.  i'm not going to try to come up with another example as i don't want to spend the time on it.

(Jan-10-2017, 09:12 PM)Larz60+ Wrote: Reverse generator do you mean coroutine ? http://www.dabeaz.com/coroutines/coprocess.py

since i have not yet studied decorations, i will, for now, avoid them.  at some point in time, soon, i will pick up on them. right now i am studying generators and will write some code for that, very soon.   maybe useless code that, like many classroom assignments, could likely be done better some other way.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
The reason why i brought up coroutines is that they are reverse generators.
Instead of a yield, they use 'send' which is 'sending' a value from a user routine
to the coroutine. Simply explained here
[url=http://www.juandebravo.com/2015/07/06/demystifying-coroutines-in-python/][/url]
Reply
#8
(Jan-10-2017, 07:10 PM)micseydel Wrote: Generators are built to support this. Otherwise, yeah, you can roll your own class, since the example you have seems weird for Python's built-in generators which have to yield something every time a new value is passed into them.

yielding something should be easy enough. yield what was sent to it (the caller can check if what it got is right).

or are you saying a mere class is sufficient ... if the caller is to get the results (it can just return them at the method call instead of bothering with a yield).

have any of you guys written any form of "reverse generator"?

(Jan-10-2017, 08:02 PM)ichabod801 Wrote:
(Jan-10-2017, 09:28 AM)Skaperen Wrote: what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator). a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

How is what you're asking for different from a list? A list can do all of that. Use pop to yield data, append to send data (multiple items one at a time), it builds a list, and is a list whenever you're done.

i want to also modify the data, such as compressing or decompressing.

(Jan-10-2017, 08:02 PM)ichabod801 Wrote:
(Jan-10-2017, 09:28 AM)Skaperen Wrote: what i need is the ability to run, keeping it's state, and, instead of (or maybe in addition to) the ability to yield data, the ability to send data in the other direction (to the reverse generator). a simple example would be a thing that accepts multiple data items one at a time and builds a list of them all, returning that list when done.

How is what you're asking for different from a list? A list can do all of that. Use pop to yield data, append to send data (multiple items one at a time), it builds a list, and is a list whenever you're done.

if there is a way to run some code in the background that pops data from one list, processes it, then appends it to another list, that might be interesting. it would need to use extra CPU cores and also work correct with other code appending to the list it pops from, and other code popping from the list it appends to (these lists acting as elastic pipes).

oh noo! this thread is old!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  receive from a generator, send to a generator Skaperen 9 5,425 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020