Jul
20
2010
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
no comments | tags: embedded, linux, Mac, MacPorts, OBD, Python, serial, UART, USB, wxPython, X11 | posted in Tips
May
8
2010
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
no comments | tags: astimezone, date, datetime, dateutil, ISO, ISO8601, local, Python, pytz, time, timezone, UTC | posted in Tips
Apr
28
2010
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
no comments | tags: Apple, application, Deployment, Mac, PIL, py2app, PyInstaller, PyQt, Python, Qt, Snow Leopard | posted in Tips
Apr
7
2010
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.
no comments | tags: attribute, function, Python, static, variable | posted in Tips
Apr
5
2010
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
no comments | tags: ip, networking, packet, Python, Scapy, sniffer, TCP | posted in Tips
Nov
30
2009
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
no comments | tags: file, log, output, print, Python, screen, stdout | posted in Tips
Nov
14
2009
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:
- Get the latest versions of PyQt and SIP. You need Qt installed of course.
- Configure SIP using
python configure.py --arch i386
- Configure PyQt using
python configure.py --use-arch=i386
- 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
- If your Python still refuses to run in 32 bit mode, try
no comments | tags: 32, 64, i386, Mac, PyQt, Python, Qt, SIP, Snow Leopard | posted in Tips
May
3
2009
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

First we subclass a QListWidget to handle events
Continue reading
4 comments | tags: drag, drag-and-drop, drop, explorer, file, image, picture, PIL, PyQt, Python, QListWidget, Qt, thumbnail | posted in Tips
Mar
31
2009
xiao

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
1 comment | tags: Framework, GUI, programming, PyQt, Python, Qt, Tutorial | posted in Tips