#2008-07-02T23:25:54-03:00 @xml @programming @python XML builder for Python In my spare time, I've been playing with an opensource project called GAE-REST which aims to offer a REST Atom API for Google App Engine applications. Django's templating system is often a pain to work with, and XML libraries for Python are also not stellar, so I decided to take advantage of Python's with statement and wrote a XML generation library somewhat similar to Ruby's Builder. Lots of room for improvement, but it can already produce valid Atom feeds:

  from __future__ import with_statement
  from xmlbuilder import builder, element

  xml = builder(version="1.0", encoding="utf-8")
  with xml.feed(xmlns='http://www.w3.org/2005/Atom'):
    xml.title('Example Feed')
    xml.link(None, href='http://example.org/')
    xml.updated('2003-12-13T18:30:02Z')
    with xml.author:
      xml.name('John Doe')
    xml.id('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6')
    with xml.entry:
      xml.title('Atom-Powered Robots Run Amok')
      xml.link(None, href='http://example.org/2003/12/13/atom03')
      xml.id('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a')
      xml.updated('2003-12-13T18:30:02Z')
      xml.summary('Some text.')
  print xml

Contributions over at Github are welcome.

#2008-04-08T05:27:50-03:00 @http @programming @rails Insignificant DRY Rails tidbit When designing a REST API, it makes sense to just return 400 Bad Request if a parameter is missing. This is a pattern I've seen used a lot at work and I noticed we were running into a bit of repetition throughout our code. Also very common is setting optional parameters as empty strings to avoid nil value woes. So here's a bit of DRY goodness to avoid writing more than a single line of code to do it:

  # puts this in controllers/application.rb
  def optionalize(*args); args.each { |arg| params[arg] ||= '' }; end
  def required(*args); args.each { |arg| raise BadRequest unless params[arg] }; end

  # usage example on controller actions
  required :name, :email
  optionalize :address, :country, :phone_number

As I said, pretty insignificant but might help making your code look even tighter.

#2008-03-25T09:27:12-03:00 @rants @economy The Matrix has you A random internet poster who identifies himself as Rodney Paradox (maybe that's his real last name, who knows) posted a rather brilliant poem recital, or should I say, a protest and a call to arms. His ingenious use of poetry keeps you watching him while he eloquently opens your eyes about what is wrong with our world and delivers his call to arms at the end:

"[...] Stop paying for credit, stop paying the banker's tax, use interest free credit schemes instead, buy all your consumer materials second hand [...] Stop watching television news, and stop reading newspapers too... And finally, demand that the bankers give back the wealth they robbed from humanity. Too much to ask of you, too much for you to do? Go back to sleep then, The Matrix has you."

And stop reading magazines too! One of the top personal finance magazines in Brazil shares a bit of suspicious advice on this month's cover: "take advantage of cheap and widely available credit without compromising your investment capacity". Uh, sorry, the only ones who will be taking any advantage out of such arrangement are banks. After all, they can't have lots of people investing and saving money that could be otherwise given back to the "economy", right? Banks live and profit from the sustainable large gap between the people who are using their money and the people who are taking their money.

Unfortunately the majority of people in the world have become dependent on the rich few who run their countries and their economies. Rich people start losing money? Poor people lose their jobs (and the funny thing is, it starts happening from the inside). The average worker's life is so rough he is actually sometimes dependent on credit to make life bearable.

But just imagine what would happen if people massively started saving money for long periods of time instead of paying to use money they don't have? Banks would start paying interest instead. Just imagine actually saving up enough money to pay for your dream car what it is actually worth, instead of having to pay nearly as much as its value to banks. Just imagine cooking at home, losing weight, instead of spending half your sallary to go to McDonald's. Just imagine. You'll actually stand a chance of getting rich, and banks would slowly start losing their power, along with the governments that back them. I know that's a hard thing for all of us to do, but it's about time we started.

#2008-03-25T00:29:02-03:00 @http @programming HTTP debugging I have a major psychotic fucking hatred about HTTP libraries in general. Most of them, specially in Ruby, lack an easy way to actually log all HTTP traffic they generate. I was just trying out RestClient from the Heroku guys tonight and only after lots and lots of boring futzing I could get it to log requests.

Now, don't get me wrong, it's not like I just don't love to have my flow interrupted because of a useless and time wasting distraction. But it seems to me this should be a rather essential requirement for any library that issues HTTP requests and processes HTTP responses, right? Well, all I wanted was to test a simple REST API call. Ended up writing a short Python script that acts as a generic, raw HTTP client:

import sys, socket

