[Tutor] type hint woes regex

2019-03-02 Thread ingo
In a first attempt to add types I ran into trouble. First a code snippet 
( full code at 
https://gist.github.com/ingoogni/460676b11e5a04ed3a3ac93ae0e1fddd )


_matchline: Pattern[str] = re.compile(r"(?P^\s*$)|(?P[\t]*)"
"((?P[A-Z]+):)?([ \t]*)(?P.*)")


def proc_matchlns(momfile: TextIO) -> Iterator[Dict[str, Union[str, int]]]:
  for i, ln in enumerate(momfile, 1):
match: Match[str] = _matchline.match(ln) #error 1
m: Dict[str, str] = match.groupdict()#error 2
m['ln'] = ln.rstrip()
m['ln#'] = i #error 3
yield m  #error 4

Depending on how I add types mypy nags on various aspects:

error 1:
  match seems to want type Optional[Match[str]], but why? The regex 
always has a result (I hope).


error 2:
  when I give in to the above, the result is that it errors on not 
being able to de a groupdict on a null type.


error 3:
  groupdict results in a type dict[str, str], but I want to add an int. 
Can I change the type? (In this specific case could change i to str(i) 
as it has no influence on the program)


error 4:
  result of 3 and output type, type of m and specified output type 
don't match.



Ingo

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Is this the preferred way to change terminal screen color using curses?

2019-03-02 Thread boB Stepp
I wanted to be able to change the background screen color of a
terminal window using curses.  My weak Google-Foo did not turn up a
clear example of how to do this despite much searching.  The two
_obvious_curses methods to attempt this seemed to be
window.bkgdset(ch, attr) to initially set a window's background
attributes and window.bkgd(ch, attr) to change them to new values.
The thing that has puzzled me is that "ch" is a mandatory parameter to
supply.  So after a variety of experimental efforts I came up with the
following approach which seems to do what I want to do -- just change
the terminal's background color at will.  But since I did *not* locate
any clear examples online on how to do this, I cannot help but wonder
if there is an easier approach to do what I want to do?

My code follows.  As always I am open to any constructive criticism
even if it is off this email's topic, though this code is not meant to
be a polished product.

#!/usr/bin/env python3

import curses

def start_cli(stdscr):
max_height, max_width = stdscr.getmaxyx()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN)
PAIR_BLACK_ON_CYAN = curses.color_pair(1)
stdscr.clear()
stdscr.bkgdset(' ', PAIR_BLACK_ON_CYAN)

# Fill screen with spaces to color screen background:
for y in range(max_height):
try:
stdscr.addstr(y, 0, ' ' * max_width)
except curses.error as error:
if y == max_height - 1:
# Ignore error from writing to lower right-hand screen corner:
pass
else:
raise error

# Make cursor invisible to ensure *entire* screen is colored:
curses.curs_set(0)
stdscr.refresh()

# Pause program until user presses key:
stdscr.getkey()

# Change background screen color:
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_RED)
PAIR_BLACK_ON_RED = curses.color_pair(2)
change_bkgd_color(stdscr, PAIR_BLACK_ON_RED)
stdscr.getkey()

def change_bkgd_color(window_obj, color_pair):
window_obj.bkgd(' ', color_pair)

if __name__ == '__main__':
input("Press ENTER to change screen to first color, then press"
" any key for next color change until the program exits.")
curses.wrapper(start_cli)

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this the preferred way to change terminal screen color using curses?

2019-03-02 Thread Cameron Simpson

On 02Mar2019 15:16, boB Stepp  wrote:

I wanted to be able to change the background screen color of a
terminal window using curses.  My weak Google-Foo did not turn up a
clear example of how to do this despite much searching.  The two
_obvious_curses methods to attempt this seemed to be
window.bkgdset(ch, attr) to initially set a window's background
attributes and window.bkgd(ch, attr) to change them to new values.
The thing that has puzzled me is that "ch" is a mandatory parameter to
supply.


