Paolo Donadeo — LifeLOG

All about my life, job and thoughts
  • rss
  • Home
  • Blog
  • Contacts
    • GPG public key
    • Alessandro Donadeo — Curriculum Vitæ

PyCon Due

Paolo | 14/05/2008 | 13:32

PyCon2 Italia official logo

Si è appena conclusa la seconda convention italiana dedicata al linguaggio Python, il PyCon2, svoltosi a Firenze lo scorso fine settimana. Pur non essendo un vero pythonista sono comunque interessato a Python perché in questo momento gran parte della mia attività di consulenza richiede la conoscenza di questo linguaggio e, devo dire, perché molto attirato da alcuni key note, tra i quali spicca certamente quello di Alex Martelli su Google App Engine.

Un altro aspetto interessante è stato per me il constatare come molti talk fossero incentrati sulle tecnologie legate al web e siccome sto scrivendo Ex-nunc, un web framework in Objective Caml, ho voluto informarmi su quale fosse lo stato dell'arte di progetti simili scritti in Python.

L'organizzazione della manifestazione è stata a dir poco eccellente. Develer, l'azienda che ha promosso ed organizzato il PyCon2, non ha tralasciato nessun dettaglio, a partire dal sito della manifestazione, dettagliatissimo e ricco di informazioni, dalla scelta dell'albergo centralissimo e raggiungibile a piedi dalla stazione ferroviaria, fino al ricco buffet.

Anche l'organizzazione logistica dei talk è stata di livello eccezionale: ottima acustica e disponibilità di traduzione simultanea per gli ospiti di lingua Inglese.

Infine, magari meno importante, la cornice della città di Firenze è splendida e, seppure immagino sia possibile organizzare un PyCon3 a Sesto San Giovanni, spero davvero che scelgano ancora, l'anno prossimo, una città in cui ad ogni angolo si vedono panorami come questo.

Organizzazione perfetta, argomenti trattati interessanti ed ospiti internazionali hanno fatto del PyCon2 un appuntamento di assoluto rilievo, che non teme il confronto di nessuna manifestazione simile nel mondo. Per il panorama italiano si tratta poi di un fatto del tutto eccezionale anche dal punto di vista culturale, in un paese che di tecnologia parla sempre meno e di software libero non parla affatto.

Comments
2 Comments »
Categories
Article, Computer programming, Italian, Python, Spare time
Tags
Article, Computer programming, Italian, Python, Spare time
Comments rss Comments rss
Trackback Trackback

Sending emails via Gmail with Objective Caml

Paolo | 26/04/2008 | 23:00

Gmail logo with the caml

Motivation

Last week I was writing a Python script to make an automatic backup, and I decided to send me an email in case of scp failure. I decided to use Python to send the email, possibly via GMail and I found this interesting blog post: Sending emails via Gmail with Python. I like Python, it's a good programming language, but my heart (as a developer!) beats for the Objective Caml programming language.

So I decided to port the script presented in the post in OCaml. The result is this sendmail.ml.

Compiling the script

To compile the script you need four software components:

  1. the Objective Caml environment. You can download it from the INRIA site;
  2. Findlib, to make compiling very simple;
  3. Ocamlnet: here is the home page of the project;
  4. OCaml binding to the SSL library.

You can of course compile all this stuff, but every decent Linux distributions has all packaged. In Debian you have to run the following command:

# aptitude install ocaml libocamlnet-ocaml-dev \
  libssl-ocaml-dev ocaml-findlib

Now, to compile the script, issue the command:

$ ocamlfind ocamlopt -linkpkg -package \
  netstring,smtp,ssl,str sendmail.ml -o sendmail

Before using it, remember to customize your name, email address, GMail user and password.

Code comparison

The first difference that jumps out at everyone confronting the two scripts is the number of lines: 41 lines for Python against 163 of my OCaml version. The difference is justified by the fact that the Python standard library comes with an almost full featured SMTP client, with ESMTP and TLS capability. On the other side Objective Caml has a very concise standard library, which includes essential modules and data structures, but no "batteries" are provided out of the box. This is a precise design decision by INRIA and, in some ways, I agree with them. Luckily the OCaml community is a source of excellent libraries and bindings, like Ocamlnet by Gerd Stolpmann and the SSL library binding, written by Samuel Mimram. The first one is in particular the Swiss Army Knife for network oriented battles.

Since the SMTP client provided by Ocamlnet doesn't include TLS capability I decided to stole the source code and adapt it to my needs, to have a more comfortable and high level interface resembling the one offered by the Python standard library.

So the different length is easily explained: 109 lines of code are devoted to the smtp_client class, and the actual script is 54 lines long.

The forward pipe operator

All Turing complete computer languages are equivalent, but everyone knows this is only the theory and everyone have a programming language of choice. Here are two examples of what you can do in OCaml.

The first is the pipe operator:

let (|>) x f = f x

