Save the ions!

Say hello to Python Korea!

Right after PyCon (victory!), I went to Seoul. Before leaving, I figured “Hey, they may have a Seoul-Python or something!”, so I looked around, and found a forum: python.kr.

I posted a message there introducing myself and asking if anyone wanted to meet to discuss our favorite language. I got a few responses, and I met 7 of them a few days ago.

What surprised me at first was how similar to our community they all were: they enjoy beer, and more than half of them were in startups, one similar to groupon, and the other one is kind of a mix between foursquare and kik.

One of the first things I asked was if they had regular meetings, much like we do, and I was surprised that they didn’t. They explained that in 2006, everyone wanted to do Python, so they held a conference in Seoul that ended up attracting over 1500 programmers! They continued to explain that Python was now the mainstream language, it is even taught in their universities.

They concluded by saying that a community like ours was no longer necessary, but a FAQ forum like they have now was more than enough.

I think we have a lot to do to reach that point, but it certainly gave me hope!

CalDAV slides from Montréal-Python

Someone just said maybe he’d like to see my slides from my flash presentation about CalDAV at Montréal-Python.

Here it is.

Newton’s method for square root

A while back, my friend Yannick showed me that the source code for Return to Castle Wolfenstein had been made public.

The part that interested him the most was the way they (he knew the name of the developer, I forgot it, sorry) calculated square roots.

I’ll let you have a look at it first:

/*
** float q_rsqrt( float number )
*/
float Q_rsqrt( float number ) {
	long i;
	float x2, y;
	const float threehalfs = 1.5F;

	x2 = number * 0.5F;
	y  = number;
	i  = *( long * ) &y;                        // evil floating point bit level hacking
	i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
	y  = *( float * ) &i;
	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//	y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

	return y;
}

The first thing you may notice is that this looks like it doesn’t make any sense…

The second thing you might notice is that this is in O(1) order, which is very peculiar.

The third thing you probably notice is that comment saying what the fuck?

It took my quite some time to understand what was being done in that method, but it became pretty clear that it is loosely based on Newton’s method, which works by providing an estimate on the resulting value

As an example, consider the following line of python:

a = a - ((a ** 2 - o)/(2 * a))

This is the basic implementation of Newton’s method (considering a the estimate and o the number from which we want the square root), and usually, with about 5 iterations, you get the correct value at 8 or 10 digits:

>>> o = 612.0
>>> a = 10.0
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
35.6
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
26.395505618
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
24.7906354925
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
24.7386882941
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
24.7386337538
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
24.7386337537
>>> a = a - ((a ** 2 - o)/(2 * a)) ; print a
24.7386337537

With the example above, you can see that in 4 iterations, we have a correct value up until the 7th digit, and by the fifth iteration we are correct up to the 10th digit!

Now the really puzzling part about the implementation in Return to Castle Wolfenstein is that the guess is so accurate (for the values commonly passed), that only one iteration is enough.

Pretty cool, isn’t it?

CalDAV at Montréal Python!

I’ll be doing a short presentation on my python CalDAV library tomorrow at Montréal Python!

I’m really looking forward to have some questions about it, maybe find a few contributors. I’ve already had some, as you can see on bitbucket, but for this library to be a success, we’ll have to do more than bug fixes, especially since everyone interprets the RFC differently!

Just come over and have fun listening to the talks, and enjoy a beer afterwards!

For more information, please see: http://montrealpython.org/2010/10/mp-16/

Presentation at Montreal.rb

Hey guys,

I’ve been extremely busy this week (I’m working on a very large e-commerce system at the moment, and I’ve just been elected VP of Montréal Python), so I couldn’t find any time to post this sooner, I’m sorry!

On tuesday, I was invited to give a speech at montreal.rb, to do a rehash of the famous Ruby on rails’ 15 minute blog, but using Python and Django. I have to admit, my livecoding demo took a bit more than 15 minutes, but I think the ruby crowd enjoyed it, I got many interesting questions afterwards!

Thanks to everyone who’s been there, I hope Montreal’s user groups will continue sharing!

I’ve uploaded the source code for the blog I built live, download it here.
To use it:

$ tar jvxf mtlrb.tar.bz2
$ cd mtlrb
$ python bootstrap.py
$ bin/buildout -c devel.cfg
$ bin/django runserver
$ firefox http://127.0.0.1:8000/
$ firefox http://127.0.0.1:8000/admin/

Introducing (python-)caldav

During the last few days, I’ve been busy writing a CalDAV client library for work. This is a requirement because we are going to integrate calendars from various sources in one of our websites.

CalDAV is clearly the way to go, but as far as libraries go, the only one I’ve been able to find was CalDAVClientLibrary, which, quite frankly, scares me. I’m already familiar with CalendarServer, and I can’t qualify that as “lightweight”, “simple” or “easily installable”.