Remember that the Python curses module is a shim for the curses (or 
ncurses) C library. So "man 3 curses" gets you the main library page, 
and there are distinct manual entries for the various functions.


On my machine (a Mac) the curses(3) manual entry has a section named 
"Routine Name Index" thus:


  Routine Name Index
  The following table lists each curses routine and the name of the man‐
  ual page on which it is  described.   Routines  flagged  with  “*” 
  are ncurses-specific, not described by XPG4 or present in SVr4.


and then there's a long table of functions and the match manual entry 
name. bkgdset is listed as in the curs_bkgd(3) entry, so run "man 
curs_bkgd" to view that manual entry. It describes the bkgdset,  
wbkgdset,  bkgd,  wbkgd,  getbkgd functions in some detail.


Also, the curs_color manual entry talks about terminal colours in 
detail. So "man curs_color".



So after a variety of experimental efforts I came up with the
following approach which seems to do what I want to do -- just change
the terminal's background color at will.  But since I did *not* locate
any clear examples online on how to do this, I cannot help but wonder
if there is an easier approach to do what I want to do?


I would be inclined to call getbkgd() and then modify what it gives you 
as desired, then call bkgdset(). Untested.



My code follows.  As always I am open to any constructive criticism
even if it is off this email's topic, though this code is not meant to
be a polished product.

[...]

   # Fill screen with spaces to color screen background:
   for y in range(max_height):
   try:
   stdscr.addstr(y, 0, ' ' * max_width)
   except curses.error as error:
   if y == max_height - 1:
   # Ignore error from writing to lower right-hand screen corner:
   pass
   else:
   raise error


Is the painting of the screen with spaces actually required? I would 
have thought not (again, untested). The main window (stdscr) should 
start filled with spaces.


[Reads more closely...] probably you want to call bkgd() or wbkgd() 
instead.  "man curs_bkgd" says:


  bkgd
  The bkgd and wbkgd functions set the background property of the
  current or  specified  window  and  then  apply this setting to every
  character position in that window:
  ·   The rendition of every character on the screen is  changed to
  the new background rendition.
  ·   Wherever  the former background character appears, it is changed
  to the new background character.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this the preferred way to change terminal screen color using curses?

2019-03-02 Thread boB Stepp
On Sat, Mar 2, 2019 at 4:28 PM Cameron Simpson  wrote:

> Is the painting of the screen with spaces actually required? I would
> have thought not (again, untested). The main window (stdscr) should
> start filled with spaces.

I had read this along the way, but had forgotten it.

> [Reads more closely...] probably you want to call bkgd() or wbkgd()
> instead.  "man curs_bkgd" says:
>
>bkgd
>The bkgd and wbkgd functions set the background property of the
>current or  specified  window  and  then  apply this setting to every
>character position in that window:
>·   The rendition of every character on the screen is  changed to
>the new background rendition.
>·   Wherever  the former background character appears, it is changed
>to the new background character.

I had seen this, but I have been giving way too much credence to the
names given to these methods.  This is the second time I have been
badly burned by the names used.  I had ASSumed that if there is a
bkgdset() method, that the window attributes need to be initialized
first if one is not satisfied with the default behavior.  And I *did*
try using bkgdset() by itself without manually populating spaces, but
it did not change the color of anything but the window border I had
used in my original trial code.

I tried your suggestion with bkgd() and it worked beautifully.

BTW, my Linux Mint installation did *not* have the man pages for
ncurses, even though it was installed.  I had to manually fetch the
man pages myself.

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this the preferred way to change terminal screen color using curses?

2019-03-02 Thread Cameron Simpson

On 02Mar2019 22:32, boB Stepp  wrote:

BTW, my Linux Mint installation did *not* have the man pages for
ncurses, even though it was installed.  I had to manually fetch the
man pages myself.


Maybe they're in a separate -dev or -doc package? Sometimes the base 
package (eg "libncurses") contains only the library, not header files or 
doco wanted for development.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor