Sunday, September 18, 2011

Bluetooth modem in Ubuntu 10.04

Finally work with my Samsung GT-E2230M. Although I'll not be using this since there're abundant of much cheaper and faster 3G connections, who know I'll need it some day so it's good to know that it could. The blueman package in the standard repositories so far provided all the bluetooth functionalities that I need so far.

Connecting up to PPP with the handphone as a modem is as simple as right click the respective phone in the bluetooth manager and navigate to "Serial Ports" => "Dial Up Networking". First time trying this it managed to established the connection but subsequent tries greet me with socket error. Then I have a good luck by clicking the setup assistant (the gear icon) and go through the 2 step wizard. Once connected, I can see the ppp0 interface is up with IP address assigned but I can't ping any public IP address. Checking the route, the routing table is empty. Try adding some default gateway "route -a [ppp0 ip addres] gw default" but still can't ping outside address.

Finally found what I need in this blog post. The ppp0 must be set as a default connection.
# ip route change default via [ppp0 ip address] dev ppp0.
Voila ! I'm connected to the Net now.

Monday, September 12, 2011

Formatting floats in Python

Not really straightforward since there are number of issues. In this SO question, Alex Martelli simply suggested using the rstrip() string method to remove the zero. Others suggested the "g" flag, such as "%.1g". One issue with "g" flag is if the number to be formatted is larger than the format width specify, it would display it in scientific notation. It can be fixed by using large enough width such as "%.99g".

Django has floatformat template filter and it seem to use different approach in django.core.template.defaultfilters
def floatformat(text):
    """
    Displays a floating point number as 34.2 (with one decimal place) -- but
    only if there's a point to be displayed
    """
    try:
        f = float(text)
    except ValueError:
        return ''
    m = f - int(f)
    if m:
        return '%.1f' % f
    else:
        return '%d' % int(f)