Archive for July, 2007

Bjarne Stroustrup at Google NY, July 31st 2007

Tuesday, July 31st, 2007

Tonight, July 31st of 2007 I had the privilege of attending a talk by the man himself, the creator of the C++ language, Bjarne Stroustrup.

He was sharing with us how things are going for the next version of C++0x - The 0x stands for a possible year of this decade where it will be released.

One of the interesting things that happened to me at this meeting was to finally have the honor of meeting Sebastian Delmont, and oh, those 3 geeky Red Hat guys, wonder if they were actually part of Red Hat or just hardcore fans, good thing I was wearing my Ubuntu shirt to represent for the Ubunteros.

As for C++0x, I guess my thoughts aren’t that deep, I honestly don’t care much about C++ at this point in my life, and it seems it’s an old language striving hard to keep the pace with newer features found in new languages. It seems its very hard to reach consensus on the C++ board, and its very hard to accept new features given the language is already too big, its also funny to see that everything that gets hughe (millions of people use it), will always have problems in terms of adapting to new conditions or injecting innovation to itself. Bjarne created the language around 25 years ago, and there are features that only now he’s finally been able to get accepted for this version. C++ however, we must admit is a language that’s heavily used, (but I think by end users, not the software engineering community), on software like Photoshop, Google search engine, and the Mars rovers (these were the examples Bjarnes kept bringing again and again, I would also be proud of course).

For me, I’ll stick to Python, Nasa’s been using it too for quite a while, the same with Google and many other applications, hey, YouTube’s backend runs in python.

Bible should come with a disclaimer

Tuesday, July 31st, 2007

Mirroring a post that I saw today at digg.com…

Absolutely true, it should come with that big sticker on the front.

Nueva franela Ubuntu

Tuesday, July 31st, 2007

Acaba de llegar mi nueva franela con el logo de Ubuntu. Puedes hacer franelas como estas en spreadshirt.com

Greenpoint, paradise for both Polish and Venezuelan

Sunday, July 29th, 2007

When we moved out of our ghetto latin neighborhood, my wife told me “You’re going to miss the food”, but today, I discovered one supermarket in Manhattan Ave. here in Greenpoint which has the best of both worlds.

Not only they have all the polish stuff that you can get in this neighborhood (which makes my wife’s life a lot better), but they also have latin food and best of all they have Harina Pan, to make the best arepas a Venezuelan guy could eat.

If you’re a Venezuelan and you’re planning to marry a polish person, get your ass to Greenpoint in Brooklyn, NY.

Python Reference: Binary Operators

Saturday, July 28th, 2007

Python binary operators are pretty much the same as in any other language, however I notice most programmers tend to waste a lot of memory by creating lots and lots of properties say in DB tables, or Objects and using the wrong datatypes. I think its elegant to use the concept of binary flags, for example, if you have an object that has around 8 or 16 boolean properties, you can store their state in 1 byte or 2 bytes (1 char or 2 char fields), and turn on/off the bits on those fields.

Usually, if you have a binary field, and you want to, turn on bits, toggle bits, and check bits, you do the following.

Suppose “config” is an 8 bit number, and you want to modify this bits individually, say config=4 (0b100) and you want to turn on the rightmost bit to have 0b101 (which is a 5) you could do the following

>>> #Uses the | operator
... def turnBitOn(config, binaryFlag):
...   return config | binaryFlag
...
>>> config = 4
>>> config = turnBitOn(config,1)
>>> print config
5

If you want to check if a Bit is turned on, just use the “&” operator, if the result is the same as the bit you’re comparing, then its turned on.

>>> def checkBit(config,binaryFlag):
...   return binaryFlag == config & binaryFlag
...
>>> print checkBit(config,1)
True
>>> print checkBit(config,2) #checks 0b010 in 0b101
False

What if you just want to toggle a bit, no matter what’s in there?
Just use binary XOR, the “^” operator.

>>> def toggleBit(config,binaryFlag):
...   return config ^ binaryFlag
...
>>> print config
5
>>> config = toggleBit(config,1)
>>> print config
4
>>> config = toggleBit(config,1)
>>> print config
5

And now, the last basic operation would be to turn off a bit. For this, you should do a NAND operation. The Binary Not in python as in most programming languages is the “~” operator. This is how you can use it to turn off bits.

You have to do it in conjunction with the & operator, sort of doing a NAND

>>> 5 & (~1)
4
>>> 5 &~ 1
4

So we could define our turnOffBit function as follows:

>>> def turnOffBit(config,binaryFlag):
...   return config & (~binaryFlag)
...
>>> print config
5
>>> config = turnOffBit(config,1)
>>> print config
4
>>> config = turnOffBit(config,1)
>>> print config
4

Hope this makes a good reference for those trying to make the most out of their bytes.

Python: How to debug HTTP while using urllib2

Friday, July 27th, 2007
...
import urllib
import urllib2

#this is just to prepare a dynamic uri (this is actual code from a system I'm building, sorry)
fileDownloadServiceURL = '%s://%s:%s/%s' % (transport,server,port,pathToController)
postData = {'URI':fileUri} #add more post stuff here
postData = urllib.urlencode(postData) #make sure you encode your post data

#add some custom headers if you need them
headers={"Host": server+':'+port,"Cookie":"JSESSIONID="+sessionId,"User-Agent":"Name of your User-Agent Here"}

#prepare your request, with headers and post data
req = urllib2.Request(fileDownloadServiceURL,postData,headers)

#and this is the magic. Create a HTTPHandler object and put its debug level to 1
httpHandler = urllib2.HTTPHandler()
httpHandler.set_http_debuglevel(1)

#Instead of using urllib2.urlopen, create an opener, and pass the HTTPHandler
#and any other handlers... to it.
opener = urllib2.build_opener(httpHandler)

#User your opener to open the Request.
urlHandle = opener.open(req)

#you'll end up with a file-like object... which I called urlHandle
...

The ouput will have useful debugging info about the HTTP connection.

connect: (localhost, 8080)
send: u'POST /arcturus-web/fileVariableDownload.service HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 57\r\nConnection: close\r\nUser-Agent: Temboo Twyla/Arcturus HTTP Downloader\r\nHost: localhost:8080\r\nCookie: JSESSIONID=1b8xl8nozb2i\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: 'URI=temboo%3A%2F%2Fwww.one.com%2Ffiles.f%2Fmapping1.m.var'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Language: en-US
header: Content-Type: text/html; charset=ISO-8859-1
header: Connection: close
header: Server: Jetty(6.0.2)