def main(host, port, request_file):
  flags = (socket.AF_INET, socket.SOCK_STREAM)
  client = socket.socket(*flags)
  client.connect((host, int(port)))
  client.send(open(request_file).read())
  response = client.recv(1024*1024)
  while response != '':
    sys.stdout.write(response)
    response = client.recv(1024*1024)
  client.close()

if __name__ == '__main__':
  try: main(*sys.argv[1:4])
  except Exception, e: print e

You can just pipe the HTTP server's response to a file a be done with it. It doesn't do anything other than that, if it's a binary you'll have to manually dettach it from the HTTP response, ditto for gzip, but I find it great for writing quick tests and documentation.

#2008-03-21T12:51:59-03:00 @education @science @society Math and the brutalization of Science I have finally finished reading Mathemathics: From the Birth of Numbers. I had been pretty much reading a chapter a week since forever. It's about 1000 pages long and its greatest achievement is teaching you math from scratch, in the right order. What goes vastly unnoticed by most reviewers tho, is how strongly it scrutinizes our educational system, leading us to the conclusion it is brutalizing mathematical reasoning in the minds of young people. Our educational system wants math to be merely a skill, so people can find their way into being usefull in society.

Modern education doesn't realize how well it's succeeding in narrowing our chances of ever producing another Einstein. Suddenly scientific thinking isn't something we do casually, it is no longer part of our daily lives. It's part of the school. You're supposed to learn it to do well at tests. How are children supposed to like it when there's so much pressure on being skilled at it, on doing well at tests, on being admitted in a good college? You're not, you're supposed to loathe it and avoid it.

And the problem with school isn't merely pedagogical, it is a sociopolitical disease that has grown since the 19th century, as a counterbalance to protect the state from an ever increasing advanced society. To quote John Taylor Gatto in his "The Six-Lesson Schoolteacher" essay:

"School" is an essential support system for a vision of social engineering that condemns most people to be subordinate stones in a pyramid that narrows to a control point as it ascends. "School" is an artifice which makes such a pyramidal social order seem inevitable (although such a premise is a fundamental betrayal of the American Revolution).

The state doesn't want educated people. All it wants is useful people. I repeat that because it sounds vaguely important: your government does not want you to have a proper education. And they make sure that if our society actually get educated people, it should be only a small fraction of the people, either helpless to do anything to change that, or brainwashed enough to go back into the system, be a part of it and keep it going.

It's shocking to me how some otherwise smart people let this little fact go unnoticed. As we fight our leaders, as we fight them for a brighter future, we totally overlook one of the very foundations of our broken system: the educational system. And it's about time we do something to change it.

#2008-03-18T04:49:37-03:00 @webstandards IE8 and Web Standards Joel Spolsky just wrote a 25-page-long article about IE8, Web Standards and the general quantum mechanics surrounding the subject. And I'm left wondering, who cares about IE8? Who cares about IE7, for the matter?

I see a trend where web developers are doing their best to make sure a website works on IE6, but most of code and design are generally targeted at Firefox, Opera and Safari. It might just be that the IE8 team is trying to do the right thing now. But you know what, it's too fucking late. We do not care about your Windows-only, closed-source web browser anymore.

Most web developers today choose to blissfully hope that whoever's using IE6 today will gracefully switch over to either Firefox, Opera or Safari in the future. And that's definitely what seems to be happening. More and more people are moving on to Linux and OSX. And let's just be honest to each other, Vista has so far failed, badly. Everyone who I know that has touched it can vouch for its sheer crappiness.

I honestly don't know why Joel Spolsky is writing about this subject at all, maybe it's because Microsoft has conquered a permanent status of relevance to him, but not for me anymore. Not to a lot of people I know. Maybe I'm wrong, maybe we're still forced to deal with all this crap over and over again. But for the time being, I'm just not gonna let Microsoft interfere once again with the way I build software.

#2008-03-13T21:37:49-03:00 @us @politics @economy while(true) dollar--; I can't help but think all the uproar about the U.S. economy collapse is just about the rich becoming less rich. What happens when there's a recession? People spend less. People give up luxuries, people become wiser. People start saving and they learn not to spend money they don't currently have. Americans have been spending more than they earn for a long time now. Hell, I know I have. If people wisen up, that's bad for banks, which is bad for governments. If you haven't already, go watch The Story of Stuff with Annie Leonard. That's why the Fed just lent 200 billion to banks. Socialism for the rich. The American overdose of consumerism has made a lot of people rich, but it has hurt the world more than one could possibly imagine. The real victim was the American culture, tho. Americans are becoming more and more depressed, overweight and poor, and they just sit and watch their government mercilessly screw with them, dragging their economy into the ground in hopeless military attempts to save the dollar.

