bendudson / py4cl

Call python from Common Lisp
Other
233 stars 31 forks source link

how to iterate python Iterable implementing objects, eg #S(PY::PYTHON-OBJECT :TYPE "<class 'generator'>" :HANDLE 5) #58

Open vxe opened 2 months ago

vxe commented 2 months ago

equivalent in python:

for i in GENERATOR_OBJECT:
    print(i['output'])

thanks in advance

digikar99 commented 2 months ago

You can repeatedly call the python's next operator on the iterator/generator, until it raises a StopIteration error.

(defpackage :python-user
  (:use :cl)
  (:local-nicknames (:utils :alexandria)
                    (:py :py4cl)))

(in-package :python-user)

(let ((range (py:python-eval "iter(range(2))")))
  (loop :for obj := (utils:ignore-some-conditions (py:python-error)
                      (py:python-call "next" range))
        :while (print obj)))