Fork me on GitHub

Swiss Tournament in Ruby 0

Being a chess player (not a very good one), I’ve always been intrigued by Swiss Tournaments. They are so practical, and ensure that even a lowsy player like myself, can play the same number of rounds as any other player. That’s being inclusive!

I’ve played some knock-out tournaments (to me it meant being kicked off in the second or third round), and, given their nature, not-so-good players tend not to attend these tournaments (since their fun will, almost surely, end before long).

Well, to solve a very similar problem, but not in any game championship, a co-worker suggested we could use a Swiss Tournament system. I liked the idea, but not being sure it could really solve the problem, I had to quickly implement something to test our data with… so Ruby to the rescue!

In no time we were up and running, and apart from minor issues that were being fixed along the way, I guess it’s a pretty good implementation. You can checkout the code to get a feel of it. Of course, it doesn’t follow any rules from any Chess or Go association (Wikipedia, after all, was my guide here), but it serves our goal. Being a “proof-of-concept” code, feel free to improve it (just tell me about it, will you?).

Real Life: Programming is about logic and simplicity 2

Why is it so hard for non-programmers to understand mutex locks?

No… I am not asking non-programmers to understand programming at all. But concepts that we use in programming that came from real-life in the first place? Come on… Programmers learned from real-life, not the other way around!

Let me explain: at the hospital, after a not so clever renovation, we ended up with an employee restroom that is far from work place (in fact, almost in another department). Most of the times it is occupied… So it takes a trip to the restroom just to learn that it cannot be used at that moment! In a busy morning, one of those trips is just what one can do!

As a programmer I suggested a mutex lock: change the restroom door lock and make only one copy of the key, that should be kept in a common place and returned after being used. If you want to use the restroom and it’s occupied, just learning the key is not there is enough to save a useless trip.

Come on… that is not a hard concept! As I said, we, programmers, learned it from real-life, in settings just as the one I described above!

Well, I just got the key (“acquired the lock”, in programmer speech) and went to the restroom. When I unlocked the door I was surprised by someone already using it! How embarassing! Someone had the smart idea of making a copy of the key! What part of the mutex concept did people not understand?

Luckily, most computational mutex algorithms prevent “lock cloning”... :-)

Debian-RS and Vincent Danjean 0

The day before yesterday I learned that a fellow Debian Developer was visiting Porto Alegre Federal University to do some work on Parallel Computing: Vincent Danjean. People from local user group organized a last minute get-together at Cavanhas (that served as last meeting of the year) and we had the most pleasant time. Guaraldo registered the moment with his cellphone camera:

Vincent is flying back to France today or tomorrow. Hope he had a great time in Porto Alegre and have a safe trip back home.

Looking for a new programming language to learn 21

I know it has been a long time since my last post. I am sorry about that, but life has it’s complications every now and then (as you know)... Well, on to the article.

Recently I had to reimplement in C a prefork server I wrote in Ruby for an internal project at Propus. Not that the Ruby version wasn’t enough (after all, although being in Ruby, I was using Unix plumbing, much in the fashion Ryan tell us about in the – now famous – I like Unicorn because it’s Unix article)... The problem is that, in one of our clients, the only version available for Ruby was 1.8.1.

Yeah… I know… But we were not allowed to upgrade and, although it didn’t seem at first, the same server presented a nasty memory leak in 1.8.1 that was not present in 1.8.7 and 1.9.1. I still don’t know where the problem is… I suspect some of the C-to-Ruby glues around TCP sockets might be blamed, but after a couple of days trying to figure it out, I decided it was easier just to reimplement it using C.

It actually took less than a day to get the C version going… nothing fancy and, apart from memory footprint, just the same functionality and about the same speed of the Ruby version. But it was enough to remind me I really don’t like all the scaffolding one has to raise in order to make something useful in C. It’s not just a matter of SLOC (of course, C version was more than 3 times longer than Ruby one)... I am talking about all the manual memory management, pointer operations and the disgusting experience of dealing with strings in C. I know some people are addicted to that sort of thing like heroin, but to me it just slows development.

This experience made me think about learning a second compiled programming language. I do some Perl, a lot of Python and (of course) most of my work in Ruby, but those are all interpreted languages. For compiled languages I always resorted to C… So I am officially looking for a language to learn.