Here we define a (very common in FP) infix operator which simply inverts the order of its operands. What the frack is this? Very simple, we use it to invert the order of a function with its last parameter so, if we want to compute the 3rd Fibonacci number we can write:

let fib3 = fibonacci 3

but also:

let fib3 = 3 |> fibonacci

This is not a style issue, we can define a simple infix operator that feeds a function with a value; we can of course connect several functions together, like in a shell script with the Unix pipe operator, transforming an ugly and difficult to be read call:

let result = func1(func2 (func3(x)))

into:

let result = x |> func3 |> func2 |> func1

In the sendmail.ml script, line 127, we read:

email_string |>
  Str.global_replace new_line_regexp "\r\n" |>
    Str.split crlf_regexp |>
      List.iter (fun s ->
        self#output_string (if String.length s > 0 && s.[0] = '.' then
                              ("." ^ s ^ "\r\n")
                            else s^"\r\n"));

Here we take the string containing the email, we replace all new lines with the sequence "\r\n", split the stream into lines and in the end send each line to the SMTP server, taking care of quoting each line starting with a period. In 6 lines of code.

Algebraic data type

Algebraic data type are a very interesting aspect of functional programming. We can easily wrap two heterogeneous data types into a single one with two line of code:

type socket =
  | Unix_socket of Unix.file_descr
  | SSL_socket of Ssl.socket

The smtp_client class contains a reference to the connection handle used for communicating with the server which is a plain file descriptor or an SSL socket, which one depends on the state of the communication. I do not want to create a virtual class or an interface and two implementing class as I should do in horrible languages like Java, spending half an hour deciding which methods to put in the public interface, and so on; after all, it's only a file descriptor!

Now I have a new type which is a disjoint union of the two original types and I can write code like this (line 54):

let input = match channel with
  | Unix_socket s -> Unix.read s
  | SSL_socket s -> Ssl.read s i

Here we say: if channel is actually a Unix file descriptor, let's define a new function "input" which is the standard function "read", from Unix module, otherwise, if channel is an SSL socket, let's define "input" as the Ssl.read function, which works only in ciphered sockets. From now on I'll use input instead of one of the two original functions.

Ok, it's time to stop the waffle. Enjoy the script if you need, it's completely free, like in free beer, in free speech and even in free sex! :-)

Comments
No Comments »
Categories
Article, Computer programming, English, Objective Caml, Python, Spare time
Tags
Article, Computer programming, English, Objective Caml, Python, Spare time
Comments rss Comments rss
Trackback Trackback

Administration

  • Register
  • Log in
  • Entries RSS
  • Comments RSS

Search with Google

Facets (like tags, but better)

Article Books Chillout Cinema Computer programming Digital life Editors Essay Information retrieval Job Karate Librarianship Life Linux Music News Nu-jazz Objective Caml Photography Politics Python Random thoughts Review South American literature Spare time Tips Visual arts Writing

Site map

  • Home
  • Blog
  • Contacts
    • GPG public key
    • Alessandro Donadeo — Curriculum Vitæ

Facets hierarchy

My photos on Flickr

www.flickr.com

RSS Recent links on del.icio.us

  • Nemiver : A GUI debugger for GNOME
  • Monads are Plug-ins
  • Goodbye, Passwords. You Aren’t a Good Defense.
  • Raphaël—JavaScript Library
  • The Official Website of the Beijing 2008 Olympic Games
  • A Fundraising Survival Guide
  • TestDisk - CGSecurity

RSS Interesting News

  • eigenclass - Some functional programming and OCaml koansreddit/programming
  • Uomini e mezz'uomini, ominicchi, piglianculo e quaquaraquàBlog di Beppe Grillo
  • Richard Jones: Just draw something on the f-ing screenOCamlcore Planet
  • La filosofia berlusconianaAntonio Di Pietro
  • Matías Giovannini: Monads are Plug-insOCamlcore Planet
  • Matías Giovannini: Monads are Plug-insOCamlcore Planet
  • Italy angered by 'fascist' labelBBC News | Europe | World Edition

My Last.Fm

Recent Posts

  • Ascoltare Radio Monte Carlo… con Linux
  • Holiday!
  • Vacanze!
  • Ritardo Cronico
  • PyCon Due
  • Sending emails via Gmail with Objective Caml
  • BBC article on Italy and mafia

Old posts

  • August 2008 (1)
  • July 2008 (2)
  • June 2008 (1)
  • May 2008 (1)
  • April 2008 (2)
  • March 2008 (1)
  • January 2008 (4)
  • November 2007 (2)
  • October 2007 (6)
  • August 2007 (2)

Blogroll

  • Alex
  • Andrea
  • Benji
  • Dome
  • Gigi
  • Ilaria
  • kOoLiNuS’s blog (English)
  • kOoLiNuS’s blog (italian)
  • Le ricette del secco
  • Roberto Gastaldi

Make me a present...

My Amazon.com Wish List
My Hoepli.it Wish List
rss Comments rss valid xhtml 1.1 Viewable With Any Browser design by jide