How do I modify a generator in Python?

2024/9/28 9:31:01

Is there a common interface in Python that I could derive from to modify behavior of a generator?

For example, I want to modify an existing generator to insert some values in the stream and remove some other values.

How do I do that?

Thanks, Boda Cydo

Answer

You can use the functions provided by itertools to take a generator and produce a new generator.

For example, you can use takewhile until a predicate is no longer fulfilled, and chain on a new series of values.

Take a look at the documentation for other examples, including things like ifilter, dropwhile and islice to name just a few more.

https://en.xdnf.cn/q/71356.html

Related Q&A

Reading a website with asyncore

I would like to read a website asynchronously, which isnt possible with urllib as far as I know. Now I tried reading with with plain sockets, but HTTP is giving me hell. I run into all kind of funky en…

How to select specific the cipher while sending request via python request module

Usecase: I want to find out how many ciphers are supported by the hostname with python request module.I am not able to find a way to provide the cipher name to request module hook. Can anyone suggest …

Different classes made by type with the same name in Python?

I was playing around with metaclasses in Python and found something very curious. I can create two classes with the same name, but that are actually different objects. See:>>> def create_class…

Installing python server for emacs-jedi

I am trying to install Jedi for emacs using marmalade package manager by following instructions here -- http://tkf.github.io/emacs-jedi/latest/. The package manger installs Jedi along with its dependen…

Multi-feature causal CNN - Keras implementation

Im currently using a basic LSTM to make regression predictions and I would like to implement a causal CNN as it should be computationally more efficient.Im struggling to figure out how to reshape my cu…

Adding a join to an SQL Alchemy expression that already has a select_from()

Note: this is a question about SQL Alchemys expression language not the ORMSQL Alchemy is fine for adding WHERE or HAVING clauses to an existing query:q = select([bmt_gene.c.id]).select_from(bmt_gene) …

How should I move blobs from BlobStore over to Google Cloud Storage?

Our application has been running on App Engine using the Blobstore for years. We would like to move our video files over to Google Cloud Storage. What is the best practice for migrating large blobs f…

Python: Find `sys.argv` before the `sys` module is loaded

I want to find the command line arguments that my program was called with, i.e. sys.argv, but I want to do that before Python makes sys.argv available. This is because Im running code in usercustomize.…

Dotted lines instead of a missing value in matplotlib

I have an array of some data, where some of the values are missingy = np.array([np.NAN, 45, 23, np.NAN, 5, 14, 22, np.NAN, np.NAN, 18, 23])When I plot it, I have these NANs missing (which is expected)f…

How to change the creation date of file using python on a mac?

I need to update the creation time of a .mp4 file so that it will appear at the top of a list of media files sorted by creation date. I am able to easily update both the accessed and modified date of …