CalDAV

In case you don’t know what CalDAV is, it’s an extension to webdav for calendaring, and is defined in rfc4791. If you don’t feel like reading all of this, it’s mostly HTTP with XML requests and responses, but not always, which is why it gets kind of weird…

Example requests

Creating a calendar:

MKCOL /calendars/pythoncaldav-test HTTP/1.1
Host: example.com
Accept-Encoding: identity
Content-Length: 249

<?xml version='1.0' encoding='utf-8'?>
<D:mkcol xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:">
  <D:set>
    <D:prop>
      <D:resourcetype>
        <D:collection>
          <C:calendar-collection/>
        </D:collection>
      </D:resourcetype>
      <D:displayname>Yep
    </D:prop>
  </D:set>
</D:mkcol>

Deleting a calendar:

DELETE /calendars/pythoncaldav-test HTTP/1.1
Host: example.com
Accept-Encoding: identity

caldav

The version I just released is the very beginning of the project: it has basic functionality, but lacks many things, such as error handling and full support for the protocol. The documentation also needs a lot more work.

Example

I tried keeping the API very simple, and close to what an ORM would be used like, here’s an example:

import caldav
from caldav.lib.namespace import ns

# Principal url
url = "https://user:pass@hostname/user/Calendar"
vcal = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Example Corp.//CalDAV Client//EN
BEGIN:VEVENT
UID:1234567890
DTSTAMP:20100510T182145Z
DTSTART:20100512T170000Z
DTEND:20100512T180000Z
SUMMARY:This is an event
END:VEVENT
END:VCALENDAR
"""

client = caldav.DAVClient(url)
principal = caldav.Principal(url)
calendars = principal.calendars()
if len(calendars) > 0:
    calendar = calendars[0]
    print "Using calendar", calendar

    print "Renaming"
    calendar.set_properties({ns("D", "displayname"): "Test calendar",})
    print calendar.get_properties([ns("D", "displayname"),])

    event = caldav.Event(client, data = vcal, parent = calendar).save()
    print "Event", event, "created"

    print "Looking for events after 2010-05-01"
    results = calendar.date_search("20100501T000000Z")
    for event in results:
        print "Found", event

Links

I released caldav and its documentation on pypi, and the code is on bitbucket:

Contributions are welcome!

Portrait de blogueurs

J’ai été interviewé pour Portrait de blogueurs hier soir!

Django Debug Toolbar

I wish for two things:

  • That there was some kind of RT function with rss feeds (the only feature I like about twitter)
  • That Django Debug Toolbar is merged in django 1.2+

 

via Planet GNOME

BTW, I can’t believe no one else on Planet Django mentioned it yet!

When a crazy person talks to bots…

Allright, fine, it was me…

I have just discovered that Google Translate can be used through XMPP! Details in this article about translation bots.

The odd thing is, once I have a “contact” in my contact list, I don’t expect it to be a bot (yes, I would suck at being a judge for a Turing test), so I get carried away…

(15:40:36) ion: you're the best, mr. googlebot!
(15:40:36) en2fr@bot.talk.google.com: tu es le meilleur, Monsieur. googlebot!
(15:40:48) ion: i'm not a googlebot!
(15:40:49) en2fr@bot.talk.google.com: Je ne suis pas un robot Googlebot!
(15:41:02) ion: yes you are!
(15:41:03) en2fr@bot.talk.google.com: Oui vous êtes!
(15:41:14) ion: hey!
(15:41:14) en2fr@bot.talk.google.com: hey!
(15:41:21) ion: you're making me angry!
(15:41:22) en2fr@bot.talk.google.com: vous me mettre en colère!
(15:41:36) ion: quit repeating all I say!
(15:41:36) en2fr@bot.talk.google.com: arrêter de répéter tout ce que je dis!

Anyways, have fun with the bots!

The Chatroulette phenomenon

I’m fairly certain that by now everyone knows what Chatroulette is, so I won’t introduce the concept.

I just wanted to mention what has been happening over there in the last couple of months. About 2 months ago, I opened it up, and just saw a few people staring at their webcams, but mostly lots of dudes showing off (or touching) their junk. That was… let’s say unpleasant.

However, since then, I keep seeing these videos of people using chatroulette very creatively, and most of those videos went viral (or somewhat viral), so I think it shows that people are excited about the possibilities offered by chatroulette.

A few examples

Young Rival made a music video, which is pretty awesome.

Everyone now knows Merton

Ben Folds made the Ode to Merton

Then there’s this dude, who looks alot like Nicolas Wright

Anyways, those are only a few of my favorites, and you can find a lot more on youtube