PyCharm copy console indicator (`>>>`) when copying text from Python console - intellij-idea

When I copy this text:
and paste it into a text field, I just get:
scipy.special.softmax([1,1])
array([0.5, 0.5])
I would like to get:
>>> scipy.special.softmax([1,1])
array([0.5, 0.5])
This is the behaviour when using the Python interactive console normally outside PyCharm.

this seems to be reported already, please see https://youtrack.jetbrains.com/issue/PY-52621/Unable-to-copy-code-from-the-console-as-plain-text and feel free to vote in order to increase the priority.
I apologize for the inconvenience.

Related

python 3.9 weird behaviour regarding input function

I am using python 3.9 in the newest Pycharm ide. I have a very simple 3 liner:
a=input()
b=input()
print(b)
I input 3, enter, then 4---but somehow the value 4 is not registered, and nothing is printed. Strangely enough, when I add a prompt to the second input function, everything returns normal
a=input()
b=input("test:")
print(b)
The output returns the value b as expected. I have looked everywhere online and could not explain this odd behaviour. What is going on?
Turns out to be a pycharm setting. Had to enable "emulate terminal in output console" under run/edit configurations

This code is directly from a textbook for Cisco Devnet yet presents a syntax error in python 3.8.1 shell. Why? I tried viewing text in notepad++

while True:
string = input('Enter some text to print. \nType "done" to quit>')
if string == 'done':
break
print(string)
print('Done!')
SyntaxError: invalid syntax
image of issue
Image after idz's suggestion
I think your problem is that you wrote:
while true:
instead of
while True:
However, if you are using Python 2 you should be aware that input will attempt to evaluate what you type in as Python. Depending on what your aim is, you may want to use raw_input instead. This is not an issue if your are using Python 3.

Is there a way to get ipython autocompletion when piping a pandas dataframe to a function?

For example, if I have a pipe function:
def process_data(weighting, period, threshold):
# do stuff
Can I get autocompletion on the process data arguments?
There are a lot of arguments to remember and I would like to make sure they get passed in correctly. In ipython, the function can autocomplete to show me the keyword args which is really neat, but I would like it to do this when piping a pandas dataframe too!
I don't see how this would be possible, but then again, I'm truly in awe of ipython and all its greatness. So, is this possible? If not, are there other hacks that people have come up with?
Install the pyreadline library.
$ pip install pyreadline
Update:
It seems like this problem is specific to some versions of ipython. The solution is the following:
Run below command from the terminal:
$ ipython profile create
It will create a default profile at ~/.ipython/profile_default/ipython_config.py
Now edit this ipython_config.py and add the below lines and it will solve the issue.
c = get_config()
c.Completer.use_jedi = False
Reference:
https://github.com/jupyter/notebook/issues/2435
https://ipython.readthedocs.io/en/stable/config/intro.html

line break symbols of pandas dataframe at pycharm interactive console

I tried below before print pandas dataframe at pycharm interactive console.
pd.set_option('display.max_columns', None)
pd.set_option('expand_frame_repr', False)
But as you can see above, I got an unexpected result.
The weird thing is, a week ago, there was horizontal scrollbar with no line break symbols. I failed to find the option related to it at pycharm. Any kind of help will be appreciated. Thank you.
Found. Don't check 'Wrap on typing' at Setting > Editor > Code Style > Default Options.

Persistent Python Command-Line History

I'd like to be able to "up-arrow" to commands that I input in a previous Python interpreter. I have found the readline module which offers functions like: read_history_file, write_history_file, and set_startup_hook. I'm not quite savvy enough to put this into practice though, so could someone please help? My thoughts on the solution are:
(1) Modify .login PYTHONSTARTUP to run a python script.
(2) In that python script file do something like:
def command_history_hook():
import readline
readline.read_history_file('.python_history')
command_history_hook()
(3) Whenever the interpreter exits, write the history to the file. I guess the best way to do this is to define a function in your startup script and exit using that function:
def ex():
import readline
readline.write_history_file('.python_history')
exit()
It's very annoying to have to exit using parentheses, though: ex(). Is there some python sugar that would allow ex (without the parens) to run the ex function?
Is there a better way to cause the history file to write each time? Thanks in advance for all solutions/suggestions.
Also, there are two architectural choices as I can see. One choice is to have a unified command history. The benefit is simplicity (the alternative that follows litters your home directory with a lot of files.) The disadvantage is that interpreters you run in separate terminals will be populated with each other's command histories, and they will overwrite one another's histories. (this is okay for me since I'm usually interested in closing an interpreter and reopening one immediately to reload modules, and in that case that interpreter's commands will have been written to the file.) One possible solution to maintain separate history files per terminal is to write an environment variable for each new terminal you create:
def random_key()
''.join([choice(string.uppercase + string.digits) for i in range(16)])
def command_history_hook():
import readline
key = get_env_variable('command_history_key')
if key:
readline.read_history_file('.python_history_{0}'.format(key))
else:
set_env_variable('command_history_key', random_key())
def ex():
import readline
key = get_env_variable('command_history_key')
if not key:
set_env_variable('command_history_key', random_key())
readline.write_history_file('.python_history_{0}'.format(key))
exit()
By decreasing the random key length from 16 to say 1 you could decrease the number of files littering your directories to 36 at the expense of possible (2.8% chance) of overlap.
I think the suggestions in the Python documentation pretty much cover what you want. Look at the example pystartup file toward the end of section 13.3:
http://docs.python.org/tutorial/interactive.html
or see this page:
http://rc98.net/pystartup
But, for an out of the box interactive shell that provides all this and more, take a look at using IPython:
http://ipython.scipy.org/moin/
Try using IPython as a python shell. It already has everything you ask for. They have packages for most popular distros, so install should be very easy.
Persistent history has been supported out of the box since Python 3.4. See this bug report.
Use PIP to install the pyreadline package:
pip install pyreadline
If all you want is to use interactive history substitution without all the file stuff, all you need to do is import readline:
import readline
And then you can use the up/down keys to navigate past commands. Same for 2 or 3.
This wasn't clear to me from the docs, but maybe I missed it.