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