Tuesday, October 30, 2007

Converting numerical bases in python

Working with hexadecimal, binaries, and other bases is becoming common at work, at first we try to use custom functions to convert from and to some bases, especially hexadecimal. But other languages have built-in methods to do that conversion, and so python too.

First to convert and hexadecimal string to an integer we use:

int_number = int('ab1', 16)

The variable int_number will be 2737. That is ab1 hex to integer conversion. Now what about the other way, integer to hexadecimal, that would be:

hex(int_number)

And that would return '0xab1'. Note that it returns a string and has the 0x notation, we could use that notation also in the first conversion like this:

int_number = int('0xab1', 16)

So as you can see we pass a parameter to the int function to indicate the base, if we dont pass any python assumes that we are using a 10 base. As you have also seen there is a hex built-in function but there is no binary, or any other base built-in, so since we work a lot with binary we built a small function to do the work. There are hundreds of ways to do this, using recursion, whiles, fors, lamdas, etc. We end up using this function:

def dectobin(number):
if number < 1:
return ""
else:
return dectobin(number / 2) + str(number & 1)


Hope this works for you.

Wednesday, October 24, 2007

FreeBSD 7.0 Beta1 Available!

Since I first tried FreeBSD on the server side I have a preference for it over Linux. I think it has superior performance, not that I have done any real benchmark but I have, sort of, felt it. Anyway some friends have done some tests in the past, but tests or not I still think it is a rock solid OS. My experience comes from FreeBSD 4.11 and 5.3, thos two versions were probably the best, now that they are both out of maintenance i use a mix between 5.5 and 6.0. But for some reason those two versions have not been as solid as the 5.3 or 4.11 were. In most cases it was a hardware compatibility problem.

Now FreeBSD 7.0 is nearing its release, probably in December we will have a brand new FreeBSD. I expect to see better hardware support and much more stability in general. So in order to accomplish that I hope to test the new beta release and any other change in the future. Just hope to have some time at work.

Tuesday, October 23, 2007

Komodo Edit

As a developer I have been searching an editor or IDE that could adapt to every need I could have. This search has been frustrating and not productive. Finally I have accepted the fact that there is no tool that will accomplish what I expected so now I work with a combination of utilities and editors.

Normally my developer time is spend in Linux, there I use gVim or vim. Sometimes when I have to work from home or a client I do it on my laptop that runs Windows Vista. There I recently have been using Komodo Edit. In the search for the perfect IDE I have seen ActiveState Komodo IDE tons of times, but it is available freely only as a Demo. Now ActiveState has released Komodo Edit completely for free and it comes with a set of features that covers my needs.

Also Komodo Edit can be extended using xpi files (the same as Mozilla Firefox and Thunderbird). And there is a community site where you can find this extensions. There are few extensions available now, but when the userbase grows I'm sure they will grow in number.

The features I use more or love more are autocomplete, the calltips, the Django syntax highlighting, block ident/unident, block comment/uncomment, and the multi-edit capability. Even though the space in the laptop screen is reduced (13" compared to the 19" + 19" + 17" that I use at my desktop), I can navigate with very little effort through all my code.

There is even a vim emulation mode! I haven't tried it yet, but for heavy vi users it could be a choice if you wish to explore other editors. There is a linux version but for now I still prefer to use only gVim. It is enough.

So the search is over for now, maybe in a couple of months I will go back to the search of the perfect tool, but for now I think I can use this tools and be productive enough.