Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to Iterate though an iterable, without Julia-ising the contents #153

Closed
oxinabox opened this issue Jun 10, 2015 · 2 comments
Closed

Comments

@oxinabox
Copy link

I am trying to work out how to iterate though an python iterable, without the contents being converted to Julia types -- i need them to stay as PyObjects.

I have now created a wrapper to do so:
it is my first time using julia's iterators, and its pretty rough.
I'm also not sure if there isn't already a way to do it (though I've been looking for one for quiet a while).

#state = inner_inst, next
immutable PyObjectIter
    inner::PyObject 

    function PyObjectIter(obj::PyObject)
        inner = pycall(obj["__iter__"],PyObject)
        new(inner)
    end
end
function next_or_nothing(pyiter::PyObject)
    try
        pycall(pyiter["next"],PyObject)
    catch PyError
        nothing
    end
end

function Base.start(iter::PyObjectIter)
    inner_inst = deepcopy(iter.inner)
    next = next_or_nothing(inner_inst)
    inner_inst, next
end

function Base.next(pyiter::PyObjectIter, state::(PyObject,Union(Nothing,PyObject)))
    inner_inst, next = state
    new_next = next_or_nothing(inner_inst)
    next, (inner_inst,new_next)
end

function Base.done(pyiter::PyObjectIter, state::(PyObject,Union(Nothing,PyObject)))
    inner_inst, next = state
    next==nothing
end
@stevengj
Copy link
Member

stevengj commented Jul 2, 2015

You shouldn't need deepcopy etc. Basically, you just need to follow the existing PyObject iterator but get rid of the conversion to PyAny.

@tkf
Copy link
Member

tkf commented Nov 4, 2018

closed by #594

@tkf tkf closed this as completed Nov 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants