[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Eryk Sun
On 12/20/20, Cameron Simpson  wrote:
> On 20Dec2020 15:48, Christopher Barker  wrote:
>
>>That would be great, though I just looked at the 3.9 docs and saw:
>>"The Windows version of Python doesn’t include the curses module."
>
> Yeah, Windows.

A C or ctypes implementation is required in Windows. Virtual terminal
mode is supported in Windows 10, for which it *might* be enabled. This
can be queried via GetConsoleMode. If virtual terminal mode isn't
enabled, then clearing the screen has to be implemented by scrolling
the console screen buffer. The screen buffer size, visible rectangle,
and current character attributes can be queried via
GetConsoleScreenBufferInfo. The window can be scrolled via
ScrollConsoleScreenBuffer. The entire buffer can be scrolled out, like
the CMD shell's CLS command, or one can just scroll the buffer enough
to clear the visible window. The cursor can be set to the home
position via SetConsoleCursorPosition.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MRU5G2IB4CBSK5TAWKHS7EXIV6ECBEKO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Felipe Rodrigues
well, my two cents here is that I do clear my screen - a lot. In the same
use case as
Guido's mentioned - exploratory programming. But when I do it, I do in a
manner
that was discussed in the original thread - by using CTRL+L, which is of
course
terminal-specific but clears everything both in Python's REPL and ipython.

As a comparison, I also code almost daily in Elixir, which has a REPL as
well,
but for some weird reason, CTRL+L does not work there.


[image: --]
Felipe V. Rodrigues
[image: https://]felipevr.com



On Sun, Dec 20, 2020 at 8:48 PM Christopher Barker 
wrote:

> On Sun, Dec 20, 2020 at 1:23 PM Cameron Simpson  wrote:
>
>> On 20Dec2020 08:51, Christopher Barker  wrote:
>> >On Sat, Dec 19, 2020 at 8:53 PM Guido van Rossum 
>> wrote:
>> >> at sounds like a very special status. Why not os.clear()?
>>
>> My anger at programmes which gratuitously clear the screen is large.
>>
>
> There are a LOT of bad things one can do with Python, I don't think we
> need to make something difficult simply because it can be abused.
>
> One problem is: what does it mean? On a terminal, easy. But in a GUI?
>> Clear the screen (possibly forbidden)? A window? The "main" window?
>> Etc.
>>
>
> It would be meaningless outside of a terminal -- call it `clear_term` if
> you want.
> Even print() doesn't  have a consistent useful meaning in a GUI program.
>
> Anyway, I think it should be in curses (or be loaded via curses on
>> demand),
>
>
> That would be great, though I just looked at the 3.9 docs and saw:
> "The Windows version of Python doesn’t include the curses module."
>
> So we're pretty much back to square one.
>
> I think the idea here is that we want a simple way, out of the box that
> people can use to clear the terminal, that will work on most commonly
> configured systems out of the box, and can be overridden (monkey patched)
> to work in IDEs (e.g. Idle), custom terminal emulators (e.g. iPython
> Qtconsole), etc. We don't want to require a full curses implementation.
>
> and just have a clear_screen function thus:
>>
>> def clear_screen():
>> setupterm()
>> print(ti_getstr('cl'), end='', flush=True)
>>
>
> I just tried that on my mac and tigetstr('cl') simply returns None with no
> effect.
>
> I've only looked for a coupe minutes, but it seems you need to set up
> curses in order ot use it, so maybe not teh best way to go for a single
> action?
>
> The tutorial seems to indicate there is a clear method available, so I did
> try that, and it does nothing on my Mac with the default terminal app:
>
> def clear_screen():
> from curses import wrapper
>
> def clr(stdscr):
> # Clear screen
> stdscr.clear()
> wrapper(clr)
> clear_screen()
>
> I may have done something wrong, so anyone please correct -- but it's sure
> looking to me like curses is not a solution to this problem.
>
> here is that this is trivial function
>
>
> clearly not trivial to write for someone new to curses :-)
>
>
>> On terminals, see above. In a GUI, who knows? And how does one tell the
>> programme which it is talking to?
>>
>
> That would be up to the GUI toolkit to provide if it includes a terminal
> emulator of some sort.
>
> >is it so bad to use a subprocess?
>>
>> Yes. It is _really slow_,
>
>
> could it possible be slow enough to matter? not the kind of thing that's
> in a tight loop.
>
> depends on external reaources which might not
>> be there,
>
>
> oh, like the curses lib ;-)
>
>
>> and subprocess brings other burdens too.  Python comes with
>> curses and that knows directly how to do this.
>>
>
> not that I could figure out, and apparently not on Windows.
>
> Anyway, the idea (I have) is that this would be somewhere like
> os.clear_term(), and the implementation would be platform specific -- if on
> *nix systems, it used curses, great!
>
> The other thought is that this has been in iPython, with the simple
> os.system() implementation, for years, so it eather works well enough, or
> no one ever uses it :-)
>
> I looked at iPython issues, and apparently there is (or was) an issue with
> Windows if the current working dir is a network mount, but that's all I
> found. Of course, there are 1400 open issues, so I may have missed one :-)
>
> Anyway, not my area of expertise, I just think it's a good idea if someone
> with the appropriate expertise wants to step up and write it.
>
> -CHB
>
> --
> Christopher Barker, PhD
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/IL2WSB5P4OTQURVBOHYK3QJ3JZOMYZYF/
> Code of Conduct: 

