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
Related
I am writing a python script that I want to use in a unix pipeline. My goal is to write to the screen using curses (which should only be seen by the person running the command, not the pipe), and then write the "return value" to stdout at the end so it can continue down the pipeline, something along the lines of ./myscript.py | consumer_script
This was failing in mysterious ways until I found This. The suggested solution was to use newterm instead of init_scr.
My problem is that I am using python, and from what I could find in the documentation, newterm doesnt exist. All I was able to find was a single reference to newterm, and it didn't come with a link.
Could someone please either point me towards the python newterm, or suggest another way of working with pipes and curses.
I think you're making this more complicated than it needs to be... the simple answer is to write the curses stream to another handle than stdout. If it works for you, stderr is the obvious choice. In short, anything that gets written to stdout goes into the pipeline, and if you don't want it there, you need a different handle.
Check out this thread for ways to write to stderr in python:
How to print to stderr in 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!
I am playing with some programming challenges that will check the submission by:
python my_submission < in.txt > out.txt
When I try and make my submission, I want to read some cases/numbers/whatever from in.txt to see what is happening. Currently I am doing that by:
import sys
file = open('in.txt')
sys.stdin = file
for line in sys.stdin:
case1 = line.split()
some_function(case1)
So when I run my python program (hit cmd+B) in Sublime text, I can see whether I manage to read the input correctly, process one test case correctly, etc.... Then I just commend out the 2nd and 3rd line when my program should be submitted to the submission judge.
I was just wondering: is this the "preffered workflow" for dealing with this? Do pro programmers write some kind of unit test template function to do this?
The preferred workflow is to let the shell doing the redirection so you don't have to change the program code all the time.
But your IDE (sublime text) doesn't allow you to specify such arguments, so it limits your options.
Solutions/workarounds:
Start the program from a shell. Which means you need to switch between the terminal window and sublime all the time.
Write a second program which runs the first and which sets up the input redirection. This way, you just need to switch tabs in sublime.
Instead of reading from stdin directly, use the fileinput module. See How do you read from stdin in Python? This will allow you to write proper unit tests for your code. You can then use the Python Unittest Helper plugin for Sublime.
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 tests that i ran which can take up to 15m at a time. during these 15m, a log file is periodically written to. however, most of the content is useless.
in response to this i have a python script that parses out the useless text and displays the relevant data.
what i'm trying to achieve is similar to what tail -f log_file, constantly updating the terminal with the newest additions to a file. i was thinking that if a python script ran as a process, it could parse the log file whenever the tests write to it, then the python script can go to sleep until interrupted again once the log file is written to.
any ideas how one can achieve this?
i already have a script that does the parsing, i just don't know how to make it do it continually and efficiently.
You could just have the script filter standard input, and pipe tail -f through it. When you're waiting on stdin, your script will sleep, so it's plenty efficient.
Eg.
python long_running_script.py && tail -f log_file | python filter_logs.py
Your script can be something like
while true:
line = sys.stdin.readline()
if filter_line(line): print line
looks like you need something like "pytailer":
http://code.google.com/p/pytailer/
While I never used it myself, last example looks like what you want.
any ideas how one can achieve this?
This should be pretty easy to do. Most of what you want is already part of your OS.
python test.py | python log_parser.py
Be sure your tests write their log to stdout instead of some other file. This is often easy to do with small changes to the logging configuration.
Having implemented almost this exact tool, I had great success using the inotify capability in twisted