You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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).
The text was updated successfully, but these errors were encountered: