Python Documentation and Vim
·Today at a CodeMash session I did a bit of pair programming, which honestly I hadn’t done in over 10 years and was a great experience, and we were given to work on two programming exercises: the canonical “FizzBuzz” game and then a more difficult String Calculator Kata. Because my partner did not have a language environment he felt comfortable taking the lead with – his primary languages, oddly enough, were Bash, PowerShell, and a proprietary ERP language – I ended up controlling the keyboard and we finished the exercise using Python.
The String Calculator exercise is very straightforward until you get to the 7th step and beyond, which involve handling a variable number of variable length delimiters within a string. Obviously, this problem calls for regular expressions and while I had intended to avoid that route since my partner was not familiar with them, I decided it was much easier than the alternative approach of multiple character-by-character passes through the input string.
Now, anyone who has used the Python standard library can tell you that the re
module is one of its oldest and cruftiest corners. The module clearly dates
back to the very early days of the language and, to my amateur eye at least,
there seem to be hidden and dangerous C-based things happening under the hood
that make the module relatively difficult to work with, or at least difficult
to remember the proper incantation to work with it effectively.
While working with my partner I referred to the Python documentation numerous
times, including to browse a list of methods on the built-in str
type and the
interface for the re.MatchObject
(because, Quick, how do you get a list of
all matched groups from a MatchObject
?)
You can get (most of) this information directly from the pydoc
command and
there is a vim plugin that integrates it
nicely. Save that plugin in ~/.vim/bundle
(You are using
Pathogen, right?) and you now have the
:Pydoc
and :PydocSearch
commands to search the Python documentation
directly in vim. The first command allows you to read the docs for a
module/class/method/function if you know its name already (e.g. :Pydoc
re.search
) while the latter searches through the documentation for matching
strings in module doc synopsis lines (that is, it’s the same thing as `pydoc -k