[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Cameron Simpson
On 20Dec2020 15:48, Christopher Barker  wrote:
>On Sun, Dec 20, 2020 at 1:23 PM Cameron Simpson  wrote:
>> My anger at programmes which gratuitously clear the screen is large.
>
>There are a LOT of bad things one can do with Python, I don't think we need
>to make something difficult simply because it can be abused.

True.

>One problem is: what does it mean? On a terminal, easy. But in a GUI?
>> Clear the screen (possibly forbidden)? A window? The "main" window?
>> Etc.
>
>It would be meaningless outside of a terminal -- call it `clear_term` if
>you want.

+1

>> Anyway, I think it should be in curses (or be loaded via curses on
>> demand),
>
>That would be great, though I just looked at the 3.9 docs and saw:
>"The Windows version of Python doesn’t include the curses module."

Yeah, Windows.

>So we're pretty much back to square one.

No, for Windows we shell out to its command line unless a Windows person 
knows what to do. We could hardwire the ANSI clear screen sequence as a 
fallback when there's no curses module, that's easy with a try/import.

>I think the idea here is that we want a simple way, out of the box that
>people can use to clear the terminal, that will work on most commonly
>configured systems out of the box, and can be overridden (monkey patched)
>to work in IDEs (e.g. Idle), custom terminal emulators (e.g. iPython
>Qtconsole), etc. We don't want to require a full curses implementation.

Ok. I still think on UNIX we want curses as the preferred choice since 
to accomodates all terminal types. But a fallback assuming ANSI seems 
sensible to me, unless there's a Windows counter example.

Untested:

cls_bs = None

def clear_screen():
if cls_bs is None:
try:
import curses
except ImportError:
cls_bs=curses.tigetstr('clear')
print(cls_bs.encode(), end='', flush=True)

>I just tried that on my mac and tigetstr('cl') simply returns None with 
>no effect.

Yeah. 'clear' works though.

>The tutorial seems to indicate there is a clear method available, so I 
>did
>try that, and it does nothing on my Mac with the default terminal app:
>
>def clear_screen():
>from curses import wrapper
>
>def clr(stdscr):
># Clear screen
>stdscr.clear()
>wrapper(clr)
>clear_screen()

Nonono.

That requires a full curses init, "full screen curses mode programme" 
etc.  We just want the terminal escape sequence. setupterm() inits the 
terminal db access, then off we go. As above.

>>is it so bad to use a subprocess?
>>
>> Yes. It is _really slow_,
>
>could it possible be slow enough to matter? not the kind of thing that's in
>a tight loop.

In your imagined use case. Guarenteed one day this will be in a tight 
loop. In a simulation or something.

>depends on external reaources which might not
>> be there,
>
>oh, like the curses lib ;-)