So far, the best candidates are OCaml (I got a little excited about JoCaml a few months ago, now I might get serious about it), Haskell, Lisp, Objective-C, Ada, and Vala. Of these, I’ve been reading a lot about OCaml… It seems a fine and expressive language, with decent foundations, object-oriented extension, broad standard library and (with JoCaml) concurrency… Also it might give me the proper excuse to finally wrap my mind around a functional language!

People keep me pointing to Java and Erlang… Well… for using Java I would much prefer using JRuby. Erlang, ITOH, has a weird syntax (at least to me) and it seems much of what makes it great will, eventually, be part of Ruby (or already is using libraries) – either that or I’ll just wait for Reia to be ready. Besides, neither can be compiled to native code (ok, that argument can be stretched both ways, so just ignore it).

So, what do you think? Any advice?

XMPP4R-Observable now on GemCutter 2

Just a quick update: XMPP4R-Observable is now on GemCutter. That’s due to GitHub disabling gem building, and although everybody can get the source from GitHub as usual, those who want to quickly install it using Rubygems can do:


bash$ gem install xmpp4r-observable -s http://gemcutter.org

Happy Hacking.

Ruby versus Python 12

This is not another rant to praise one in spite of the other (an everybody knows I love Ruby, so it would not be impartial), but sometimes people seems to live in another world and do things for the wrong reasons.

I just read this blog post by Kanwei Li in which he gives 2 or 3 reasons he ditched Ruby in favor of Python. First of all, both are great languages and, although I favor Ruby, I use Python for some projects and they are not all that different. Of course, everyone is free to choose which language one favors, but Kanwei seems to be “ditching” Ruby out of not knowing much about it, or out of preferring one style over the other…

His first “reason” is that in Python white spaces matter. I used to think this is just a matter of style, but every now and then mandatory alignment hurts me (just try to put together a code generator and you’ll notice it). Although my code is always correctly aligned, I like that it’s done so because I want it that way, and not because some language demands it. Rants and more rants have been written about Python’s mandatory alignment (or other languages lack of it), and I am not going through all of it… Just I don’t think it’s a good reason to ditch Ruby…

After, he makes a big deal out of Ruby’s ternary if. As written by him, he prefers

#python
if len(a) > 0:
        v = a[0]
        a = a[1:]
else:
        v = None
over Ruby’s ternary if

#ruby
v = a.empty? ? a.shift : nil
Hey! Come on… Ruby’s ternary if is not mandatory… It was copied from C just as a syntax sugar. You can do without it, just as in Python:

#ruby
if ! a.empty?
    v = a.shift
else
    v = nil
end
Better yet! you can use if’s return as v value:

#ruby
v = if ! a.empty?
    a.shift
else
    nil
end

How beautiful is that!

Python lacked ternary if for a long time, and when it finally acquired one via PEP 308 its syntax was made different from every other language! Although I don’t think that is a problem, some people might think it would be better not reinventing the wheel.

Next, Kanwei goes over a famous “problem” of Ruby: the lack of a sum method for Array. I admit it’s strange, but that is completely coherent: Ruby’s Arrays are ordered collection of objects and not mathematical arrays. How do you sum objects that are not numbers? Many different people will have many different answers to that, so Ruby leaves this decision for the programmer and provides basic methods to deal with collections of anything (that can be used to apply sum to numbers, if wished). So, in Ruby you have to use Array#inject to perform a sum:

[1,2,3].inject(0) { |sum, value| sum + value }

Array#inject (actually Enumerable#inject) was borrowed from Smalltalk and allows you to loop through an array, building up an “accumulator value” as you go. When it’s done, the final value of this accumulator is returned. Very useful for combining array elements, whether by summing them, building up a pretty display string, whatever. In the example above, I am initializing the accumulator with 0.

If you use Array to mathematical operations and you want your arrays to work that way, you can always add a sum method to Array class:

class Array
    def sum
        self.inject(0) {|sum, value| sum + value}
    end
end

Maybe it would be better if you just use Arrays as containers (as it was intended to) and implement that sum inside your own class… I completely agree with Reg Braithwaite here.

