Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use curses.ascii.islower?

I've just recently stumbled upon curses.ascii.islower(). It checks if the passed character is lower case. What is the benefit in using this function as opposed to str.islower()? Yes, it requires a conversion of a ASCII character to string object, which adds overhead. But other than that are there any advantages?

One disadvantage I found is the need for extra library, which may or may not be available.

like image 485
ilya1725 Avatar asked Feb 17 '15 20:02

ilya1725


1 Answers

Timing both it seems str.islower is a lot more efficient so it is not just the overhead of needing to import:

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 you don't actually have to cast to a str object, you can pass either a one character string or an integer.

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

But using chr with str.lower is still 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 reference the curses howto documentation makes about the use of curses.ascii is how it may be useful when using the curses library:

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 supplies ASCII class membership functions that take either integer or 1-character-string arguments; these may be useful in writing more readable tests for your command interpreters. It also supplies conversion functions that take either integer or 1-character-string arguments and return the same type.

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

like image 167
Padraic Cunningham Avatar answered Oct 03 '22 12:10

Padraic Cunningham