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!
Related
When I execute a python program, the results starts to appear quickly and I can't read it all. It just flushes over my screen.
When the execution ends, I can no longer see the first displays, because the terminal display space is limited.
How save the output, so I can read all of it?
You have a few options here.
Add a breakpoint and learn how to use the debugger. Once you add this command (import pdb;pdb.set_trace() # this will take some learning so look up what pdb is online. actually, i prefer 'ipdb' instead.), the code will stop at that specific point when you execute it.
Save it to a file (python file.py > filename.txt) and then read it afterwards. Bonus: Before you ask yourself, where are my outputs? https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file
(More advanced) Your code is spitting out too much garbage output. You can remove some of the code or use python logging filters.
May be platform dependant.
On Linux you can also pipe your program output into your favorite pager (less for example) if you don't want to write it to a file.
python file.py | less
When reading a book or just coding on terminal/IDLE it's common to make typo, forgot brace or comma etc. After I got error and all what I wrote before is lost.
Then I have to write down code again..
Is there any way/option to return back all what write before and just edit mistake and continue to code?
In Idle (at least my version, Python 2.7.10 on windows), you can simply copy paste your code. In the python interpreter, you can't afaik, however you can use the up/down arrow keys to recall lines you previously "submitted" (i.e. typed and pressed enter).
If I understood correctly, IDLE is a GUI (graphical user interface - a visual representation of a program rather just through text) made to have a bit more features for programming in Python. You can use IDLE interactively, like in Terminal (a.k.a command line), or use it to write your script rather than in a separate text editor. Then once you save your script/program you can do neat things like run it directly from IDLE. There's nothing more special about the Terminal, you just have to do some more work.
Furthermore, all the code you have written on your GUI is on the cache memory which is used in system to store information recently accessed by a processor. So, I suggest you write again your code you can't recover them without saving.
To avoid these kind of problems use Git!
Git is a version control system that is used for software development and other version control tasks.
IDLE's Shell window is statement rather that line oriented. One can edit any line of a statement before submitting it for execution. After executing, one may recall any statement by either a) placing the cursor anywhere on the statement and hitting Enter, or b) using the history-next and history-prev actions. On Windows, these are bound, by default, to Alt-p and Alt-p. To check on your installation, Select Options => IDLE preferences on the menu. In the dialog, select the Keys tab. Under Custom Key Bindings, find the 'histor-xyz' actions in the alphabetical list.
For short, one-off scripts, I have a scratch file called tem.py. Since I use it often, it is usually accessible via File => Recent files.
I am in the works of creating a python program similar to this
. Anyway what i want to do is have users be able to modify there own programs but i need help understanding how this works. I have looked through the source code and am confused where this happens even if someone could just point me towards that that would be very helpful. I know that the programs will not be sandboxed but that is not something im worried about at the moment. If you could point me in any direction that would be great! Thank you!
The "robot programs" are just stored as plain text files.
There's a general-purpose text editor in editor.py. When you open a robot in a given view, e.g., the Qt4 view in qt4view.py, it just instantiates a text editor and hands it the robot's file. Again, the fact that the robot's file is a Python script doesn't matter; it just edits it as a text file.
The battle code, meanwhile, opens the same robot files as Python code that the text editor opens as text files. You can see this code in game.py: It just uses the subprocess module to run Python, passing the robot file as an argument.
My other answer deals with what you actually asked. But I don't think it's what you really wanted to know.
You just want to know how to run some Python script, that you've got a pathname for, in a separate Python interpreter, right?
While it's possible to figure that out from the pybotwar code, there's a whole lot of extra stuff that will get in the way of understanding it—the conf.py file, the configurable extra flags, etc.
But the answer is simple: Use the subprocess module, just as you would for running any program. In this case, the Python interpreter is the executable (usually you want sys.executable, the same Python interpreter you're using), and the script you want to run as an argument. For example:
script_output = subprocess.check_output([sys.executable, script_path])
The subprocess documentation explains all the different options very nicely.
I have a simple Python script that uses ANSI escape sequences to have colored output on the terminal.
And this works great, but when the output is being used elsewhere (for example in VIM) all the ANSI sequences show up and it makes it really unreadable.
For example, to display RED I do something like:
^[[91m Errors:
-------^[[0m
Which is perfectly readable in the terminal.
I could add a flag to my Python command line tool to avoid displaying these characters when I need to deal with output, but I was wondering if there is a way of having colored output without messing up output.
The solution would have to use the Python Stdlib or would have to avoid installing a third party library.
However, I am perfectly OK if the approach doesn't work on Windows :)
You can use os.isatty with the stdout file descriptor, which can be retrieved by calling fileno on sys.stdout.
Edit: It looks like files also have an isatty method; therefore, you can avoid using the os module and just using sys.stdout.isatty().
Look at this answer it covers exactly what you are asking for
How do I detect whether sys.stdout is attached to terminal or not?
When I am working with a Python Interpreter, I always find it a pain to try and copy code from it because it inserts all of these >>> and ...
Is there a Python interpreter that will let me copy code, without having to deal with this? Or alternatively, is there a way to clean the output.
Additionally, sometimes I would like to paste code in, but the code is indented. Is there any console that can automatically indent it instead of throwing an error?
Related
Why can I not paste the output of Pythons REPL without manual-editing?
IPython lets you show, save and edit your command history, for example to show the first three commands of your session without line numbers you'd type %hist -n 1 4.
WingIDE from Wingware will let you evaluate any chunk of code in a separate interpreter window.
IPython will let you paste Python code with leading indents without giving you an IndentationError. You can also change your prompts to remove >>> and ... if you wish.
I have a vim macro to "paste while cleaning interpreter prompts and sample output [[==stuff NOT preceded by prompts" and I'll be happy to share it if vim is what you're using. Any editor or IDE worth that name will of course be similarly easy to program for such purposes!
Decent text editors such as Notepad++ can make global search and replace operations that can replace >>> with nothing.