📜 ⬆️ ⬇️

Console Python Tricks: Team History + Autocompletion

If you are developing programs on Python, then I am sure that you are very familiar with working with him interactively. This is the easiest and most convenient way to test the idea that has come to mind, run the library function, step through an algorithm, and so on. without creating unnecessary junk files. In general, the Python console is a very powerful and convenient thing. I am constantly asking for a comparison with a unix shell in a language. If it were not for two fly in the ointment: this is a command that you have to type again each time, instead of just pressing the up arrow and correcting the command, as well as writing a lot of excess, which only adds typos and the need to redial. In general, I want as in the bash: history + autocompletion.
I am surprised why this is not done by default. By the way, it may very well be that this is already implemented in your system, and this is completely irrelevant to you - then you can absolutely safely skip this article. Perhaps it will be useful to someone else. On Macos, Freebsd, Debian, and Fedora I came across this was not.



To do this, create somewhere in the depths of the file system file initialize.py with the following content:
')
import sys, os, readline

histfile = os.path.join(os.environ[ "HOME" ], ".pyhist" )
try :
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile

try :
import readline
except ImportError:
print "Module readline not available."
else :
import rlcompleter
readline.parse_and_bind( "tab: complete" )


* This source code was highlighted with Source Code Highlighter .


I prefer to throw such files into ~ / bin /.
And then at each launch, first run this script. Of course, you don’t have to do this with your hands - if you use bash as a shell, just add a line to your .bashrc:
export PYTHONSTARTUP = $ HOME / bin / initialize.py


Now work with the console of a python becomes not just high, but continuous high. Call the shell:

$ python
Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
Type "help" , "copyright" , "credits" or "license" for more information.
>>> from UserDict import UserDict



By itself, the history of the commands is initially empty - so the up arrow should not be pressed.
Create some object - for example, I took UserDict. After it is imported, start typing the command and press <Tab>
>>> u=User # <Tab><Tab>
UserDict UserWarning
>>> u=UserDict()

# :
>>> u. # <tab><tab>
u.__class__ u.__delitem__ u.__init__ u.__repr__ u.copy u. get u.iteritems u.keys u.setdefault
u.__cmp__ u.__doc__ u.__len__ u.__setitem__ u.data u.has_key u.iterkeys u.pop u.update
u.__contains__ u.__getitem__ u.__module__ u.clear u.fromkeys u.items u.itervalues u.popitem u.values
>>> u.


* This source code was highlighted with Source Code Highlighter .


After exiting the interpreter, the entire command history will be written to the ~ / .pyhist file, and by running the console again, you can easily repeat the actions you did last time.

It may well be that the first time you do not earn. Most likely the fact is that the python-readline module is not installed - this is how it was with me. Debian is put elementary:
apt-get install python-readline

For other distributions, see the instructions for your distro.

The way to be honestly peeped on the Internet. I hope you will be useful.

Source: https://habr.com/ru/post/46101/


All Articles