There is no reason to respond so aggressive, but I saw another similar tone recent posting from you to somebody else.
Your example does not even match the question.
And I was able to implement isalpha() for complete unicode as module that runs even on ESP8266, not in firmware.
Seems that I am not the noob you stated.
I started looking on unicode character distribution for characters with isalpha True.
It turned out that all unicode characters starting and above 0x300000 have isalpha False.
Next I looked at 256 character pages, there are 768 below 0x300000.
There are 339 pages with 256 False entries and 353 pages with 256 True entries for isalpha.
Only 76 pages have mixed True/False entries.
It turned out that isalpha has more True values for Python 3 (101013) than the 99537 in Python 2.
"gen_blk.py" is a Python 3 script that outputs "isalpha.py" MicroPython script.
Biggest part is 768 entries mixed array "ISALPHA" of booleans and 256bit integers.
Function "isalpha(str)" is implementation for whole unicode.
Since all character processing like "for c in str" creates length 1 strings, these strings end up as interned strings.
I did add "tst(strt,len)" function for testing "isalpha()".
But that runs oom even on 100KB free memory ESP32 when testing all 1114112 unicode characters
(because of the more than 1 million interned strings).
So I added "tst_(strt,len)" function passing unicode character ord value to function "_()" called from "isalpha()" as well.
That allows to test whole unicode character set.
"tstPlanes()" is a convenience function testing all 17 (default) or only first 3 multilingual (65536 chars) planes (with False arg).
"tstPlanes()" returns correctly 101013 isalpha characters found, 2nd response is time in ms, 3rd is average time per call.
I tested on Intel laptop Python3,
Code: Select all
$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import isalpha
>>> isalpha.tstPlanes(False)
(101013, 202.113525390625, 0.0010280025502045949)
>>>
on ESP32
Code: Select all
>>>
MicroPython v1.9.4-623-g34af10d2e on 2018-10-03; ESP32 module with ESP32
Type "help()" for more information.
>>> import isalpha
>>> isalpha.tstPlanes(False)
(101013, 11504, 0.05851237)
>>>
as well as ESP8266:
Code: Select all
>>>
MicroPython v1.9.4-272-g46091b8a on 2018-07-18; ESP module with ESP8266
Type "help()" for more information.
>>> import isalpha
>>> isalpha.tstPlanes(False)
(101013, 38704, 0.196859)
>>>
Python3 on laptop needs 1µs, MicroPython on ESP32 58µs and on ESP8266 196µs per function call on average.
Python3 says sys.getsizeof(ISALPHA)=6232. But importing module in MicroPython takes 10KB memory, here for ESP8266:
Code: Select all
>>>
MicroPython v1.9.4-272-g46091b8a on 2018-07-18; ESP module with ESP8266
Type "help()" for more information.
>>> gc.collect()
>>> gc.mem_free()
28608
>>> import isalpha
>>> gc.mem_free()
16848
>>> gc.collect()
>>> gc.mem_free()
18416
>>>
This is a part of ISALPHA array near end:
Code: Select all
..., True, True,
115792089237316195423570985008687907853269984665640564039439146271038674829311,
1073741823,
False, False, ...
This is function isalpha, calling out to "_()":
Code: Select all
def isalpha(str):
for c in str:
if not(_(ord(c))):
return False
return True
And here is where the work is done:
Code: Select all
def _(i):
if i>=0x030000:
return False
else:
b = i//256
if type(ISALPHA[b])==type(True):
return ISALPHA[b]
else:
return ISALPHA[b] & (1<<(i%256)) != 0
Summary:
- isalpha.py can be used as long as 10KB of free memory is available
- ESP8266 average "isalpha()" time is only 3.8 times that of ESP32
- files attached