weix.us

To slice a generator

The next() and islice() functions allow you to access specific elements of generators in python.

  • next(iterator) gives the next item from an iterator (including a generator). I ended up using this one because multi-document YAML readers in python return a generator, but markdown documents with YAML frontmatter count as multiple documents. So this ended up being the nicest way I could think of to access just that frontmatter data.
from ruamel.yaml import YAML

with open(f, "r") as stream:
    fm = next(YAML().load_all(stream))
  • islice(iterator, index, index+1) returns a specific element from a generator. Haven’t had to use this one yet, but I thought it would be useful to include as a trick.
from itertools import islice

files = sorted(list(Path(".").glob("*.png")) + list(Path(".").glob("*.PNG")))
  • islice(iterator, index, None) returns all elements starting at the specified index from a generator. Since generators don’t load the elements into memory until they’re used, it can be a nice way to conserve memory when working with large collections of images.
from itertools import islice
from PIL import Image

files = sorted(list(Path(".").glob("*.png")) + list(Path(".").glob("*.PNG")))
images = (Image.open(f.stem + ".jpg") for f in files)
next(images).save(output.pdf, "PDF", resolution=100.0, save_all=True, append_images=islice(images,1,None))

Posted by Elliott Weix.