Skip to content

Configuring VIM as a (Python) IDE

While this is slanted towards configuring Vim for Python development this setup minus a couple of Python specific plugins (pep8, pydoc and pyflakes) would work well for any language.

This setup was influenced by Turning Vim into a modern Python IDE.

Vim Plugins

First of all, here is the list of Vim plugins we’ll be working with …

Autoclose

AutoClose automatically (and correctly) closes brackets, parenthesis, curly braces, single and double quotes.

Color-Sampler-Pack

Color-Sampler-Pack contains the top 100 rated color schemes on vim.sf.net.

Gundo

Gundo is a visual history browser.

Lusty

Lusty includes the LustyExplorer and LustyJuggler plugins for Vim which provide easy to use file and buffer management.

pathogen

pathogen allows plugins to be installed and managed within their own private directories.

pep8

pep8 checks your Python code against some of the style conventions in PEP 8.

pydoc

pydoc integrates viewing and searching Python documentation.

pyflakes

pyflakes highlights common Python errors like misspelling a variable name on the fly. It also warns about unused imports, redefined functions, etc.

ScrollColors

ScrollColors is a colorscheme Scroller/Chooser/Browser.

snipMate

snipmate provides tab completion for often-typed text.

SuperTab

SuperTab provides a tab completion for a variety of tasks.

virtualenv

virtualenv sets Vim paths correctly when working with virtualenvs, allows for activating and deactivating within a Vim session.

Dependencies

pyflakes

The only plugin that requires an external dependency is pyflakes. Let’s take care of that first.

sudo yum install pyflakes

Alternatively, you could install via pip.

sudo pip install pyflakes

git

We are going to manage our Vim modules with git. This allows for easy updating, rollback etc. of Vim plugins. Additionally, most Vim plugins are directly avaialable via github. See the vim-scripts page at github.

sudo yum install git

Repo Preperation

Create plugin directories.

cd $HOME
mkdir -p .vim/{autoload,bundle}

Initialize repo.

cd .vim
git init

Install plugins as submodules to the repo.

git submodule add https://github.com/andrewle/vim-autoclose.git bundle/vim-autoclose
git submodule add https://github.com/vim-scripts/Color-Sampler-Pack.git bundle/color-sampler-pack
git submodule add https://github.com/sjl/gundo.vim.git bundle/gundo
git submodule add https://github.com/sjbach/lusty.git bundle/lusty 
git submodule add https://github.com/cburroughs/pep8.git bundle/pep8
git submodule add https://github.com/vim-scripts/pydoc.vim.git bundle/pydoc
git submodule add https://github.com/tpope/vim-pathogen.git bundle/pathogen
git submodule add https://github.com/vim-scripts/ScrollColors.git bundle/scrollColors
git submodule add https://github.com/ervandew/supertab.git bundle/supertab
git submodule add https://github.com/jmcantrell/vim-virtualenv.git bundle/vim-virtualenv 
git submodule init
git submodule update
git submodule foreach git submodule init
git submodule foreach git submodule update

Create symlink in order to autoload pathogen.

ln -s '../bundle/pathogen/autoload/pathogen.vim' autoload/pathogen.vim
git add autoload

Move vimrc under repo and symlink it under home.

mv $HOME/.vimrc .
ln -s './.vim/.vimrc' $HOME/.vimrc
git add .vimrc

Commit changes to repo.

git commit -m "Initial commit."

vimrc file

" pathogen
let g:pathogen_disabled = [ 'pathogen' ]    " don't load self 
call pathogen#infect()                      " load everyhting else
call pathogen#helptags()                    " load plugin help files

" code folding
set foldmethod=indent
set foldlevel=2
set foldnestmax=4

" indentation
set autoindent
set softtabstop=4 shiftwidth=4 expandtab

" visual
highlight Normal ctermbg=black
set background=dark
set cursorline
set t_Co=256

" syntax highlighting
syntax on
filetype on                 " enables filetype detection
filetype plugin indent on   " enables filetype specific plugins

" colorpack
colorscheme vibrantink

" gundo
nnoremap <F5> :GundoToggle<CR>

" lusty
set hidden

" pep8
let g:pep8_map='<leader>8'

" supertab
au FileType python set omnifunc=pythoncomplete#Complete
let g:SuperTabDefaultCompletionType = "context"
set completeopt=menuone,longest,preview

