python string escape sequence simulation is not working [duplicate] - python

This question already has an answer here:
python IDLE shell appears not to handle some escapes correctly
(1 answer)
Closed 3 years ago.
There are several folks on here looking for backspace answers in Python. None of the questions I have searched have answered this for me, so here goes:
The Simple Goal: be able to print out a status string on one line, where the next status overwrites the first. Similar to a % complete status, where instead of scrolling a long line of 1%\n, 2%, ... etc. we just overwrite the first line with the newest value.
Now the question. When I type this in idle: print("a\bc") I get this as output: ac with what looks like an odd box with a circle between the 'a' and 'c'. The same thing happens when using sys.stdout.write().
Is this an Idle editor setting/issue? Does anyone even know if what I am trying is possible in the Idle Shell?
Thanks for any insight.
PS: Running Python 3.3.2 Idle on Windows 7, 64-bit system.
EDIT: Copying the output in Notepad++ is revealing that Python is printing out a 'backspace' character, and not actually going back a space. Perhaps what I am trying to accomplish is not possible?

Edit:
Apparently the carriage return \r and the backspace \b won't actually work within Idle because it uses a text control that doesn't render return/backspace properly.
You might be able to write some sort of patch for Idle, but it might be more trouble than it's worth (unless you really like Idle)

