Friday, August 24, 2007

Internet privacy with Firefox

If you don't want your girlfriend to know what you're watching online, you can use Firefox profiles to clear your footsteps. Here's how:

Start by printing this page, because you'll have to close Firefox in order for this to work. Next, launch Firefox with the -p option. If you're in Ubuntu, you can do this by pressing Alt-F2 and typing firefox -p at the "Run Application" window.

The profiles window will appear with only one profile called "default". Create a new one (let's call it private), select it and start Firefox. Next, go to "Edit", "Preferences" and in the "Privacy" tab untick all the "History" options. Tick the "Always clear my private data when I close Firefox", click the "Settings" button and tick all the boxes. Your private profile is now ready!

Now close Firefox and open it again with the -p option. Select the "default" profile and set the "don't ask me on startup" option. Run Firefox and make sure you have the old profile back (with history and all). Of course I'm assuming that's what you want by default.

From now on, each time you want to navigate to websites you don't want your girlfriend to know about (say, because you want to surprise her with a valentines gift), just execute Firefox by typing firefox -p private.

Wednesday, August 22, 2007

Sending emails via Gmail with Python

Whenever I need to send files from work to home I use gmail. Usually this email message consists of one single file (that may be a tarball) and has the file name for subject. In the "Automate Everything" spirit, I decided to build a script to do this task for me.

The first thing I had to do was find out how to send an email with an attachment via gmail. It wasn't too hard to find this information around the web, but it still took me the best part of an hour. So here's a simple Python script that sends an email with an attachment:

#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "your_email@gmail.com"
gmail_pwd = "your_password"

def mail(to, subject, text, attach):
msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()

mail("some.person@some.address.com",
"Hello from python!",
"This is a email sent with python",
"my_picture.jpg")

Wednesday, August 15, 2007

Dual monitor in Ubuntu

I bought a new 32'' LCD TV. A Samsung LE32S62. The TV comes with a VGA connection, so obviously I had to connect my laptop to it!

The first challenge was making Ubuntu work with dual monitor. I did a few searches on the web and found a few instructions on how to do this. I ended up with a rather simple configuration. Just edit the /etc/X11/xorg.conf file and add the following under the "Device" section. (NOTE: I'm using an nVidia graphics card. This may be different for your card)

Option "TwinView" "on"
Option "TwinViewOrientation" "RightOf"

Next restart X (Ctrl-Alt-Backspace) and you should end up with dual monitor. This isn't exactly brilliant, because the TV ended up as the main monitor, and I would prefer to have the laptop as the main monitor. But after a bunch of failed tries I just gave up. After all, I don't think I'll use this setup a lot of times. (but I am thinking on recovering my old computer to use it as a media center!)

Here's a couple of pics of the setup:


Setting up xorg.conf with gvim


Watching a movie with VLC

Sunday, August 12, 2007

Back to basics: Vi

I've been doing a lot of programming in Ubuntu, mostly in PHP (my sister's in law web site), Perl and Python. I tried a lot of editors and (so called) IDEs, but was never happy with any of them. So I went back to basics: using Vim.

Now, I can ear the screams of Emacs users and the cries of all of you who only learned the :q! shortcut to exit Vi as soon as possible, but here's my view on things:

Emacs users: I tried using Emacs. I really did. And I actually used it for a long time while I was in college. But after using Vi for a while I had a really hard time both remembering the shortcuts and configuring Emacs to behave like I wanted to. So I just stuck with Vi.

:q! users: Vi is not that hard. Really! It does need a bit getting used to, but the freedom of not using the mouse and all those small shortcuts that you think you'll never use but turn out to be extremely useful make up for the learning curve.

To speed up your learning curve, here are the two secrets to loving Vi:
  • A proper .vimrc file
  • Knowing the right commands
Because there are so many "right commands" I'll leave that to a later post. Let's focus on the .vimrc file for now. And just to get your expectations in the right place, I'm not a Vim power user. If you look the web for .vimrc files you'll find files with hundreds of lines of code. Mine has 25, including comments... But it makes Vim behave like I want, so it's enough. Just go through the code and comments and you'll be able to understand what the configurations do. If not, drop me a line.

NOTE: The last 2 lines are wrong! Instead of [ you should have <. And instead of ] you should have >. But Blogger does a really bad job with these 2 characters....
" Turn on syntax highlight
syntax on
" Set tabs to 4 in size and make sure autoindent is on
set tabstop=4
set autoindent
" Make backspace work as in most editors
set backspace=indent,eol,start
" Black background and white foreground
highlight Normal guibg=black
highlight Normal guifg=white
" Make the default window size 50x80
set lines=50
set columns=80
" Store temp and swap files in this directory
set directory=/tmp/
" Show the status line
set ls=2
" Set the status line to:
" Filename [Modifed & Readonly Status] line number/total lines column (#buffer)
" More info at:
" http://www.winterdom.com/weblog/CommentView,guid,3372fa1e-966e-4389-b2ba-bb2ce527f52a.aspx
let &stl="%f %([%R%M]%)%=%l/%L %c (#%n)"
" Map Ctrl-Ins and Shift-Ins to clipboard copy paste (FIX THIS!!!)
map [C-Insert] "+y
map [S-Insert] "+gP

Saturday, August 11, 2007

Objects in Perl? Use Python!

I've been meaning to try Python for a while. I'm kind of a sucker for programming languages, and knowing a bunch of languages allows me to choose the right tool for the right job. Turns out that this week I had the perfect opportunity to check out Python.

The task at hand was analyzing patterns in a bunch of XML files. I had 270Mb of XML files, and the largest file was 32Mb. Because I was pretty sure I was executing the analysis more than once (errare humanum est and I want to improve the analysis in the future) I decided to use SAX to read the files.

Having decided to use SAX I then decided to use Perl to do the job. I'm pretty familiar with it and was able to quickly find a SAX sample. Besides, I had a few string matches and replacements to do, and Perl is a great language for that.

Turns out that using SAX in Perl demands that you use and define objects in Perl. And it turns out that defining objects in Perl is... well, terrible! I really hated the syntax, bless and the way attributes were defined. It all looks like a big hack! To add insult to injury, the perlSAX has a few quirks when changing handlers. This is mandatory to make your SAX code maintainable... So I dropped Perl and went for Python.

To my surprise the transition was really easy. I was able to convert my Perl code to Python very quickly, with only a few doubts now and then on specific stuff. Here's what I gained from the transformation:
  • I learned Python (finally!)
  • Better SAX handlers (the quirks that happen in Perl don't happen in Python)
  • Clearer attribute access (if you have an object with a reference to an array of references and want to print it in Perl... things can get weird)
  • Clearer object definition and usage (no bless!)
  • Fewer lines of code (from ~250 to ~150)
  • Same performance (I was worried about this, but both scripts took the same time to execute!)
I still love Perl. If I want to parse a bunch of text files, do text transformations and the like it will be my 1st choice.

But whenever I need to do something a bit more complex that requires complex data types or OO programming, from now on I'll definitely turn to Python!