bashrc

Additionally, I suggest making the following changes to .bashrc

export EDITOR=/usr/bin/vim

Reference

Autoclose

Links:
vim.org page
github page

Reference Files:
bundle/vim-autoclose/README
bundle/vim-autoclose/plugin/autoclose.vim

Color-Sampler-Pack

Links:
vim.org page
github page

Reference Files:
bundle/Color-Sampler-Pack/README
bundle/Color-Sampler-Pack/plugin/color_sample_pack.vim

Gundo

Links:
main page
vim.org page
github page

Reference Files:
bundle/gundo/README.markdown
bundle/gundo/plugin/gundo.vim

Help:
:help gundo

Lusty

github page

LustyExplorer

Links:
vim.org page
github page

Reference Files:
bundle/lusty/doc/explorer-vim.writeup
bundle/lusty/plugin/lusty-explorer.vim

LustyJuggler

Links:
vim.org page
github page

Reference Files:
bundle/lusty/doc/juggler-vim.writeup
bundle/lusty/plugin/lusty-juggler.vim

pathogen

Links:
vim.org page
github page

Reference Files:
bundle/pathogen/README.markdown
autoload/pathogen.vim

pep8

Links:
vim.org page
github page

Reference Files:
bundle/pep8/README.rst
bundle/pep8/pep8.py

pydoc

Links:
vim.org page
github page

Reference Files:
bundle/pydoc/README
bundle/pydoc/ftplugin/python_pydoc.vim

pyflakes

Notes:
This depends on the external pyflakes Python module. Install via yum or pip.

Links:
vim.org page
github page

Reference Files:
bundle/pyflakes-pathogen/README.rst
bundle/pyflakes-pathogen/ftplugin/python/pyflakes.vim

See Also:
:help quickfix

ScrollColors

Links:
vim.org page
github page

Reference Files:
bundle/ScrollColors/README
bundle/ScrollColors/plugin/ScrollColor.vim

snipMate

Links:
vim.org page
github page

Reference Files:
bundle/snipmate/README.markdown
bundle/snipmate/plugin/snipMate.vim

Help:
:help snipMate

SuperTab

Links:
vim.org page
github

Reference Files:
bundle/supertab/plugin/supertab.vim

Help:
:help supertab

virtualenv

Links:
vim.org page
github page

Reference Files:
bundle/vim-virtualenv/README.mkd
bundle/vim-virtualenv/plugin/virtualenv.vim

Help:
: help virtualenv

Setting up virtualenv, pip, virtualenvwrapper and IPython on Fedora

Install the necessary packages.

sudo yum -y install python-pip ipython

Use pip to install virtualenvwrapper and virtualenv to our home directory.

echo 'alias pip="pip-python"' >> .bashrc
source .bashrc
pip install --install-option="--prefix=$HOME" virtualenvwrapper

Setup environment for virtualenv.

mkdir ~/virtualenvs
echo 'export WORKON_HOME=$HOME/virtualenvs' >> .bashrc
echo 'source $HOME/bin/virtualenvwrapper.sh' >> .bashrc
source .bashrc
workon

Setup postmkvirtualenv script to install IPython. I adapted the postmkvirtualenv script from here.

echo 'CACHE=$WORKON_HOME/.pip-cache
mkdir -p "$CACHE"
$VIRTUAL_ENV/bin/pip install --download-cache="$CACHE" ipython' >> $WORKON_HOME/postmkvirtualenv

Create test virtualenv.

mkvirtualenv --no-site-packages test-ipython
cdvirtualenv
ipython --c='print("success")'
deactivate
rmvirtualenv test-ipython

Enjoy.

Flattening lists in Python

This has come up a few times recently so I thought I’d do a quick post on the subject.

The first example flattens a list of single lists, using a list comprehension.
This solution does not work with nested lists.

list_of_lists = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
[ item for inner_list in list_of_lists
       for item in inner_list ]

The second example flattens a list of nested lists, using recursion.
This solution works with a list of single lists or with nested lists.

flat = list()
def flatten(nested, flat):
    for i in nested:
        flatten(i, flat) if isinstance(i, list) else flat.append(i)
    return flat

