Jul 20 2010

Free OBD2 Software for Mac

xiao

If you just picked up a generic OBD-II – USB interface on eBay and have a Mac, it is true that it is generally more convenient to access these hardware on Windows. Even embedded device developers tend to use Windows to develop against these generic FT232R chip based USB-UART devices simply because of more available supports. But worry not, it can be done on Mac (really on Linux with a more open-platform framework than Windows), it’s just a bit more complicated since it tends to come in be open-source source code rather than prepackaged self-sufficient binaries. Continue reading


May 8 2010

Python Convert ISO8601/UTC to Local Time

xiao

Ok, I looked this up twice now… once too many

Suppose you have a datetime of the form “2010-05-08T23:41:54.000Z” and you want a local datetime object

import pytz, dateutil.parser
utctime = dateutil.parser.parse("2010-05-08T23:41:54.000Z")
localtime = utctime.astimezone(pytz.timezone("Canada/Eastern"))

Boom


Apr 28 2010

Deploy PyQt Applications on Mac OS X with PyInstaller!

xiao

The interweb seem to incline on py2app when it come to deploying applications on mac. I’ve tried to make a single deployable .app file for my application for a long time trying to follow these instructions from ars technica. I’m not a hacker and just want to produce a deployable usable application for others to use. And it seems py2app from MacPorts wasn’t able to surmount the Snow Leopard’s 64-bit compatibility issue.

And then, I was slacking off while studying for my final and out of nowhere I found PyInstaller‘s explicit support for PyQt and its recent support for the mac. And after trying, almost everything works out without much of a kink. Credit goes to ChrisWayg who produced an amazingly complete and up-to-date set of instructions to follow. I’m merely telling how my application did using his instructions (April 2010) and hopefully doing my part to draw more attention to the excellent PyInstaller. Continue reading


Apr 7 2010

Python’s Function Static Variable

xiao

So you want a variable that stays between different calls of a function. Not the sexiest thing ever but always handy in small programs.

There’s tons of ways of doing this. You can embark on a quest to find the meaning of Pythonic or take this method that’s relatively simple:

def a():
    if not hasattr(a, "b"): a.b = 0
    a.b += 1
    print a.b

Calling a(), you’ll get 1, 2, 3, …

Note attribute ‘b’ of ‘a’ does not exist until you declare it for a first time. My main preference here is that ‘static’ variables used this way does not spill onto the rest of your code. Also, it’s clean, no classes, no data structures.


Apr 5 2010

Suppress Scapy IPv6 Warning

xiao

When you run Scapy without a default IPv6 routing gateway, Scapy will display this annoying warning:

WARNING: No route found for IPv6 destination :: (no default route?)

You definitely don’t want to see it every time you run the script you built with Scapy. To get rid of it, simply add

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)

before importing Scapy to suppress all messages below error messages


Nov 30 2009

Python Log Stdout to File

xiao

Python has the ability to alter its sys.stdout as to redirect its print commands to pretty much anything.

If, for instance, you want to print to both standard output and to a log file, you can create a class to handle the stdout like such:

class MyOutput():
    def __init__(self, logfile):
        self.stdout = sys.stdout
        self.log = open(logfile, 'w')
 
    def write(self, text):
        self.stdout.write(text)
        self.log.write(text)
        self.log.flush()
 
    def close(self):
        self.stdout.close()
        self.log.close()
 
sys.stdout = MyOutput("log.txt")
print "blah blah blah"

Continue reading


Nov 14 2009

PyQt and Snow Leopard

xiao

There are some 64-bit related issues when using PyQt and Snow Leopard. There’s the way to resolve it by reverting to 32-bits:

  1. Get the latest versions of PyQt and SIP. You need Qt installed of course.
  2. Configure SIP using
  3. python configure.py --arch i386
  4. Configure PyQt using
  5. python configure.py --use-arch=i386
  6. Finally, make sure your python is running in 32-bit mode because current Qt doesn’t support 64-bit mode. Add
    export VERSIONER_PYTHON_PREFER_32_BIT=yes

    to your .bash_profile in your home directory

  7. If your Python still refuses to run in 32 bit mode, try
    arch -i386 python

May 3 2009

PyQt Drag Images into List Widget for Thumbnail List

xiao

This simple tutorial shows how you can create a program with Python and Qt to allow for image files from Explorer/Finder/Nautilus to be dropped in a list widget and create list items with thumbnails

droppedthumbnails

First we subclass a QListWidget to handle events
Continue reading


Mar 31 2009

Qt with C++ or Python Tutorial

xiao
Qt Creator

Qt Creator

This is a collection of tutorials I found useful to get into the Qt framework. Being more used to the Microsoft standard, I have always wanted to branch into more cross-platform stuff so that I can at least write tools and programs for my Mac. I never really liked the idea of “reinventing the wheels” feel of C++ unless it was on an embedded system so Python looked like a solid contender to the rather messy Perl. Continue reading