Blog

Python autompletion in Vim

a.l.e, 2013-02-13

Today I've been asked for ways to get interactive help while typing Python code in Vim.

I had to admit that I try to use Vim "as Vanilla as possible"... but I wanted to give it a try.

So, i guess that what you most want is to have some drop downs popping up that tell you which method are avaiable in a specific context.

As a first thing, you have to make sur that Vim is compiled with the +python option. The best way to know it, is to run:

$vim --version

If it says (among other things) -python you're out of luck. If it says -python and you're on Debian, and you only have installed plain vim (or, even worse, vim-tiny) you should install vim-noxto get many more options compiled in. You'll also get +python.

Once you've got that, it's easy to get "Omni completion to work. You can start Vim, save a .py file and type:

import string
string.

We will now configure Vim in a way that when your cursor is after the last dot, a pop shows up and you get a documentation in a separate pane.

In vim, type the following commands:

:filetype plugin on
:set ofu=syntaxcomplete#Complete

Now, while the cursor is after the dot, use the ctrl-x ctrl-o to get hints in a popup!

If you like how Omni completion works, you can add the following lines to your .vimrc file:

filetype plugin on
set ofu=syntaxcomplete#Complete

oneliner pattern drawing with python

a.l.e, 13.1.2013

just run

import sys, random; print "".join([random.choice(["╱", "╲"]) for x in range(2000)])

and you'll get:

╱╱╱╲╱╲╱╲╱╲╲╱╲╱╱╱╱╲╱╱╲╱╲╱╱╲╲╲╱╱╱╱╲╱╱╱╱╱╱╲╲╲╲╲╱╱╲╱╱╱╲╲╱╱╲╲╲╱╲╱
╲╱╱╲╲╱╱╱╱╲╱╱╲╱╲╲╱╱╱╲╱╲╲╲╱╱╲╱╲╱╱╱╲╱╱╲╲╲╲╲╱╱╲╱╱╲╱╲╱╲╱╲╲╱╱╱╱╲╲╱
╱╱╱╱╲╲╱╲╲╲╲╲╲╲╱╱╱╱╱╲╲╲╱╲╲╲╱╱╱╲╲╲╲╱╱╱╱╱╲╲╱╲╱╲╲╲╲╲╱╱╱╲╱╱╱╲╱╲╱╲
╲╲╲╲╱╱╲╲╲╱╲╱╱╱╲╱╲╱╱╱╲╱╱╱╱╱╲╱╲╱╱╲╱╲╱╱╱╱╲╱╲╲╲╱╲╱╲╱╱╱╱╱╱╲╲╲╱╲╲╱
╲╱╱╱╲╲╲╱╲╲╱╱╱╲╱╲╱╲╲╱╲╲╲╱╲╱╲╱╲╱╱╲╲╲╱╲╱╲╱╲╲╱╱╱╲╲╱╲╱╱╱╱╱╱╲╱╲╲╲╱
╲╱╱╱╱╱╲╱╱╱╱╲╱╲╲╱╲╱╲╱╲╱╲╱╱╲╱╱╲╲╲╲╲╲╱╱╲╱╲╲╱╱╱╲╱╲╱╲╱╱╱╲╱╱╲╲╲╱╲╲
╱╲╱╲╱╱╲╱╲╱╲╲╲╱╲╲╱╱╲╱╱╲╱╲╱╲╱╱╲╲╲╲╱╲╱╲╱╱╲╲╱╲╱╱╲╱╲╱╲╲╱╲╱╱╲╲╱╱╱╲
╱╱╱╱╱╲╲╲╲╲╱╱╲╱╲╱╱╱╲╱╱╲╲╱╲╱╱╱╱╱╲╲╱╲╲╱╲╲╱╱╱╲╲╲╲╲╱╲╲╱╲╱╱╲╲╲╱╱╲╱
╲╱╲╲╱╲╲╲╱╲╱╲╲╱╲╲╲╲╲╱╱╱╱╱╱╲╱╲╲╱╱╲╱╱╲╱╲╲╲╱╱╲╱╲╱╱╲╱╱╲╲╱╱╱╱╲╲╱╲╲
╲╱╲╲╲╲╱╱╱╲╲╲╲╱╲╱╲╱╲╱╲╲╲╲╱╲╲╱╱╲╱╱╱╱╲╱╲╱╱╲╱╲╱╱╱╱╲╲╱╲╲╱╲╱╱╱╲╱╲╲
╱╲╱╱╱╲╲╱╱╱╱╱╱╲╱╲╱╲╲╱╲╱╲╲╱╱╲╲╱╲╱╱╲╱╱╱╲╱╲╲╲╱╱╱╲╲╱╲╱╲╲╱╲╱╲╲╲╲╱╲
╲╲╱╲╲╲╱╱╲╱╲╱╲╲╱╲╲╱╲╲╲╱╲╲╱╱╲╱╱╱╲╲╲╲╲╲╱╱╱╱╱╲╱╲╲╱╱╲╲╱╲╱╲╲╱╲╱╲╱╲
╱╱╲╱╱╲╱╱╲╱╱╲╲╲╱╲╱╲╲╱╲╲╲╱╲╲╲╲╲╱╲╱╱╲╲╱╲╱╲╲╱╱╲╲╲╱╲╱╲╱╲╱╲╱╱╲╱╲╱╱
╱╱╱╱╱╲╲╱╲╲╱╱╱╲╱╱╱╱╱╲╲╱╱╱╲╱╲╱╱╱╱╱╱╲╱╲╲╱╱╲╱╱╲╲╱╱╲╱╱╱╱╲╱╱╱╲╲╲╱╲
╱╲╲╱╱╱╱╱╱╱╱╲╱╱╱╲╲╲╲╲╲╱╲╱╱╱╲╱╱╱╲╱╲╲╲╱╱╲╲╲╲╱╲╲╱╱╲╲╱╱╱╲╲╲╲╱╱╲╲╱
╲╱╲╲╲╱╲╲╱╱╲╲╲╲╱╲╲╲╲╲╲╱╱╲╱╲╱╱╱╱╲╲╱╲╱╱╲╱╱╱╲╱╱╲╱╱╲╲╲╲╲╲╲╲╲╲╱╲╱╲
╲╱╲╲╲╱╲╲╱╱╱╱╲╲╲╲╲╲╱╲╲╲╲╱╱╲╲╲╱╱╱╱╱╲╱╲╲╲╱╱╲╱╱╱╲╲╱╱╲╲╲╲╲╱╲╲╱╱╲╲
╱╱╲╲╱╱╱╱╱╱╲╲╱╲╲╲╱╲╱╲╱╱╱╱╲╱╲╲╲╱╲╱╲╱╱╱╲╱╱╱╱╲╲╲╲╲╲╱╱╱╱╲╲╱╲╱╱╱╲╲