Kanwei also mention Python is faster than Ruby. That is true, but was “more true” some time ago. First of all, Python is older and has had more time to improve its speed. Ruby, ITOH, just now acquired a good VM and improvements to it finally can run parallel to improvements in the language itself, so I am expecting this to be less true every release. Python is already not getting much faster between releases, unlike Ruby (the differences between 1.8.7 and 1.9.1 are really impressive!). IMHO this is not a good reason to choose one instead of the other: if you really need speed, go for C :-)

Now this is something I find interesting Kanwei has mentioned: “Python is more production ready”. He argues that Google is using it, so it must be good. Well… I cannot argue against that: Google is really using Python. But IBM, Oracle, EA, Cisco, Siemens, etc are using Ruby… so that is just a matter of preferring one or another company. Both are production ready… I agree, though, that Ruby 1.9.1 has many differences from 1.8.7, and that that may be seen as some inconsistency, but Python also has changed a lot since its 2.0 version, for that matter. And the changes to Ruby brought many benefits… I think they worth it.

At last, Kanwei compares Python and Ruby docstrings. Here I also have to agree with him: Ruby docstrings sucks. Actually that’s why everybody uses rdoc instead (and that is much more powerful than Python’s docstrings). Again, I don’t think that is reason enough to ditch Ruby (actually, the existence of rdoc, rubygems & friends should bring people to Ruby instead), but that is a matter of personal taste.

Surely, Kanwei’s reasons were easy to argue against. There are areas were Python shines much more than Ruby (and vice-versa), but those Kanwei mentioned are not among them.

I think both languages are powerful enough, and both are way better than Perl or PHP, so either one you choose would be fine. Better if you don’t have to choose and use both ;). If you have to, ITOH, pay more attention on how you feel while coding in each one, and not to some cheap reasons such as above. If you are a programmer, what matters most is that you’ll spend a lot of time coding with any given language… let that be something pleasant then.

Code testing coverage 0

I like building tests for my code. That is not an old habit, it’s just something I’ve been developing in the recent months or some few years. No, I am not doing TDD (although that doesn’t sound like a bad idea): I just build tests after I code as a safeguard – to be sure I haven’t broken anything. I suspect there are more programmers like myself than those using tests as part of a TDD (BDD, SDD, etc) approach, but that is just an opinion.

Well I just recently became found of code coverage estimates and tools, and rcov is such a nice tool that sometimes I just find myself building tests just to “please” it. I also suspect there are at least a bunch of people that do the same. Here are the results of the test coverage of one of my projects:


spectra@rohan:~/work/xmpp4r-observable$ rake rcov
(in /home/spectra/work/xmpp4r-observable)
rm -r coverage
Loaded suite /usr/bin/rcov
Started
........................
Finished in 70.995814 seconds.

24 tests, 97 assertions, 0 failures, 0 errors
+----------------------------------------------------+-------+-------+--------+
|                  File                              | Lines |  LOC  |  COV   |
+----------------------------------------------------+-------+-------+--------+
|lib/xmpp4r-observable.rb                            |   648 |   414 |  61.4% |
|lib/thread_store.rb                                 |    58 |    39 |  87.2% |
|lib/observable_thing.rb                             |   187 |   118 |  91.5% |
+----------------------------------------------------+-------+-------+--------+
|Total                                               |   893 |   571 |  69.4% |
+----------------------------------------------------+-------+-------+--------+
69.4%   3 file(s)   893 Lines   571 LOC
spectra@rohan:~/work/xmpp4r-observable$

Sure it’s tempting to get more of lib/xmpp4r-observable.rb covered, isn’t it?

EeePC: Surviving liquid spills - phase II 3

About four months ago I described how my EeePC survived the spilling of orange juice over its keyboard. No! I never spilled anything on it again, if that’s what you’re thinking… But I noticed that some keys (those that got more juice on) began to malfunction. At the beginning I paid no attention to it, hoping that it would just go away, but, eventually, they just stop working… And those are not just unimportant keys… I am talking about arrows and the forward-slash (/) keys in the lower right corner of the keyboard!!! How could I survive without those keys, without a quick access to my bash history and vim search?

