Python's Valentines Day Gift to Clojure

Tue 14 February 2012
  • ANN tags:
  • clojure
  • project
  • python published: true comments: true

Inspired by meh's Ruby-Clj module, I created the python equivalent "pyclj" last weekend. Pyclj is a clojure literal reader/writer for python. It enables data exchange between python and clojure, in a clojure-native way. It's Valentines Day today, I'd like to release it as the gift of python to clojure :)

The API is very simple. It's all like python's data modules (json, pickle)

[cc lang="python"]
import clj

clj.loads("[1 2 3]")
clj.dumps({"a":1, "b":2})
[/cc]

Clojure types are mapping to python data structures :

Clojure Python
list list
vector list
set set
map dict
nil None
string string
int int
float float
boolean boolean
char string
keyword string

But how we win clojure's heart from ruby?

We are faster.

Considering clojure literal below:
[cc lang="clojure"]
[1 2 3 true false nil {:a 21.3 :b 43.2} "Hello"]
[/cc]

Comparing ruby-clj(0.0.4.5, ruby 1.9.3p0) and pyclj(0.1.3 python 2.7.2):
[cc lang="ruby"]
require 'clj'

s = "[1 2 3 true false nil {:a 21.3 :b 43.2} \"Hello\"]"

t1 = Time.now()
for i in 0...10000
Clojure.parse(s)
end
puts Time.now()-t1
[/cc]

[cc lang="python"]
import clj
import time

s = "[1 2 3 true false nil {:a 21.3 :b 43.2} \"Hello\"]"

t1 = time.time()
for i in range(10000):
clj.loads(s)
print time.time()-t1
[/cc]

The result:
ruby: 13.451157809 python: 0.712423086166 Edit 20120216 13:30
ruby-clj 0.0.5.3 has resolved the performance issue :)
The new result ruby-clj/0.0.5.4 Vs pyclj0.1.4 (on my laptop):
ruby-clj: 2.044872364
pyclj: 1.19659209251

The project is hosted on github. Feel free to join the development and enhance it.