Hahaha. Batteries supplied.

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CTDKM3PHPAWAHXHBVFR3CCMBUNQAAZVY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Christopher Barker
On Sun, Dec 20, 2020 at 1:23 PM Cameron Simpson  wrote:

> On 20Dec2020 08:51, Christopher Barker  wrote:
> >On Sat, Dec 19, 2020 at 8:53 PM Guido van Rossum 
> wrote:
> >> at sounds like a very special status. Why not os.clear()?
>
> My anger at programmes which gratuitously clear the screen is large.
>

There are a LOT of bad things one can do with Python, I don't think we need
to make something difficult simply because it can be abused.

One problem is: what does it mean? On a terminal, easy. But in a GUI?
> Clear the screen (possibly forbidden)? A window? The "main" window?
> Etc.
>

It would be meaningless outside of a terminal -- call it `clear_term` if
you want.
Even print() doesn't  have a consistent useful meaning in a GUI program.

Anyway, I think it should be in curses (or be loaded via curses on
> demand),


That would be great, though I just looked at the 3.9 docs and saw:
"The Windows version of Python doesn’t include the curses module."

So we're pretty much back to square one.

I think the idea here is that we want a simple way, out of the box that
people can use to clear the terminal, that will work on most commonly
configured systems out of the box, and can be overridden (monkey patched)
to work in IDEs (e.g. Idle), custom terminal emulators (e.g. iPython
Qtconsole), etc. We don't want to require a full curses implementation.

and just have a clear_screen function thus:
>
> def clear_screen():
> setupterm()
> print(ti_getstr('cl'), end='', flush=True)
>

I just tried that on my mac and tigetstr('cl') simply returns None with no
effect.

I've only looked for a coupe minutes, but it seems you need to set up
curses in order ot use it, so maybe not teh best way to go for a single
action?

The tutorial seems to indicate there is a clear method available, so I did
try that, and it does nothing on my Mac with the default terminal app:

def clear_screen():
from curses import wrapper

def clr(stdscr):
# Clear screen
stdscr.clear()
wrapper(clr)
clear_screen()

I may have done something wrong, so anyone please correct -- but it's sure
looking to me like curses is not a solution to this problem.

here is that this is trivial function


clearly not trivial to write for someone new to curses :-)


> On terminals, see above. In a GUI, who knows? And how does one tell the
> programme which it is talking to?
>

That would be up to the GUI toolkit to provide if it includes a terminal
emulator of some sort.

>is it so bad to use a subprocess?
>
> Yes. It is _really slow_,


could it possible be slow enough to matter? not the kind of thing that's in
a tight loop.

depends on external reaources which might not
> be there,


oh, like the curses lib ;-)


> and subprocess brings other burdens too.  Python comes with
> curses and that knows directly how to do this.
>

not that I could figure out, and apparently not on Windows.

Anyway, the idea (I have) is that this would be somewhere like
os.clear_term(), and the implementation would be platform specific -- if on
*nix systems, it used curses, great!

The other thought is that this has been in iPython, with the simple
os.system() implementation, for years, so it eather works well enough, or
no one ever uses it :-)

I looked at iPython issues, and apparently there is (or was) an issue with
Windows if the current working dir is a network mount, but that's all I
found. Of course, there are 1400 open issues, so I may have missed one :-)

Anyway, not my area of expertise, I just think it's a good idea if someone
with the appropriate expertise wants to step up and write it.

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IL2WSB5P4OTQURVBOHYK3QJ3JZOMYZYF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Cameron Simpson
On 20Dec2020 08:51, Christopher Barker  wrote:
>On Sat, Dec 19, 2020 at 8:53 PM Guido van Rossum  wrote:
>> at sounds like a very special status. Why not os.clear()?