cute isn't it?

Low-tech time-laps movie

a.l.e, 29.10.2012

I've been looking for a way to stream from our last week translation sprint in Brescia and I've finally found a very simple and cheap solution.

Specifications:

Capturing the images

I wrote a small script that uses fswebcam to take a snapshot every n seconds:

    #!/bin/bash
    fswebcam -d v4l2:/dev/video1 -i 0 -r 640x480 -l 5 --jpeg 30 --no-banner capture.jpg --exec ./upload.sh

The parameters mean: - I'm using the external webcam (that's why it's video1 and not viedo0) - 640x480 is already pretty big for what i need... - I want a snapshot every 5 seconds - I want the snapstho to be very low quality (30% jpeg quality) - The snapshot is saved into the capture.jpg file

A second script uploads and caches each image:

    #!/bin/bash
    ncftpput yourserver webcam/ capture.jpg
    now=`date +%Y%m%d_%H%M_%S`
    thishour=`date +%Y%m%d_%H`
    cp capture.jpg capture_$now.jpg

Of course, if you only want to have a timelaps movie, without a live view, you don't have to upload the file to the webserver!

Some notes:

Showing the stream

You simply have to add an HTML file on your webserver (in my case /index.html) that shows the uploaded images and reload it through some javascript at the same interval as the snapshot are taken:

<html>
<head>
<title>webcam</title>
<script>
var url = "capture.jpg";
function srcreload() {
    var image = document.getElementById("capture");
    var random = "?" + Math.random();
    image.src = url + random;
    setTimeout(srcreload, 5000);
}

setTimeout(srcreload, 5000);

</script>
</head>
<body>
<title>webcam</title>
<img id="capture" src="capture.jpg" />
</body>
</html>

If you're on a shared hosting, make sure that that you're not producing too much traffic upstream (your uploads) or downstream (your visitors looking at your images).

Creating the time-laps movie

Once your event is over and you have all the images in a directory:

mencoder "mf://capture*.jpg" -o capture.avi -mf fps=16 -ovc lavc -lavcopts vcodec=mjpeg

There are lot of ways to create the movie. Just pick one!

With ffmpeg, I've finally converted the AVI movie into WEBM for uploading it to the web. It will be online very soon (I want to add some titles...)