nested_list = [1, 2, [3, 4, [5, 6, 7, [8]]], [9], 10]
flatten(nested_list, flat)

This writes the values to en existing list, which I don’t care for style-wise. The next example does away with that and returns a new list.

def flatten(nested):                                                                                               
    flat = list()                                                                                                  
    def flatten_in(nested, flat):                                                                                  
        for i in nested:                                                                                           
            flatten_in(i, flat) if isinstance(i, list) else flat.append(i)                                         
        return flat                                                                                                
    flatten_in(nested, flat)                                                                                       
    return flat

nested_list = [1, 2, [3, 4, [5, 6, 7, [8]]], [9], 10]
flatten(nested_list, flat)

These examples execute in about the same amount of time. If you want to test for yourself save the first example to example1.py, the second to example2.py and run the following:

python -m timeit -s "`cat example1.py`"
python -m timeit -s "`cat example2.py`"
python -m timeit -s "`cat example3.py`"

The second or third example is a must if you are going to be consuming nested data. Otherwise, I would say it’s just a matter of style.

A few useful bytearray tricks

I came across this blog post earlier and found it useful.

Some cool new features in PyCharm 1.5 (EAP)

New features include …
External Documentation
Rendered Docstrings
Syntax Highlighting and Completion in Docstrings
Docstring Stub Generation
reStructuredText support

Read more and see some screenshots here.

Scapy 2.2 released.

Get it here. See the changelog here.

PyCon 2011 US video is available

First of all I’ve had a great time at PyCon this year. I’ve met a lot of great people, seen some really good talks and learned a few things as well. I don’t think you can ask for much more than that.

Thanks to the PyCon coordinators (and volunteers) for putting the conference together and letting me present my tutorial. I’d also like to say thanks all those who attended the tutorial. I know it was a hard topic and we went fast, so thanks for hanging in there.

Anyway, videos of the talks are available here. All of the slides should be available shortly here.

Hidden features of Python – Part 2

Following up on Marcos’ previous post, here is a list of my favorites …

Creating generators objects

Wrap an iterable with enumerate

Sending values into generator functions

Function argument unpacking

Context managers and the “with” Statement

Doctest

Conditional Assignment

Interactive Interpreter Tab Completion

Nested list comprehensions and generator expressions

Hidden features of Python – Stack Overflow

Hello Pythonistas!  Ran into this link from a tweet I’d received.   Check it out!  http://dlvr.it/F0jhg

Here are the rules for posting and commenting to this over at Stack Overflow:

What are the lesser-known but useful features of the Python programming language?

  • Try to limit answers to Python core.
  • One feature per answer.
  • Give an example and short description of the feature, not just a link to documentation.
  • Label the feature using a title as the first line

Easy VoIP listening with Wireshark

Hello there!

This post is inspired by the Holiday themed “The Nightmare Before Charlie Brown’s Christmas” over at Ethical Hacker.

While I will not cover all the questions and answers outlined in the link above, I will show you how to easily discern the antagonist’s nefarious plan from the packet capture  file provided for the challenge.   You can find the capture by clicking here.

According to the link above, what the antagonist is doing is plugging his laptop into the patch cable he’d removed from an IP phone.  He is then able to get himself an IP address on the same subnet as the IP phone, and essentially eavesdrop and inject any calls that are made.

The only records we have to work with are the server logs (not covered here) and the packet capture.   Let’s open the packet capture with Wireshark.

Initial Opening of the PCAP

Wow!  That’s a lot of Session Initiation Protocol (SIP) packets!  I’m no VoIP expert, but I’m pretty sure that Wireshark has a few neat tricks to easily make sense of this.   Let’s check it out!

Click on the Telephony menu on the toolbar, and then select VoIP Calls.  You should see a new window like this:

All VoIP calls are belong to us!

Pretty nice, no?  In just a couple of steps, we were able to read in a packet capture and use Wireshark’s internal analysis capabilities to make sense of our villain’s plot!   Notice the Player button in the center of the image above.   Go ahead, you know you want to click on it!

As I hoped to keep this short and sweet, all you have to do now is choose which conversation you wish to hear at a time, and simply let the player do the work.

So, stop delaying and head over to Ethical Hacker and engage this challenge.  The contest is over, but the opportunity for learning is still there.  Enjoy!

Follow

Get every new post delivered to your Inbox.