My anger at programmes which gratuitously clear the screen is large.  
(Years of anger watching IBM-derived PCs boot, particularly, when I want 
to see some diagnostic.)

One problem is: what does it mean? On a terminal, easy. But in a GUI? 
Clear the screen (possibly forbidden)? A window? The "main" window?  
Etc.

Anyway, I think it should be in curses (or be loaded via curses on 
demand), and just have a clear_screen function thus:

def clear_screen():
setupterm()
print(ti_getstr('cl'), end='', flush=True)

I can already see many ways to bikeshed on that, alas. But the point 
here is that this is trivial function (albeit often wanted, however 
misguides I might personally consider that want to often be).

>I also have no idea about implementation, but I"m sure there's a few 
>folks
>with platform expertise that could make this work on many systems out of
>the box.

On terminals, see above. In a GUI, who knows? And how does one tell the 
programme which it is talking to?

>is it so bad to use a subprocess?

Yes. It is _really slow_, depends on external reaources which might not 
be there, and subprocess brings other burdens too.  Python comes with 
curses and that knows directly how to do this.

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZSF6GY7QFGPLOXRBIATRXDRCHSCQUCFJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Typed Python execution mode

2020-12-20 Thread redradist
Good design should follow open-close principle from SOLID ...
You provide template (standard way to do something) for community and if 
package can work through this interface that is required all goes good ...

In such way community would have the standard way to do something instead of 
lots of different way that even not compatible with each other

Also this design is exactly according Zen Python:
"There should be one-- and preferably only one --obvious way to do it."
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EMLYJWF4UGQK6VHTYTEIXILOZ2QIJDST/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Typed Python execution mode

2020-12-20 Thread redradist
Yes, it is not bad, it is open-close principle from SOLID for good design 
system ...
You provide template (standard way to do something) for community and if 
package can work through this interface that is required all goes good ...

In such way community would have the standard way to do something instead of 
lots of different way that even not compatible with each other

Also this design is exactly according Zen Python:
"There should be one-- and preferably only one --obvious way to do it."
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/U2DPBD7PVZKINBDGE6MTGVBENLX6URAW/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: built in to clear terminal

2020-12-20 Thread Christopher Barker
On Sat, Dec 19, 2020 at 8:53 PM Guido van Rossum  wrote:

> at sounds like a very special status. Why not os.clear()?
>>
>
> I do agree with the motivation for doing this. (I've started doing more
> exploratory programming and I've felt this need clearly a few times.)
>

Frankly, you can't do hardly any scripting without importing at least os or
sys anyway. And "scripting" environments could pre-import whatever they
want.

I can't comment on the exact semantics or implementation (though I'd prefer
> it if it didn't have to run a subprocess).
>
> IDLE should probably monkey-patch this so it does something reasonable in
> its shell window.
>

I'm not sure if there's anything to do to make that easier, but it would be
good if it were easy for any terminal emulator in IDEs, etc to have it to
hook into.

I also have no idea about implementation, but I"m sure there's a few folks
with platform expertise that could make this work on many systems out of
the box.

This SO post: (
https://stackoverflow.com/questions/55771796/how-to-clear-the-screen-of-interactive-ipython-console-from-python-code)
both provided one more data point about folks wanting this, and a pointer
to the fact that iPython already includes something -- though I just found
that code, and it's no fancier than what my student came up with:

if os.name == 'posix':
def _term_clear():
os.system('clear')
elif sys.platform == 'win32':
def _term_clear():
os.system('cls')
else:
def _term_clear():
pass

(https://github.com/ipython/ipython/blob/master/IPython/utils/terminal.py)

is it so bad to use a subprocess?

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ADTVNPCCL7IVS6SQCEV4HDYBH73BOYCU/
Code of Conduct: http://python.org/psf/codeofconduct/