Re: Tools to help with text mode (i.e. non-GUI) input

2025-01-17 Thread Grant Edwards via Python-list
On 2025-01-17, Alan Gauld via Python-list  wrote:
> On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
>> Alan Gauld  writes:
>>> On 11/01/2025 14:28, Chris Green via Python-list wrote:
 I'm looking for Python packages that can help with text mode input,
>>>
>>> The standard package for this is curses which comes as part
>>> of the standard library on *nix distros.
>> 
>> The thing about curses (which may or may not be a problem) is that, by
>> design, it takes over the whole screen.  If you want to do simpler text
>> manipulations (showing a dismissible message, showing bold text, etc.)
>> without interfering with existing text, curses can't do it, at least not
>> easily.
>
> It's not that difficult to use the terminfo codes directly.

It's easy to do, but it's tricky to do it right.

https://github.com/GrantEdwards/Python-curses-and-terminfo

> [...]
>
> Here is "hello world" in bold...
>
> import curses
> curses.setupterm()
> bold = curses.tigetstr('bold').decode('ascii')
> normal = curses.tigetstr('sgr0').decode('ascii')
>
> print(bold, 'Hello world', normal)

Don't forget to use tparm() to parameterize strings for things like
"move curser".

Once you've parameterized the string (if needed), you've _might_ need
to worry about the stuff in the string that's meant to be interpreted
by tputs/putp:

Quoting man tparm(3)

   All terminfo strings [including the output of tparm] should be
   printed with tputs or putp.

That's becuase terminfo strings can contain escape sequences that are
filterd out and interpreted by tputs/putp. The approach above only
works if you only care about certain terminals, and you know that none
of the terminfo strings you're using have those interal terminfo
escape sequences in them [AFAIK, that's true for the linux console,
xterm and the like, but not for many serial terminals.]

--
Grant


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tools to help with text mode (i.e. non-GUI) input

2025-01-17 Thread Mats Wichmann via Python-list

On 1/17/25 12:03, Keith Thompson via Python-list wrote:

Alan Gauld  writes:

On 15/01/2025 00:41, Keith Thompson via Python-list wrote:

Alan Gauld  writes:

On 11/01/2025 14:28, Chris Green via Python-list wrote:

I'm looking for Python packages that can help with text mode input,
I haven't followed this whole thread, but rich (low-level) and textual 
(higher-level) are designed for this - part of the same project family - 
and really worth a look. I know someone else mentioned rich earlier.


There's a little video on the textual homepage that shows some of what 
it can do in action:


https://github.com/Textualize/textual

--
https://mail.python.org/mailman/listinfo/python-list


Re: Tools to help with text mode (i.e. non-GUI) input

2025-01-17 Thread Keith Thompson via Python-list
Alan Gauld  writes:
> On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
>> Alan Gauld  writes:
>>> On 11/01/2025 14:28, Chris Green via Python-list wrote:
 I'm looking for Python packages that can help with text mode input,
>>>
>>> The standard package for this is curses which comes as part
>>> of the standard library on *nix distros.
>> 
>> The thing about curses (which may or may not be a problem) is that, by
>> design, it takes over the whole screen.  If you want to do simpler text
>> manipulations (showing a dismissible message, showing bold text, etc.)
>> without interfering with existing text, curses can't do it, at least not
>> easily.
>
> It's not that difficult to use the terminfo codes directly. But
> that won't give access to things like lists of default values, mouse
> control etc that the OP wanted. But for simple text characteristics
> and moving the cursor around terminfo works ok even if a bit tedious.
>
> Here is "hello world" in bold...
>
> import curses
> curses.setupterm()
> bold = curses.tigetstr('bold').decode('ascii')
> normal = curses.tigetstr('sgr0').decode('ascii')
>
> print(bold, 'Hello world', normal)

Cool -- but I think you want:

print(bold, 'Hello world', normal, sep='')

> Whilst you can position the cursor etc it very quickly
> becomes easier to just use full blown curses.

-- 
Keith Thompson (The_Other_Keith) keith.s.thompso...@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Struggling to understand Callable type hinting

2025-01-17 Thread dn via Python-list

On 18/01/25 12:33, Ian Pilcher via Python-list wrote:

I am making my first attempt to use type hinting in a new project, and
I'm quickly hitting areas that I'm having trouble understanding.  One of
them is how to write type hints for a method decorator.

Here is an example that illustrates my confusion.  (Sorry for the
length.)


import collections.abc

class BufferScanner(object):


