Python.el and Python-mode: Documentation & Features - python

I have been reading about the different Python packages available for Emacs for some time. However, I have never been able to find the official documentation of either package.
From what I understand, python-mode seems is hosted at launchpad, but the repository does not have a wiki nor a document explaining the features, etc. On the other hand, python.el comes with recent versions of Emacs, but I can't find a documentation walkthrough its features anywhere.
Also, do both of them support step-by-step debugging through ipdb ipython? Where can I find more information on this matter?

Comments following ;;; Commentary is a good starting point, as #lunaryorn suggested. If you need setup read this part first.
But I think just start using it when your setup is done (for python.el, there is nothing to do if you use Emacs 24.3) is the best way to go through the features.
This is because Emacs is self-documenting editor. You can read all document about Emacs in Emacs. In Python buffer, use <f1> b (or C-h instead of <f1>) to see a list of command you can use (this is the list of features). Each command is linked to the docstring of the command. You can also open a menu using "Ctrl + Right Click" to see a list of command you can use in the buffer. The document of python-mode function, which can be opened by <f1> f python-mode RET, may be another good starting point. If you want to see documentation of some keybind, type <f1> k KEY-BIND. There are more useful help commands. See <f1> <f1> for more info. These techniques are not specific to python-mode, so you can use them for python.el and python-mode.el.
Re ipdb: python.el (and I guess python-mode.el also) can do it. There is code snippet in ;;; Commentary that you can use in Emacs configuration.

https://blueprints.launchpad.net/python-mode/+documentation
This offers some basic FAQs meanwhile.
python-mode.el is designed to support ipdb and all other debuggers right from the spot. Configuration as explained at top of python.el isn't needed, as these stuff is implemented. Most commands are self-explaining. Every command has a documentation string, which users will find useful for some features like py-expression. Also the return values are reported there.
To get an overview which commands exist, the menu is a good starting point.
http://www.gnu.org/software/emacs/manual/html_node/emacs/Menu-Bar.html
python-mode.el lists commands alongside with its explanations in directory "doc".
Read the comments as suggested. Afterwards maybe employ outline-mode or hs-minor-mode to take a tour of the symbols.
Finally: M-x py- TAB, resp. M-x python- TAB lists the implemented commands. Once the name is known, call it's docu Ctrl-h f
Both modes should work with IPython/ipdb.
Seem several howto's at the net.
Maybe consult this
http://gremu.net/blog/2010/using-ipythons-debugger-pdb-replacement/
Please file a bug-report if you can't make it work.
As for with python-mode.el:
https://bugs.launchpad.net/python-mode
As for python.el : M-x report-emacs-bug RET

Related

VI auto completion Nonesense

I read that CTRL+P is the auto completion short cut in VI, but the recommendation given by the auto completion doesn't make that much sense to me.
Say from the re package there is a method called findall. Eclipse could recommend that method when I do CTRL+Space:
But When I tried the auto completion in VI, it could not find the findall method. the only recommendation seems like a command that I have typed before which has nothing to do with the re module.
Can Anyone give me some hints what is the auto completion story behind the CTRL+P in VI and how could I tune it up so it would be as good as Eclipse's auto completion.
(Every time I doubt the power of VI, the truth is always that it is me who lack the power to discover the beauty of VI)
Thanks!
In vim <C-p> is not the only completion shortcut available. Omni completion defined for python should be called with <C-x><C-o>, then use <C-n>/<C-p> to select variants. There are more completion types, all start with <C-x>, see :h ins-completion.
It is still better to use some third-party plugin (like jedi-vim or klen/python-mode) to get python completion as they are smarter. Note that at least klen/python-mode will not redefine <C-p>. They redefine <C-x><C-o> by setting 'omnifunc' option.
Without any additional modules, vim's autocompletion only searches the current file for occurences of the string you've started typing, regardless of whether it makes sense. If you use the tags file, via ctags or other, you're able to use strings across a variety of files, rather than just the current buffer.

How can I make PyDev autoformat the maximum number of characters per line?

I am using PyDev for Python programming in Eclipse.
Is it possible to make PyDev auto-format my code to limit the maximum number of characters on a single line?
With standard PyDev
It seems that this option is only available in Eclipse's Java Editor.
The Java editor allows you to create "profiles" for the code formatter, while PyDev's options for the code formatter are very limited.
However,
You can hack this. PythonTidy.py is an awesome script that cleans up Python code to make it follow PEP8 conventions, and that can be tweaked with your own settings.
PythonTidy (code cleanup & formatting)
Get here (homepage) the source for PythonTidy.
You will see inside the file, at the beginning of the code and just after the comments, that many settings are defined.
The first one of these is COL_LIMIT with its default value set to 72. Now you can use PythonTidy to format your code the way you want.
Integration with PyDev
Now you have to make this work with PyDev's formatting. This blog post will explain it really better than me, but still I'll sum up the steps :
Write a Jython interface betwenn PyDev's editor (PyEdit) and PythonTidy. This blog's author already wrote a wrapper script in the public domain available in the above link or here in case the link goes 404.
Save this file anywhere you want, with the name pyedit_pythontidy.py, along with the PythonTidy.py file.
Configure PyDev to use this script as its Code Formatter. You can do this in Preferences > PyDev > Scripting PyDev
Note #1: I really recommend reading the original blog post to have a better understanding
Note #2: The wrapper script author did not implement Code Block formatting, so this means you can only format a full file. This should not be that hard to implement, up to you.
Note #3: All credits goes to bear330 for the PyDev integration part.

