How to transfer code properly via REPL paste mode?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
allankliu
Posts: 11
Joined: Fri Mar 30, 2018 11:13 am

How to transfer code properly via REPL paste mode?

Post by allankliu » Fri Mar 30, 2018 11:45 am

MicroPython code deployment is different in various boards. For some reason, My ESP8266 doesn't work any more for webREPL, even I reflash the stable firmware time by time.

And I have to test the code via serial. It is possible to paste the code via REPL, and save the pasted code string into a file in filesystem and get it runing.

But I find an issue for this mode. No matter Ctrl-A for raw REPL mode or Ctrl-D paste mode, the '\r\n' or '\x0D\x0A' will be converted into real return and newline anyway. When this string is saved into filesystem, the python code has syntax error.

For example:
'''
if not line or line == b'\r\n':
break
'''

will be saved as:
'''
if not line or line == b'
: break
'''

The latter will be invalid python code.

Now I can not get them works. Sad.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to transfer code properly via REPL paste mode?

Post by pythoncoder » Sat Mar 31, 2018 8:34 am

I use two methods. Paste mode is selected with ctrl-e. This allows you to paste code and immediately run it with ctrl-d, and is a great feature of MicroPython for running small samples.

Longer programs should be saved as a file on your PC then transferred to the target. There are various ways to do this but I favour rshell as it has a number of powerful features.
Peter Hinch
Index to my micropython libraries.

allankliu
Posts: 11
Joined: Fri Mar 30, 2018 11:13 am

Re: How to transfer code properly via REPL paste mode?

Post by allankliu » Mon Apr 02, 2018 12:48 am

Thanks, @pythoncoder,

Your answer is very helpful, I can not wait to try out rshell.

Actually I tried WebREPL and command line at first, but for some unknown reason, now WebREPL keeps me trying to "import webreply_setup", and refuse to start webrepl anyway. So that's why I tried to use Ctrl-E, to paste code, and then write pasted code into a remote python code to run. What a big loop.

And in Ctrl-E mode, if I try to transfer code with "\r\n", now I have to escapte them first, by writing "\\r\\e".

Anyway, now I got extra tools like rshell/uPyLoader to transfer source file.

Thank you.

jpvlsmv
Posts: 1
Joined: Mon Apr 09, 2018 10:27 pm

Re: How to transfer code properly via REPL paste mode?

Post by jpvlsmv » Mon Apr 09, 2018 10:35 pm

Use Python's raw strings:

ctrl-E
code=r'''(paste)'''
ctrl-D

then save that variable into the target file ( with open('target.py','w') as f: f.write(code) )

It'll transfer everything except nested triple-quoted strings, without additional backslash-escaping.

Post Reply