This doesn't answer your question in a literal fashion, but I think it might be useful to point out that generally interfaces like the one where you are describing (e.g., where one part of the screen is continuously updated, without newlines), it just generally implemented using a library like ncurses.
Python has a curses library built-in (http://docs.python.org/3.3/library/curses.html), which can more or less achieve your end goal.

Related

Is it possible to make pycharm's run display section display the same as actually running the program would?

Disclaimer: Sorry if the question is stupid or repeated, I've tried to find similar ones that answer what I need to know but I couldn't. I've started to learn programming about 1 month ago and this is my second time on this website. Feel free to point out any errors or better ways to formulate my questions on stackoverflow, I'll be grateful.
Context:
I was trying to find out a way to print a string - in this case ' º ' - after my user's input - an angle -, on the same line.
I need an alternative way to do this, or help with the one I'm using.
What I got from my research is that, apparently, using the command os.system(cls) should erase the previous line, and putting \033[A before the string should move the cursor up one line. So using these two together should erase the previous line and then overwrite it.
Here's my try:
from os import system
cls = lambda: system('cls')
angle = float(input(f'Insert an angle:'))
cls()
print(f'\033[AInsert an angle:{angle}º')
Desired result on run:
Insert an angle: *60*º # being 60 the user's input
Actual result on pycharm:
Insert an angle:60
Insert an angle:60.0º # for some reason, you can't see it when paste it here, but there's a symbol of a crossed rectangle on the beginning of this line on Pycharm's run
How it looks on pycharm's run terminal
As you can see, the line isn't getting overwritten, only repeated.
What is weird is that when I run this program with Python 3.8 instead of Pycharm, it works as intended, but, on Pycharm, the line isn't overwritten. Instead, Pycharm just prints a crossed rectangle symbol.
Why does it work when executing the file with Python 3.8, but not when pressing "run" on pycharm?
Is there a way to avoid it?
Are there better alternatives to printing a string on the same line as an input?
In cases where I need special printing (ANSII escape codes, backspacing...), I use the actual Terminal, not the Python Console.
For whatever reason, interactive consoles, regardless of IDE, seem to have issues with handing specialties like that. With the normal Terminal, it works as expected:
I have never found a way of having the interactive console handle cases like this.

ANSI escape codes not working on IDLE... (python)

I am making a program involving ANSI escape codes, and they were all working fine on replit until I switched back to IDLE (3.9 on both). But it doesn't work:
it should have looked like this:
I have seen several posts before that complain that the IDLE doesn't support these escape sequences because it isn't a terminal, so I tried to do it directly from the cmd but the beastly symbol still appeared, this time as a boxed question mark:
I know that it won't work straight from the IDLE, so I wonder if you can import a software like mintty into python?
Powershell works though...
P.S. please don't tell me to import colorama or something! I really want this to be the way. I also don't have immediate access to iPython (even though I would like to) so it's not really an option for me... unless I have to :D
EDIT: the code I put across the python programs:
import sys, os
os.system("")
CSI = f"{chr(0x1B)}["
print(f"""{CSI}3m{CSI}1m{CSI}4m{CSI}31m
look at this""")
sys.stdout.flush()
# I put the sys.stdout.flush() and os.system("") to try and fix the problem...
The IDLE shell is not a terminal emulator and not intended to be production environment. The decision so far is that is should show program developers what their program's output, without interpretation. This may change in the future but no final decision yet.
If you are on Windows, I believe its console can be put into ANSI mode, but Python does not do that for you. I don't know if program code can do so.
As near as I can tell, there is not wrapper program that turns mintty into an importable python module. It would not make much sense. Rather, you would want to open mintty or a mintty-based terminal-emulator, such as git bash, and open python within that terminal instead of CommandPrompt.
ANSI code is a broad term. You have to specify which code page you are using. For example, my Windows is in Chinese Simplified. Therefore if I want to escape from UTF-8 default in Python, I would put # coding : cp936 on the first or second line of a script. Then it can read and write text files with the simplified Chinese coding.
Second Question:
Could I make a red/green/etc. font for every character and put it as print('...', file=...)? It should be possible because colored emojis exist.
It should work, but I would like to know how I could (if it's possible) automate this with some program that makes a file containing those characters and displays them with the previous print statement.
Cheers!

Criteria (NOT TOOLS) to check what version of python a piece of code is written for? [duplicate]

This question already has answers here:
Tool to determine what lowest version of Python required?
(3 answers)
Closed 5 years ago.
One finds many small programs or sample code on the Internet, which do no not necessarily specify in which context they were written (shebang), and do not necessarily use obvious things as print statements.
They may crash with some or some other version of python, but this may not be due to fundamental incompatibilities but just due to missing libraries which might be hard to find.
There are some tools as mentioned in this question but the question here is: "What would be good criteria to decide if a code is compatible with either version of python ?"
There are at least :
The presence of the shebang (But it is often not present)
Print statements (without parenthesis) are from python2 or before (But you do not always have them, especially in GUI programs)
Integer division (//) is from python 3 and later (But not all programs compute integer divisions)
What else ?
#Mureinik, #JJJ, #Bear Brown, #Tempux, please remove duplicate flag.
Ideally a python script will include a shebang on the first line something like: #!/usr/bin python and/or comments telling you the minimum, (and possibly maximum), version that it will work with.
Other clues:
print Something # Python 2 Only
print(Something) # Python 3 Mostly
from __future__ import print_function # As first active code
print(Something) # Now works for both
Of course and well written code will either be compatible with many versions or specifically check for the versions that it requires.
If libraries are missing then the error messages are really clear but for the most part python code tends to "just run"™ so the real solution is to try the code with the versions of python that you have to hand.
You can refer from official documentation : Python Docs
I am assuming that you have an IDE for Python 3.x where you can try that piece of code.
As stated by Steve, You can differentiate by using the print function of python(x).
print "hello world"
So, if there is print statement(like above) in the code you will get:
SyntaxError: Missing parentheses in call to 'print'.
Thus, that piece of code was for 2.x python .

In python's IDLE, when I enter a new line it doesn't tab back like normal

Relevant background information
I'm not asking for any technical coding advice but how to use an IDE as
i've seen to encounter some sort of bug or of turned something on.
I'm coding Object-Oriented python in it's basic editor: Idle.
Problem
When I enter a new line, it doesn't tab back where relevant anymore
but where I was in the previous line, but in the next one.
Sometimes it goes even farther.
What I've tried to do
I looked everywhere on IDLE if it was some sort of option I've accidentally
turned on. I've restarted IDLE completely multiple times.
What I'm asking for
Does any python programming know what this is and how to turn it off.

how to save python session input and output [duplicate]

This question already has answers here:
How to save a Python session, including input and output, as a text?
(4 answers)
Closed 1 year ago.
All of the ways which discussed this question save the history of your commands in a file or you have to use an IDE ,I am using vim with python-mode (no mouse using) what I would like to do is to save my session as code I wrote and the python output ,So I dont have to use paper and pen to write my input and python output all what I have to do is to print out my session , I tried the code (.pystartup)
and it only save my input and I tried to redirect the output to a file and it only save the python output , is there a way to have both in one file ready to be printed out .
I really cherish vim, it'S a fantastic piece of work. Nevertheless, your requirements are easier with other tools.
Probably the best choice, in my oppinion, would be to use the ipython notebook. It offers really rich features, including graphics with mpl and much more, and for me is the perfect tool for "reproducible experiments". The full state of a notebook can be saved to disk, reloaded, exported, printed etc.
You should really give it a try.
The old "I use $EDITOR instead of $IDE because I don't want to use the mouse" bullshit… IDEs have shortcuts for everything and they all allow you to customize them to your liking. Learn them all and forget about your mouse.
Anyway, what you want is neither an editor nor an IDE; you want a REPL like bpython or ipython (possibly with its notebook feature mentioned above). Both tools allow you to save and restore your sessions and are far better at "getting" your python code than Vim.

Categories

Resources