Well, I began googling around and found some good advice. Everything that made sense regarding how to cleanup my keyboard I compiled and, when I was enough confident it would work, I just follow the procedure I’ve devised. This is what I did:

  1. After turning it off and removing the battery, I removed the keyboard. Check the instructions I linked in my previous article for some pictures on how to do it.
  2. I poured 500 mL of distilled water in a clean plastic box (I bought 1L for BRL 20 at a local pharmacy), added enough dish detergent to make some foam (I was careful to select a non-biodegradable one) and drowned the whole keyboard in the solution.
  3. For the next 30 minutes I pressed and released the affected keys over and over again. My intention was to dissolve anything that might have remained from the orange juice.
  4. I left it soaking in the solution for the next 12 hours.
  5. The next day I got the keyboard out of the solution and used current tap water to remove any detergent still left in it. This might have took about 10 minutes.
  6. Our tap water is really clean, but its hard, and I would not like to remove any juice from the keyboard just to add some minerals that might have the same effect, so after I was certain all the detergent was removed, I left the keyboard in the remaining of the distilled water for another 12 hour soak.
  7. After that I just removed the keyboard from the soak, dried it a little bit using a paper towel and left it to air-dry (away from the sun). I don’t remember how long it took, but I believe not more than 4 hours… Those were hot days… Anyway, I was really sure it was dry.

When I plugged it back, surprise! All keys are working again! Of course, that was just what worked for me… Best advice still is: Keep liquids away your EeePC!

I hate Joey Hess 1

OK. I don’t actually hate him… It’s just that I also wanted to buy a Palm Pre and install Debian in it... But living in Brazil and using GSM makes that really hard today. Maybe he can advance me some info: can I migrate my data from my Palm Treo 650 to a Palm Pre? (Anyway, if that is not possible, I’ll just run J-Pilot inside the thing ;) ).

Good luck with your new toy. Keep us posted!

FISL10 day 4 0

Today I finally went to attend some lectures. I decided that since I was to give one and Arena was over, I was allowed to just sit there and pretend I was just attending FISL10 and not organizing it.

Well, first things first. My lecture was on my fork to implement PubSub in XMPP4R-Simple. Nothing really fancy, just describing what we’re doing in Propus with that fork. I can upload the slides if somebody asks to, but everything there’s to know about it is in the code.

After having had lunch with some friends and talking with others I haven’t seen since last year (and that I still hadn’t seen in FISL), I went to the Key Signing Party we organized. That went fine. We had 114 different keys sent, but just 42 showed up for the party (including my 2). I don’t know what is the average in other parties, but I think it was enough given we had the competition of other 12 other activities, and it was a first-time experience.

Later I attended to High-Speed Cryptography and DNSCurve lecture by DJB, which was a really amazing talk. I was moderator for a panel between him and Frederico Neves on Wednesday (as I told you before), and I was present when they debated about NSEC3 and how prone to enumeration attacks it is. Frederico challenged DJB to enumerate NIC.br’s NSEC3 testing network under sec3.br. In this talk he told the audience that he enumerated 23 of the 26 hosts in that network just using desktop-level computers (and not some fancy Gigaflop crypto-breaker station)... that is until he had to prepare the last talk. (I am guessing, but he described the technique here)...

After I just learned how to Fail Faster and Succeed Sooner with Michael Tiemann, another good lecture in which Tiemann told how Fedora is coming from failure to failure until the successful last releases (and how did that tied up with RHEL strategy).

Then I went to the Panel on Electronic Frontier, one I was most curious to go. Really interesting panel talking about freedom in the Internet and how we, as citizens, have to oppose anything that takes away this freedom. One of the many good ideas I learned from that panel was how to fight against traffic shaping (one of the many things almost all ISP does in Brazil and don’t say a word about): building our own Community ISP. I found it an interesting idea, but have to research on how it fits in Brazilian legislation (it may even be unlawful).

My initial intent was to escape before the end of that panel in order to attend the session were DJB would announce this year’s Programming Arena winner group. But before I could get out, Marcelo Branco called me to join the panel in his place, since he had to take care of the proceedings to FISL10 final session. So that was it. I still have to ask Organization Committee who own the Arena…

The final session was kind of crazy. The usual announcements of numbers and a presentation of a piece of President Lula speech. Jon ‘maddog’ Hall recorded a video of the audience inviting Linus to come. It were also announced that FISL11 will be in Usina do Gasômetro. I am not too excited about this place, and I still doubt it’ll be ready to hold an event such as FISL… I’ll just play “wait and see” ;)

