Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a "consuming-generator"
#1
a concept and coding scheme i once created in C (as well as C could do this) i would (now days) call a "consuming-generator". but it was really more like a "filter". basically, it accepted a sequence of data items from the caller and returned new data items to the caller. in both cases this was done in C by the code calling a function, passing it some data, or not (if there wasn't any but the caller was just checking for any). then the function would return some data, or not (if there wasn't any to return). a totally different thing was used to give or take an EOF. i wrote a couple filters this way, one of which did some run-length compression, so it would be given more data than it gave back.

in python, i don't think a generator can do this. but maybe a co-routine could. has this kind of thing been done? are there any helper modules for it?
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
It's not clear what you're trying to do here. From your description and your use of the word "filter", I can't tell if what you want is actually the filter function (when you say "it would be given more data than it gave back", that suggests filter to me, for example). Coroutines are for asynchronous programming and that doesn't sound like it's relevant here.

Perhaps you could give a more detailed example, or even a code sample (even if pseudocode) for a simple one of the things you wrote?
Reply
#3
a "filter" in the general computer science terminology is a thing or object (could be a function, a class, a command, a module, a server,a machine, a robot, pretty much anything) that takes some input and produces some output that is based on the input. common filters do things like translation, recoding, compression, a so forth. many functions are considered to be filters in this sense, such as hex() in python.

what i was doing in C was trying to make the construction of more complex filters easier to interface with, especially filters that might need more input than initially given to it to give more output. compression often encounters this where one chunk of input has the beginning of a long run of spaces an its end and the next chunk has more spaces. the output will eventually have a code that represents that run of spaces. this is a "stateful filter" because it is keeping some of that data for a while between accesses. that kind of thing is hard to do well in just about any language. it's why unix shells do pipelines (commands or executable programs being the filters).
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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