[Microbit-Python] Image to string and back again

Matt Hickford matt.hickford at gmail.com
Thu Jan 19 17:06:47 EST 2017


Hi Mark. Thanks for your reply. To clarify, the problem is str(image)
contains extra spaces, newlines and single quotes compared to the format
expected by Image(str)

    >>> str(Image.DUCK)
    "Image(\n    '09900:'\n    '99900:'\n    '09999:'\n    '09990:'\n
 '00000:'\n)"
    >>> Image(str(Image.DUCK))
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: Unexpected character in Image definition.

The formats are only slightly different. My suggestion would be make
Image(str) automatically strip the extraneous characters so that the round
trip Image(str(Image.DUCK)) would just work.

As you say, another workaround is to use eval—assuming you trust the data.
My app receives strings over radio, so I went with stripping characters.
-Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/microbit/attachments/20170119/db74c386/attachment.html>


More information about the Microbit mailing list