...


     @staticmethod
     def _check_eof(method: collections.abc.Callable -> (
     collections.abc.Callable
     ):

...


I cannot figure out how to correctly specify the Callable argument and
return type for _check_eof().  As indicated by the name, method should
be a method (of the BufferScanner class), so its first positional
argument should always be an instance of BufferScanner, but it could
have any combination of positional and/or keyword arguments after that.


Is it a typing problem?
The def is not syntactically-correct (parentheses).
What happens once corrected?

Also, which tool is 'complaining', and what does it have to say?

General comment: as far as type-hints go, rather than trying to learn 
how to deal with complex situations, it might be better to ease-in 
gradually - add the easy stuff now, and come back to deal with the rest 
later (otherwise the typing 'tail' is wagging the coding 'dog'!)


--
Regards,
=dn

--
https://mail.python.org/mailman/listinfo/python-list


Re: Strategies for avoiding having to use --break-system-packages with pip

2025-01-17 Thread Peter J. Holzer via Python-list
On 2025-01-14 11:32:35 +, Chris Green via Python-list wrote:
> Use a virtual environment, what do I have to do then to make using
> my program (that uses tkintertable) 'transparent', i.e. I just
> want to be able to run the program from the command prompt like
> any other program.

Just use the python interpreter in the venv in the hashbang line.

For example, here's the first line of one my scripts:

#!/usr/local/share/wds/venv/bin/python3

As you can probably guess, the venv is in /usr/local/share/wds/venv.

There is no need for wrapper scripts which activate the venv. Python
does that all by itself.

I have a small script, install-python[1], to assist with setting the
hashbang, but if it's just a few scripts you can simply edit it manually.

hp

[1] https://git.hjp.at:3000/hjp/install-python/src/branch/master/install-python


-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Struggling to understand Callable type hinting

2025-01-17 Thread Ian Pilcher via Python-list

I am making my first attempt to use type hinting in a new project, and
I'm quickly hitting areas that I'm having trouble understanding.  One of
them is how to write type hints for a method decorator.

Here is an example that illustrates my confusion.  (Sorry for the
length.)


import collections.abc

class BufferScanner(object):

def __init__(self, buf: str) -> None:
self._buffer = buf
self._index = 0
self._eof = False
self._line = 1
self._column = 1

@property
def eof(self) -> bool:
return self._eof

@staticmethod
def _check_eof(method: collections.abc.Callable -> (
collections.abc.Callable
):
def wrapper(*args, **kwargs):
self = args[0]
if not self._eof:
method(*args, **kwargs)
if self._index >= len(self._buffer):
self._eof = True
return self._eof
return wrapper

@_check_eof
def next_line(self) -> None:
"""Advance the scanner to the beginning of the next line."""
try:
i = self._buffer.index('\n', self._index)
except ValueError:
self._index = len(self._buffer)  # decorator will set _eof
return
self._index = i + 1  # move past the newline
self._line += 1
self._column = 1


I cannot figure out how to correctly specify the Callable argument and
return type for _check_eof().  As indicated by the name, method should
be a method (of the BufferScanner class), so its first positional
argument should always be an instance of BufferScanner, but it could
have any combination of positional and/or keyword arguments after that.

I've read the TypeVar and ParamSpec documentation, and me head is
spinning, but neither one really seems to help with this situation.

Any pointers to good resources or the correct way to do this are
appreciated.

Thanks!

--

If your user interface is intuitive in retrospect ... it isn't intuitive


--
https://mail.python.org/mailman/listinfo/python-list


Re: Tools to help with text mode (i.e. non-GUI) input

2025-01-17 Thread Keith Thompson via Python-list
Alan Gauld  writes:
> On 11/01/2025 14:28, Chris Green via Python-list wrote:
>> I'm looking for Python packages that can help with text mode input,
>
> The standard package for this is curses which comes as part
> of the standard library on *nix distros.

The thing about curses (which may or may not be a problem) is that, by
design, it takes over the whole screen.  If you want to do simpler text
manipulations (showing a dismissible message, showing bold text, etc.)
without interfering with existing text, curses can't do it, at least not
easily.

If your terminal supports it, your current screen contents can be
restored after the application finishes (curses enables the "alternate
screen" and then restores the primary screen on exit).

There don't seem to be a lot of good solutions for doing curses-like
text manipulation without taking over the entire screen.

[...]

-- 
Keith Thompson (The_Other_Keith) keith.s.thompso...@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
-- 
https://mail.python.org/mailman/listinfo/python-list


is input from a pipe?

2025-01-17 Thread km via Python-list
Not a question, but a quick note about a problem that sometimes pops up in 
forums, that is how to detect on Linux if standard input (or any I/O 
stream) is via pipe. My suggestion is to check if the stream is a FIFO, if 
True it is a pipe, otherwise not a pipe 

The solution that sometimes is proposed, that is

if not sys.stdin.isatty()

simply checks if the input is not from a terminal, but it may be from a 
file, not only from a pipe.


import os
import sys
import stat 
def check_if_stream_is_pipe(ifile):
return stat.S_ISFIFO(os.fstat(ifile.fileno()).st_mode)

print(check_if_stream_is_pipe(sys.stdin))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tools to help with text mode (i.e. non-GUI) input

2025-01-17 Thread Alan Gauld via Python-list
On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
> Alan Gauld  writes:
>> On 11/01/2025 14:28, Chris Green via Python-list wrote:
>>> I'm looking for Python packages that can help with text mode input,
>>
>> The standard package for this is curses which comes as part
>> of the standard library on *nix distros.
> 
> The thing about curses (which may or may not be a problem) is that, by
> design, it takes over the whole screen.  If you want to do simpler text
> manipulations (showing a dismissible message, showing bold text, etc.)
> without interfering with existing text, curses can't do it, at least not
> easily.

It's not that difficult to use the terminfo codes directly. But
that won't give access to things like lists of default values, mouse
control etc that the OP wanted. But for simple text characteristics
and moving the cursor around terminfo works ok even if a bit tedious.

Here is "hello world" in bold...

import curses
curses.setupterm()
bold = curses.tigetstr('bold').decode('ascii')
normal = curses.tigetstr('sgr0').decode('ascii')

print(bold, 'Hello world', normal)

Whilst you can position the cursor etc it very quickly
becomes easier to just use full blown curses.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list