Nice copying from Python Interpreter - python

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.

Related

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!

Running codes in different IDEs (Text Editors)

This is a dumb question but I'm new to Python and couldn't figure out why my codes only works with Jupyter notebook and not in other IDEs or Text Editors. For example, I realized I always have to use the "Print" function to get my output in PyCharm and Sublime Text3 while I can just run values without the print function in Jupyter Notebook.
Here's a simple example:
In Jupyter Notebook, I can simply run the codes without the print function
x = 10
y = 20
x+y
if i run this, i still get my output, which is 30.
But if I do the same thing in PyCharm or Sublime Text3, I don't get the output. It just says [Finished in X.Xs] without printing my output and I always have to use the print function to get the output.
x=10
y=20
print(x+y)
I wonder what causes the difference. At first, I thought it was because of the type of software I was using and realized both Pycharm and Jupyter Notebook are IDEs. Do I need to change settings in order to make my codes work in Pycharm or Sublime Text3?
Thank you.
Jupyter Settings is like that. It evaluates your last line of code, if it is 'None' prints nothing but if not None, would just get printed. Only the last uncommented expression or line gets printed nothing intermediate.
Python essentially operates in two modes - script execution mode and interactive mode, otherwise known as REPL (Read, Evaluate, Print, Loop). Jupyter is set up by default to run in REPL mode, where the results of operations are printed to stdout (see this question to change this behavior).
In contrast, PyCharm and Sublime Text execute scripts straight through in non-interactive mode, and only print to stdout when explicitly directed to do so, such as with a call to print(). PyCharm has a REPL mode, as far as I know, and in Sublime you can use the SublimeREPL plugin, available from Package Control.

Python: Executing selected statements

I am new to python programming... Just wanted to know does IDLE has a concept of 'executing selected statements'??
F5 runs the whole program... Is there any way to do this?
No, not now. Since you are at least the second person to ask this, I added the idea to my personal list of possible enhancements. However, while running a selection would not be a problem, producing accurate tracebacks for exception would be. Doing so is an essential part of Python's operation.
Currently, one can disable code that you need to not run by commenting it out (Alt-F3) or by making it a string. One can stop execution after a particular statement by adding 1/0. Or you can copy code to a new editor window.
Do you have a specific use case in mind, or are you just wondering?
Install Spyder, with its dependecies, and you will have wonderful FREE IDE !
You will have another solution, is to use IPython Notebook, where you will be able to use your Internet Browser to run python codes!:

Using ipdb with emacs' gud without explicit breakpoints in code

I'm using python.el
If I choose 'debugger' from the menu, and enter 'python -m pdb myfile.py', gud starts, and in a split frame I see the (Pdb) prompt in one, and my python code in the other with a caret on the first line, indicating that it's ready to go. For example 'n' steps to the next line and the caret moves accordingly.
If instead I enter 'python -m ipdb myfile.py', the frame splits, and one split is labeled gud, but there's no ipdb console evident. In other words, this way of starting ipdb doesn't seem to work. Ipdb works just fine if I manually insert a breakpoint into my python code using ipdb.set_trace(), except that it does not use the gud interface. Is this intentional so that ipdb's stack trace will work nicely?
If so, that's fine, but is there a way to start ipdb from emacs without manually adding a set_trace() command?
The basic problem here is that gud is looking for a (Pdb) prompt and ipdb doesn't prompt this way. There are three ways to fix this: fix ipdb to give a (Pdb) prompt, fix gud not to need to look for (Pdb) or (my favorite) use something else either on the gud side or on the ipdb side.
The problem with fixing up gud is that it is rather old and to my mind a bit creaky using global variables and not making use of Emacs Lisp data structures available other than lists and cons cells. A total rewrite of gud is called realgud, it is currently in MELPA and ELPA. And ipdb is supported.
The last option is to use something else, so let me suggest the Python trepan debugger which is already integrated into realgud (but not gud since I consider that a dead end). Although the backtraces it gives are not exactly like ipdb's, it does colorize them and the source code.
And recent versions of trepan3k backtraces will even show, on demand, you where in the line you are. So if you had say two calls of a function, like fib() it would distinguish which of the calls function was the one in progress.

How to get hard tabs on stdout in Python

I want to paste the output of my Python script into a spreadsheet. For this to work, the output should use hard tabs, and not spaces, to separate fields. How to I convince Python that tabs is what I want?
print("a\tb")
does not do it, and neither does
print("a", "b", sep="\t")
Your help is much appreciated!
Your suggestion of
print("a\tb")
will indeed output a<TAB>b.
Verify this yourself in Python Shell / IDLE, or by writing this a\tb string to file and opening it in any notepad-type program.
The problem likely arises in whatever you're using to view the output. In my current PuTTY setup, tabs are "invisibly" translated to spaces.
EDIT: For xterm, see this "bug" report.

Categories

Resources