[::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

[::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

Post by HermannSW » Thu Sep 20, 2018 5:18 pm

Where/how could other slices be implemented/provided for (latest) MicroPython on ESP8266?

Code: Select all

$ python3 -c 'print("hello"[::-1])'
olleh
$ python3 webrepl_client.py 192.168.4.1
Password: abcd

WebREPL connected
>>> "hello"[::-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: only slices with step=1 (aka None) are supported
>>>
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: [::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

Post by jickster » Thu Sep 20, 2018 5:31 pm

HermannSW wrote:Where/how could other slices be implemented/provided for (latest) MicroPython on ESP8266?

Code: Select all

$ python3 -c 'print("hello"[::-1])'
olleh
$ python3 webrepl_client.py 192.168.4.1
Password: abcd

WebREPL connected
>>> "hello"[::-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: only slices with step=1 (aka None) are supported
>>>
https://github.com/micropython/micropyt ... ode.c#L189


Sent from my iPhone using Tapatalk Pro

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: [::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

Post by HermannSW » Thu Sep 20, 2018 10:52 pm

Thanks for pointing to code.

I just learned how to copy&paste pep8online.com validated function definition with webrepl_client.py in raw mode:

Code: Select all

$ python3 webrepl_client.py 192.168.4.1
Password: abcd

WebREPL connected
>>> rev
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'rev' is not defined
>>> \x01

raw REPL; CTRL-B to exit
>def rev(s):
    r = ""
    for c in s:
        r = c+r

    return r
\x04
OK>\x02

MicroPython v1.9.4-481-g3cd2c281d on 2018-09-04; ESP module with ESP8266
Type "help()" for more information.
>>> 
>>> exit
### closed ###
$ 

[::-1] can be done alternatively by rev() just defined:

Code: Select all

$ python3 webrepl_client.py 192.168.4.1
Password: abcd

WebREPL connected
>>> rev("hello")
'olleh'
>>> 

P.S:
Not sure whether recursive rev() is better or not, works the same in MicroPython:

Code: Select all

def rev(s):
    return "" if not(s) else rev(s[1::])+s[0]

Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: [::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

Post by HermannSW » Thu Sep 20, 2018 11:27 pm

Just learned how to define rev() more permanently with MicroPython.
First the function is defined in a module and copied over to ESP-01s:

Code: Select all

$ cat rev.py 
def rev(s):
    return "" if not(s) else rev(s[1::])+s[0]

$ python3 webrepl_cli.py rev.py 192.168.4.1:
Password: 
op:put, host:192.168.4.1, port:8266, passwd:abcd.
rev.py -> /rev.py
Remote WebREPL version: (1, 9, 4)
Sent 59 of 59 bytes
$ 
Next function gets imported and used:

Code: Select all

$ python3 webrepl_client.py 192.168.4.1
Password: abcd

WebREPL connected
>>> rev
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> from rev import *
>>> rev("foobar")
'raboof'
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: [::-1] -- NotImplementedError: only slices with step=1 (aka None) are supported

Post by jickster » Thu Sep 20, 2018 11:29 pm

Recursive .py

Big performance ouch but if that’s not an issue cool.


Sent from my iPhone using Tapatalk Pro

Post Reply