As usual, FISL10 most lasting “side-effect” was to see old friends. I am already missing people I am sure I’ll just see again next FISL

I’d like to thank all the people that came to FISL10. Hope you enjoyed and come back for FISL11.

FISL10 day 3 - the day I met the President 0

I am a little behind on the reports on FISL, but so much has got my attention during it that blogging was just put in second. I will catch up today, hopefully.

So, during the night of day 2, all those measures I mentioned had to be put in place. That was when I learned that I was suppose to be one of the selected lecturers to meet President Lula in private, representing small free software companies (how awesome!). Others include Peter Sunde, Bdale Garbee, Jon ‘maddog’ Hall, Richard Stallman, Marcelo Tosatti, Pau Garcia-Milá, Sérgio Amadeu, Marcos Mazoni, Ana Amorin, Bruno Souza, Marcelo Branco, Sady Jacques and Mário Teza.

I was told to dress accordingly… I asked what “accordingly” meant (we were in a free software event: jeans and t-shirt seemed “accordingly” to me)... but no reasoning was taken: I had to wear a tie.

Next day I went to PUCRS early, in order to prepare some lines and gather some data I could mention to the President. Something like Free Software adoption rate, which is around 26% per year, or the 134 million USD that this market moved just last year. I would also ask the President to enforce the Free Software priority in training programs sponsored by the Federal Government. I knew I would not have time for a speech or the like, and that this would be more an informal meeting… This, though, was even more difficult to prepare (I would prefer a speech!).

So, I got my pin and went to the Arena to wait for the scheduled evacuation, after which, I was told to wait in the private room, for the President arrival. I was there with the rest of the selected lecturers, so I couldn’t see when he arrived. People told me that he went all around the exposition area, and the user group area, shaking hands and being photographed with everybody there. He even entered the Programming Arena and told the contenders they were “genius” (after all, the Programming Arena was his idea, 3 years ago). People in Debian booth told me he entered the booth and wore a Debian hat…

When he finally entered the private room, everybody had about 2-3 minutes with him in the middle of a circle. Presidential photographer took lots of pictures we were told will be sent to us later this week, but of course some of us also took our own pictures. Some of those are below, taken from Sergio Amadeu’s camera:

Marcelo Branco, President Lula and I

Peter Sunde, Sérgio Amadeu, Marcelo Tosatti - in blue, behind Sérgio -, Richard Stallman, I, Bdale Garbee, Dep. Paulo Pimenta, and President Lula

Jon ‘maddog’ Hall gave him a Tux pin and a DVD with a animation produced only with Free Software (I cannot recall the title). Richard Stallman gave him a printed version of his book. Marcos Mazoni gave him a small totem with the stamp celebrating 10 years of FISL (the stamp was an idea I had two years ago but that we couldn’t do by ourselves – Mazoni’s SERPRO had the same idea and actually did it), and all others (including myself) just told him what we where there for. For me, in particular, he asked where I was born and how I went from being a doctor to own a free software company. I had the impression somebody already told him about me beforehand (Mário or Marcelo, for sure). I told him I was doing both right now… he smiled, hugged me and went on to the next of us.

After that, we were told to take our places in the audience room (FISL3 room in the map), where we heard Marcelo Branco, FISL10 coordinator, Dilma Rousseff, minister of Civil House (and appointed to succeed Lula), and President Lula. It’s easy to find this audience in youtube. Most interesting part of Lula speech, IMHO, can be loosely translated into English as:

I remember the first meeting we had at Granja do Torto [which is the presidential country residence – similar to Camp David, but less aristocratic], in which I understood absolutely nothing about what these people were discussing, and there was an enormous tension between those defending the adoption of Free Software by Brazil and those defending we should just do what we always did – remain the same, buying and paying for others’ intelligence. Thanks God, in our country, the decision to adopt Free Software prevailed.

He also said many things that pleased the audience. People raised a banner asking him to block Azeredo’s bill, and he said the bill was equivalent to censorship and that in Brazil it is “forbidden to forbid”.

After those speeches, he went to some other appointments, and the day 3 of FISL10 was over. I just wish that, if any President comes to FISL again, we’d be warned in advance, so we can prepare the map accordingly, and not have to run last-minute preparations. All in all, a great participation. I think all the hassle we had because of his coming were hugely compensated by what he said.

