Why use curses.ascii.islower?

I just stumbled upon curses.ascii.islower () . It checks if the received character is lowercase. What is the use of using this function as opposed to str.islower () ? Yes, this requires converting an ASCII character to a string object, which adds overhead. But besides this, are there any advantages?

One drawback that I found is the need for an additional library, which may or may not be available.

+6
source share
2 answers

Timing seems to be str.islower much more efficient, so you need more than just overhead:

python2:

 In [68]: timeit islower("f") 1000000 loops, best of 3: 641 ns per loop In [69]: timeit "f".islower() 10000000 loops, best of 3: 50.5 ns per loop 

python3

 In [2]: timeit "f".islower() 10000000 loops, best of 3: 58.7 ns per loop In [3]: timeit islower("f") 1000000 loops, best of 3: 801 ns per loop 

One difference / advantage is that you actually do not need to throw the str object, you can pass either a single character string or an integer.

 In [38]: ascii.islower(97) Out[38]: True 

But using chr with str.lower even more efficient:

 In [51]: timeit ascii.islower(122) 1000000 loops, best of 3: 583 ns per loop In [52]: timeit chr(122).islower() 10000000 loops, best of 3: 122 ns per loop 

The only curses link howto documentation describes the use of curses.ascii , how this can be useful when using the curses

 while 1: c = stdscr.getch() if c == ord('p'): PrintDocument() elif c == ord('q'): break # Exit the while() elif c == curses.KEY_HOME: x = y = 0 

The curses.ascii module provides ASCII class membership functions that accept either integer or 1-character arguments; they can be useful in writing more readable tests for your command interpreters. It also provides conversion functions that accept either integer or 1-digit string arguments and return the same type.

I think it will be difficult for you to find any advantage using ascii.islower over str.islower outside of anything related to the curses module.

+2
source

Give a few slightly different tests, trying to check only the speed of the function (without searching for attributes or differences in the namespace).

Tested in Python3.4 (you can easily run them yourself)

 python3 -m timeit -s "f = str.islower" \ "f('A')" 

0.171 usec per loop


Compared with:

 python3 -m timeit \ -s "import curses.ascii; f = curses.ascii.islower; del curses" \ "f('A')" 

0.722 usec per loop


In practice, you would not call it that.

0
source

Source: https://habr.com/ru/post/982629/


All Articles