readline help feature & autocomplete

im willing to create a project same as JUNOS cli or cisco cli,
I came through gnu readline, but im confused as there are too many functions and methods to implement. any how i want a cli with auto complete using tab and space bar with question mark to display commands with help text.
I have two questions :
I have found code in python and perl but the im not use to python that code is complete and i just want to know if i should continue with python. im more experienced in perl but the code i found so far isn't complete for perl.
readline.parse_and_bind('tab: complete')
should i use the same complete function for both help and autocomplete feature. where as i have gone through another function
readline.set_completion_display_matches_hook(print_suggestions)
what you suggest :P im completely new to it!
I must say that I struggle to understand which specific point you are addressing in your question. But here is a suggestion anyway:
Start out with the cmd module. It gives you a nice little framework to build a command-line interpreter. It supports tab completion out of the box (provided readline is available). Start implementing your command-line interface. Once this stabilizes you can think about adding more comfort, e.g. tab completion for command arguments, help keys, and the like. This way, you have a working app to deal with, and can address readline details more specifically when you really need them. I wouldn't wade through the whole readline API upfront, if I were in your shoes.
We had to create a cli like JunOS/Cisco/VyOS and we built it on top of ishell, which uses readline for this job.
From the project page:
ishell helps you to easily create an interactive shell for your application. It supports command completion, dynamic arguments, a command history, and chaining of commands.
You can check the project at github: https://github.com/italorossi/ishell
Cisco example:
PS: I'm the author :).

Can I get Python debugger pdb to output with Color?

I'm using PDB a lot and it seems it would be even better if I could add systax highlighting in color.
Ideally, I'd like to have to the path to the code a lighter color.
The line of actual code would be syntax highlighted.
I'm using OS X and the Terminal app.
Python 2.7
pdb doesn't support colorization. However, it's not that hard to get it, even if you're a command-line addict (as I am;-) -- you don't have to switch to GUIs/IDEs just to get colorization while debugging Python. In particular, command-line tools usually work much better when you're accessing a remote machine via SSH, saving a lot of the bandwidth and latency issues that any remote access to GUIs and IDEs can inflict on you;-).
Specifically, for the task you're asking about, consider ipdb (you also need ipython, which offers a far more advanced shell than plain interactive Python, on which ipdb relies). Both offer you good tab completion, enhanced tracebacks, and colorization -- ipython for your normal interactive work, ipdb with the same features when you're debugging (otherwise just about the same as pdb).
You could try pudb, which works in the terminal and looks like this:
I haven't tried some of the options mentioned in other answers, but to judge from the PyPI pages, pudb is better maintained and better documented.
Take a look at pdb++ - it is a drop-in replacement for pdb that fills all your requirements and adds some other nice features such as tab completion and new commands such as watch and sticky.
Here is a sample config file that will enable colours (add this after creating the file: touch ~/.pdbrc.py):
import pdb
class Config(pdb.DefaultConfig):
use_pygments = True
pygments_formatter_class = "pygments.formatters.TerminalTrueColorFormatter"
pygments_formatter_kwargs = {"style": "monokai"}
This might not possible for you, but have you tried using a graphical debugger (like the one in eclipse/pydev)? It will give you your syntax highlighting and much more.
I only use pdb directly if I don't have an option, because a graphical interface is just that much nicer.
In case someone hit the problem with colorization in a console.
My console had white background while ipdb was also adding rather light colors to syntax (for example variables were white). Pressing man ipython shows that we have 3 colors available: 'nocolor', 'linux', 'LightBG'. Ipdb was in my case installed via easy_install into my virtualenv. So it was trivial to look into ipdb source and modify it (hint search for ipdb/init.py in your env). Then I've modified following:
def set_trace():
ip = ipapi.get()
+ def_colors = ip.options.colors
+ def_colors = 'LightBG'
Pdb(def_colors).set_trace(sys._getframe().f_back)
It's a kinda hackish solution but well its for debugging purpose on my working station so its sufficient. But if anyone finds something better. Please send me a message on what to do.

Quickhelp for Python in Emacs autocomplete.el?

I am using Emacs 23.1.1 on GNU/Linux with autocomplete.el 1.3 and Ropemacs 0.6.
In Lisp programming, autocomplete.el shows the documentation (known as 'QuickHelp' in autocomplete.el) of the suggested completions. Python completion with ropemacs works, but does not show quick help for the Python completion. Is it possible to enable it and did somebody make it work?
Ropemacs does the job : Use the function rope-show-doc over the symbol or use the keybinding C-c d. Simple :)
I stopped using all the autocomplete stuff in all my developing environments. They rarely do what I want. Either the lists is too long, or too short, or not sorted well. Therefore I use dabbrev-expand in all my modes global-set-key to tab.
This works even quote well for text. Usually it is enough to get a good expansion from the local buffer where you are in. If I start typing in an empty buffer I open a second one which expand can use to look up its suggestions. This is not language sensitive, not does depend on the object you want to call a method of, but its still a big boost, and you get used to it. Maybe its not, you don't get "quick help" this way.

Categories

Resources