Pass a Julia string to Python as an iterable

question

#1

Is it possible to pass a Julia String (or alternatively a Vector{UInt8} or IOBuffer wrapping the same) to Python as an iterable object?

I have attempted to pass the data directly, and also as part of a Julia generator (e.g. (c for c in s)) to no avail.

The use case is calling upload_object_via_stream in LibCloud.jl without writing a file to disk.


#2

is there a pycall convert function you can overload?


#3

Please provide an example of the issue. PyCall converts Julia strings to Python ‘str’, and ‘str’ is iterable, so the following works as expected:

pyeval("[x for x in y]", y="hello")

#4
using LibCloud.Storage

storage = StorageDriver(region, aws_key, aws_secret)
bucket =  get_container(storage, bucketname)
upload_object_via_stream(container, "a test string", "cloudfile")

ERROR: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'str' object has no attribute 'next'",)

I would like to be able to pass some data in memory (in this case “a test string”) in a way over which Python would be happy to iterate. I get the impression I may misunderstand what type of object Python is expecting…

Any suggestions?


#5

Python strings don’t have a next attribute. Maybe it is expecting a bytearray, which you can pass from Julia via Vector{UInt8}("a test string").


#6

As per the original post, this also doesn’t work, failing with the same error:

upload_object_via_stream(container, Vector{UInt8}("a test string"), "cloudfile")

ERROR: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'bytearray' object has no attribute 'next'",)

#7

Well, you need to read the documentation for the Python module. Figure out what works in Python, and then translate it to the corresponding Julia code.


#8

According to the documentation the argument should be:

iterator (object) – An object which implements the iterator interface.

I will do some digging.

Thanks for your time.


#9

Use StringIO.


#10

This works perfectly, thank you.