[Microbit-Python] Image to string and back again

Mark Shannon mark at hotpy.org
Thu Jan 5 16:49:44 EST 2017


Hi Matt,

Conventionally in Python the `repr` function should (for simple objects) 
produce the Python code to reproduce the object.
In other words, you need to use `eval(repr(Image.DUCK))`
to get a copy of the duck image.

Cheers,
Mark.

On 05/01/17 21:40, Matt Hickford wrote:
> Hi all. I wrote a program using the radio module to send images between
> microbits, but hit a snag converting the received string back to an Image.
>
> To demonstrate the problem:
>
>     >>> Image(str(Image.DUCK))
>
>     Traceback (most recent call last):
>
>     File "<stdin>", line 1, in <module>
>
>     ValueError: Unexpected character in Image definition.
>
>
> In the end, the solution was to send `repr(image)[7:36]` rather than
> `str(image)`, which felt less obvious than ideal.
>
> Could the Image(string) constructor be improved to strip the extraneous
> characters? Otherwise perhaps a note should go in the documentation [1]?
>
> -Matt
>
> [1] https://microbit-micropython.readthedocs.io/en/latest/image.html#microbit.Image
> <https://microbit-micropython.readthedocs.io/en/latest/image.html#microbit.Image>
>
>
>
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>


More information about the Microbit mailing list