FISL10 day 2 1

Today was a busy day for the Organization Committee. As faw told me: some people from Debian haven’t even see me yet… But all this have a reason: President Lula confirmed his coming and all his security personnel flooded FISL and asked a lot of things from the Committee.

We had to “partition” FISL. They draw a red area in our map:

and demanded that only 700 people (less than 10% of the people!!!) could access that area. We had to work all day, organizing a list of 400 people that had to work in there (people from booths, user groups, programming arena, robotics festival, etc). Those will receive a special pin. The other 300 spots, will be served in a counter that goes up and down (after 300 has entered, the only way of another person get in is someone getting out).

By noon, federal police will evacuate the red area, and will screen it (I believe looking for bombs or something like that). At 13h only those wearing the pin and the circulating 300 will be allowed back.

President will arrive by 15h. He’s scheduled to visit the red area (including the programming arena – Yeah!), then he should go into a private meeting with selected lecturers and people in Brazilian Free Software Community. Afterwards he should, himself, give a lecture in Room FISL3 (also marked in red). President will leave for other appointments and, hopefully, FISL will go back to normality.

So, all the demands from Federal Police and from the community took all day long to settle. I couldn’t attend to the sessions I wanted, nor hang out with people from Debian… Hopefully, I will not be dragged by the Organization tomorrow so I can give my lecture and attend the key signing party on Saturday… Don’t get me wrong: this is a great day for FISL (and Free Software in general) – a President of a large and democratic nation is acknowledging our existence and labour. But having to restrict access in a part of FISL is not something that pleases me (and I am sure doesn’t please the rest of the Committee). Anyways… on to day 3.

FISL10 day 1 0

FISL10 began, as usual, with lots of people from lots of places packing PUCRS’ event center in Porto Alegre. This is indeed a special edition! The official numbers are not yet computed; as I write the counter reached 7168 attendees… But since the database has been running locally yesterday on, people that registered at PUCRS are not yet counted… Registration team will merge the databases eventually, but that’s not their top priority right now.

So, this year I am not helping TVSL, which is being taken care of by Luis Felipe instead. He’s been doing a good work and there could be nobody else better than him. My quality of life increased dramatically by letting TVSL in Felipe’s hands… Maybe this year I manage to actually attend to some sessions, and to hang out with Debian guys.

This year I will partially take care of the Programming Arena. The challenge was revealed today and 11 “heroes” are, right now, trying to respond to it. It has been requested that they implement DNSCurve. They could have no better coach at this than D. J. Bernstein, who has been helping them both at the Arena and through the Arena private mailing-list.

Earlier, DJB also were in DNSCurve x DNSSEC panel, one of the most interesting technical sessions so far (bias warning: I was moderator for it). DJB and Frederico Neves (from NIC.br) exchanged arguments exposing interesting details of both secure DNS alternatives.

Also, today we got the confirmation from Brazilian Presidency that the President will attend our event. It will be on Friday, and I am too excited to hear what he has to say about free software government policies. (Apparently, apart from Linus, we managed to get everybody to show up in FISL ;) ).

I am also taking care of the Key Signing Party. This has been a rather good experience, since it requires almost nothing from me (except preparing the keylist and showing up at the scheduled time and place) :-).

See you tomorrow, at FISL!

Keysigning Party at FISL10 0

We’ll be holding a Keysigning Party at FISL10. This will be a good opportunity to renew my key, given I’ve been using it since 2001 and it’s an old 1024 DSA key.

I have to thank Aníbal Monsalve Salazar and Alexander Wirt for sharing their expertise in organizing this kind of event. More information on the KSP can be obtained from the announcement.

Inspiring picture 3

Just this week I went to the movies to check the latest incarnation of Cap. Kirk, Mr. Spock et al. It really was a great movie… I was expecting less, I have to admit. But the movie was not bad at all…

With all that “space and beyond” thing in my mind, yesterday I was surprised with a greatly inspiring picture taken from Hubble Florida, before STS-125 reach Hubble (thanks Florian Weimer for the heads-up):

How inspiring can that be?

You can find the original here, along with the explanation of how and when it was taken.