Yuce's Blog

Cognitive dissonance is yoga for the brain.*

ReportLab on OSX

2012-11-03

It's easy to compile ReportLab on OSX (in my case, 10.8). The only problem is, you'll get the following warning:

# installing without freetype no ttf, sorry!
# You need to install a static library version of the freetype2 software

Don't try to brew it yet. OSX already has freetype2 installed on versions before 10.7. For 10.7 and later make sure you install XQuartz. Then we just need to help ReportLab installer to help find it.

Download ReportLab, and insert the following lines around after line 104 (after # attempt to make sure we pick freetype2 over other versions):

aDir(I, "/usr/X11/include") 
aDir(L, "/usr/X11/lib")

Remove the build continue.

python, reportlab, freetype2, osx, tricks

Compiling Python Extension Modules on Mac OSX 10.6

2011-05-27

If you get the following error when you are compiling a Python C extension module in Mac OSX 10.6:

/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed
Installed assemblers are:
/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
spammodule.c:63: fatal error: error writing to -: Broken pipe compilation terminated.

Run setup.py as follows:

ARCHFLAGS="-arch i386 -arch x86_64" python setup.py build

python, osx, tricks

Getting the Current Module in Python

2009-10-15

Say, you want to insert a function, class or any other object into the current module dy­nam­i­cal­ly. How can you do that? Of course, you start with getting the module name:

myName = globals()['__name__']

Then, get the module itself:

import sys
me = sys.modules[myName]

OK, you got the module, now insert something in it:

setattr(me, 'hello', lambda x: 'Hello %s'%x)

We can call hello in our module from now on:

print hello('World')

python, tips

Web Application Testing with Selenium: Basics and Setup

2009-10-09

In­tro­duc­tion

It's always hard to ensure an ap­pli­ca­tion works up to a spec­i­fi­ca­tion and continue keeping up to the specs in the course of de­vel­op­ment. Testing web ap­pli­ca­tions is even harder, since they are composed of more components, and usually at least some of the components of the ap­pli­ca­tion are not directly controlled by the developers, such as links to other internet resources or mashups.

Luckily, there's a great tool for easing the pain of web ap­pli­ca­tion testing: Selenium. Selenium simulates a human user, by con­trol­ling a web browser using Javascript through a system called Selenium Core. The simulated behaviour includes clicking on links, form continue.

python, selenium, testing, web

PySWIP: Facts and Rules

2009-10-08

PySWIP is a Python module which enables accessing SWI-Prolog's foreign language interface using our beloved computer language. Here's a small tutorial on adding facts and rules to prolog knowl­edge­base.

First, adding facts and rules, based on examples in a Prolog tutorial.

from pyswip import Prolog
p = Prolog()

# something is fun if it is a car and it is red.
p.assertz('(fun(X) :- red(X), car(X))')

# facts...
p.assertz('car(vw_beatle)')
p.assertz('car(ferrari)')
p.assertz('car(hyundai)')
p.assertz('bike(harley_davidson)')
p.assertz('red(ferrari)')
p.assertz('red(vw_beatle)')
p.assertz('blue(hyundai)')

Find all cars in the knowl­edge­base:

print list(p.query('car(Which)'))

Outputs:

[{'Which':'vw_beatle'}, {'Which':'ferrari'}, {'Which':'hyundai'}]

Find all fun things:

print list(p.query('fun(What)'))

Outputs:

[{'What':'vw_beatle'}, {'What':'ferrari'}]

Pretty easy, huh? ;) There are more complex Prolog examples in PySWIP source dis­tri­b­u­tion, including a sudoku solver.

python, prolog, pyswip

Getting the Currently Playing Song Info on Amarok via DCOP

2009-08-13

Here's a sample ap­pli­ca­tion for using DCOP with Python. Needed to install python-dcop on Debian. It works on KDE 3.5 with Amarok 1.4 (won't work on KDE 4)

import pydcop

playerService = pydcop.DCOPObject('amarok', 'player')

d = dict(
   title=playerService.title(),
   artist=playerService.artist(),
   album=playerService.album()
)
print '%(artist)s - %(title)s (%(album)s)' % d

kdcop is a great ap­pli­ca­tion to discover DCOP services.

I've posted a slightly extended version of this entry to Active State Python Cookbook.

python, DCOP, amarok, KDE

Ctypes Strings

2007-05-28

I've written bulk of PySWIP last summer, it is based on Nathan Denny's proolog.py. This is the first project that I used ctypes, a fantastic package which allows calling C functions from dynamic libraries that I use to link libpl.so (Linux) or libpl.dll (Windows) of SWI-Prolog. One of the dif­fi­cul­ties I had back then was finding the cor­re­spond­ing ctypes code for PL_get_chars which is defined at SWI-Prolog.h as follows:

PL_EXPORT(int) PL_get_chars(term_t t,
    char **s, unsigned int flags);

I use that function in queryGenerator:

answer = (c_char_p*maxsubresult)()
while PL_get_list(swipl_list, swipl_head, swipl_list):
    PL_get_chars(swipl_head, answer,
        CVT_ALL 
continue.

python, ctypes, prolog, pyswip

Shameful Past

2005-10-24

A few hours ago, when I was dealing with my thesis, I've come up with a Python function that constructs a tour for a given TSP instance using the nearest neighbor heuristic:

def nearestneighbor(size, dists):
   tour = [0]*size
   candy = [True]*size
   candy[0] = False

   for i in range(1, size):
       prev = tour[i - 1]
       tour[i] = min([(dists[prev][c], c)
           for c in range(size) if candy[c] and  c != prev])[1]
   
continue.

python, shame on you, nearest neighbor

Generating All Strings of a Given Alphabet

2005-06-10

For a recent research project, I had to write a function that generates all the strings of a given alphabet. It was (as always) very easy to figure it out in Python, only 5 minutes; below is the code (with mod­i­fi­ca­tions, I've polished the idea a bit):

def allstrings(alphabet, length):
    """Find the list of all strings of 'alphabet' of length 'length'"""

    if length == 0: return []

    c = [[a] for a in alphabet[:]]
    if length == 1: return c

    c = [[x,y] for x in alphabet 
continue.

python, strings, combination