No, I don't think Obama is going to win. Guys like him never do. All in all it's probably going to be freaking McCain, and I will sit back and watch this country crash and burn and hurt lots of other nations along with it, including my own. I also think the environment is going to literally burn 50 years from now, and there's nothing we can do about it. But rest assured. The planet will be just fine after we're done exterminating ourselves.

#2008-03-11T13:47:05-03:00 @religion @media @rants New Sins I have a theory. I think the media keeps track of this shit just because it's fun nowadays. Seriously, who gives a flying fuck? Religion is so histerically and hopelessly clueless it's actually humorous. In other news, over 16% of americans have been found to be religiously unaffiliated. Haven't the Church realized how much they're losing yet? Like, dude, you known men willingly believe what they want. As long as you put things in a way that's actually appealing, people will buy it. We're waiting for a new Jesus that loves abortions, gays and stem cell research. Quit the hypocrisy and all the cluelessness, start at least pretending you can still make sense, and maybe, just maybe, you might save your ass. But at the moment, you just ain't doing it right.

#2008-03-02T16:18:35-03:00 @python @concurrency @distributed Future Ronaldo's follow-up on my note about Arc got me thinking about what's to expect from the future of programming languages. Not only programming languages, but web software engineering in general. Rails brought some much delayed wisdom into the minds of most web developers, in a time where most of them didn't even know how to handle Linux over SSH, didn't even know all there is to be known about the HTTP protocol. I gave a few classes on Rails and Python programming last year and one of the very first questions I used to make was, "can anyone name all HTTP verbs?". Few actually responded.

Rails makes it insanely easy to quickly develop web applications, but when it comes to deploying, many extra skills are required from the average developer. You have to know how to set up distributed, load balanced servers that employ shared memory usage, extensive caching, not to mention taking care of scaling MySQL which is a project of its own. So these former PHP folks, people who were used to just drop in files into an expensive IBM server box, when they got into Rails programming they quickly realized deploying a scalable, large scale web application wasn't as easy as they would have expected.

I currently do lots of plain Rails 2.0 programming along with pure Python for processes, and one could say that makes up for most of today's state-of-the-art tools. But I think in the near future most Rails folks are going to be using Merb, for one thing. In Python land I see great activity in the Django, Pylons communities. But I think the major changes we are to expect in the future are not in programming languages, but overall architecture models.

Distributed, large scale computing has become my main topic of interest. For one thing I've been doing extensive use of Python multi-threaded or multi-process programming, I've learned to write daemons that take care of multi-core processors, I've learned to handle message passing, shared memory, all that it takes to build a processing cluster. The very simple fact that we can use an array of PS3s to build a supercomputer, if we just know what to do with it, makes me very enthusiastic about this.

So here's what I think we should focusing on in the future: distributed systems. Get an Amazon EC2 account and start playing. Dive into distributed and also, concurrent programming. CouchDB, SimpleDB (despite their recent screw up), Hadoop, Map/Reduce and MySQL proxy have been on my radar. Your goal should be to harness as much power as possible with the least amount of hardware.

#2008-03-02T01:53:28-03:00 @career @programming Basics Here's a small bit of advice for programmers looking for a high tech job (usually in a startup). Look, they don't care about your certificates, they don't care if you are Certified Sun/Microsoft whatever. If skills in a given programming language or technology are required, don't tell them you've taken a class or paid for 16-hour training somewhere. Even more so, if you're working with an opensource, actively developed programming language such as Python or Ruby, it just doesn't make any fucking sense to believe that you can go somewhere, have someone train you, get a certificate and be done with it. If you can be sure of anything is that most recruiters do not care where you have graduated, they don't care how you came to learn something. All they care about is how well you express to them your own confidence that you can do the job, and believe me, they can tell when you're bullshiting them. Here are the basics: if you're not a native English speaker, know how to write it and speak it well. Pick a pet opensource programming language and know to have your way in it. Release opensource software, be active in mailing lists, be a fast learner. Tell yourself you're some kind of genius that can learn just about anything in one night of reading, because, you know what, that's exactly what's going to be required of you.

#2008-02-25T13:02:35-03:00 @lisp @arc @programming Regarding Arc I never cared enough about Lisp to actually learn it but I must say, from 20 minutes of playing and reading the tutorial, Arc really looks like it's got some clever ideas in it. Whether or not it qualifies as a real language at the moment is irrelevant to me, I'm ok with calling it MzScheme on Rails or something, doesn't matter.

#2008-02-22T14:10:33-03:00 @rants @religion @politics @us I've just about had it with these people. To whom it may concern, there are slightly more important issues to give a rat's ass about other than whether or not a woman is allowed to go under a fucking abortion, alright?