Run Pencil on Ubuntu 12.04 / 12.10

Pencil is simply the best (open) sketchy mockup solution on Ubuntu. However, as described on its download page, you cannot install it as a Firefox extension because it’s not compatible with the Firefox 18.x that comes with Ubuntu 12.04. The standalone version runs okay but some features such as export as PNG fails silently because of the same compatibility issue.

Turns out you don’t need to install the deb package provided on its website. Simply download the latest tar ball from the download page on google code, then download xulrunner (Mozilla runtime) from ftp.mozilla.org. I used the latest supported version: 16.0.2.

Now extract these to a preferred place, then add a file called pencil anywhere in your $PATH with the following content:

exec <somewhere>/xulrunner/xulrunner --app "<somewhere>/pencil-2.0.4/usr/share/pencil/application.ini"

then chmod +x pencil, done.

如何印清單

2005 年我問過 Thinker 這樣的問題:想程式常碰到要印出清單的情況,對人類而言習慣的格式是:

1, 2, 3, 4

但這個格式對於 C 語言來說卻不太好處理。一般可以寫成這樣:

<<一般作法>>=
void normal(const char *list[], const int len)
{
    int i = 0;
    printf("%s", list[i++]);
    for (; i < len; i++) {
        printf(", %s", list[i]);
    }
    puts("");
}
@

但是 printf 這行會重複,似乎不是最好的寫法。如果不要重複,那就得在迴圈中加個判斷式,但每次都要多個判斷又好像有點浪費:

<<判斷作法>>=
void condition(const char *list[], const int len)
{
    int i;
    for (i = 0; i < len; i++) {
        if (i != 0)
            printf(", ");
        printf("%s", list[i]);
    }
    puts("");
}
@

當時 Thinker 想了想,給了我一個用 function pointer 的答案

void dummy() {
}

void line() {
   printf("  ------\n");
}

inter = &dummy;
for(i = 0; i < n; i++) {
   inter();
   printf("%s\n", record[i]);
   inter = &line;
}

話說,事隔多年,今天在看其他東西的時候,突然想到這個問題可以用 Clifford’s Device 的方法做。

<<Clifford>>=
void clifford(const char *list[], const int len)
{
    int i = 0;
    while (1) {
        if (0) {
          clifford:
            printf(", ");
        }
        printf("%s", list[i++]);
        if (i >= len)
            break;
        goto clifford;
    }
    puts("");
}
@

各位看官,可有什麼新想法嗎?

<<list.c>>=
#include <stdio.h>

<<一般作法>>

<<判斷作法>>

<<Clifford>>

int main(int argc, char *argv[])
{
    const char *list[] = {
        "one",
        "two",
        "three",
    };
    const int l = sizeof(list) / sizeof(char *);

    normal(list, l);
    condition(list, l);
    clifford(list, l);
    return 0;
}
@

當然啦,如果是 python,這問題可簡單了:

>>> l = ['one', 'two', 'three']
>>> print ', '.join(l)
one, two, three

另,本文採用 Noweb 格式,用工具跑一遍就可以產生 list.c 。

virtualenv, python-rope and pylint

python-ropemac is really useful for developing python in emacs, and pylint is also very handy as a analyzer. However, they both don’t work very well with virtualenv, especially because I always run emacs in server mode, and the server instance is usually not under virtualenv.

Here is how to make things work:

Edit .ropeproject/config.py:

# You can extend python path for looking up modules
    prefs.add('python_path',
              '/your-virtualenv-dir/lib/python2.7/site-packages/')

For pylint, generate (by pylint --generate-rcfile) or copy your default pylintrc to project root dir. Edit it:

# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
init-hook='this_file="/your-virtialenv-dir/bin/activate_this.py";execfile(this_file, dict(__file__=this_file))'

After this pylint will work even in emacs.

Curious case of closure in Go and Python

At http://tour.golang.org/#39, I found the following sample code:

package main

import "fmt"

func adder() func(int) int {
	sum := 0
	return func(x int) int {
		sum += x
		return sum
	}
}

func main() {
	pos, neg := adder(), adder()
	for i := 0; i < 10; i++ {
		fmt.Println(
			pos(i),
			neg(-2*i),
		)
	}
}

Its document reads:

… functions are full closures. The adder function returns a closure. Each closure is bound to its own sum variable.

Take that into consideration, it might be easier to understand the execution result:

0 0
1 -2
3 -6
6 -12
10 -20
15 -30
21 -42
28 -56
36 -72
45 -90

To me, variable sum is similiar to ‘instance variable’, in Object-oriented’s terminology. However, in Python, things can be quite different.

def adder():
    sum = 0

    def f(x):
        sum += x
        return sum
    return f


def main():
    pos, neg = adder(), adder()
    for i in xrange(0, 10):
        print pos(i), neg(-2 * i)

The code looks roughly the same, but it will raise the following exception:

Traceback (most recent call last):
  File "closure.py", line 17, in 
    main()
  File "closure.py", line 13, in main
    print pos(i), neg(-2 * i)
  File "closure.py", line 5, in f
    sum += x
UnboundLocalError: local variable 'sum' referenced before assignment

This is because if sum is to be modified, Python must decide which variable to change. sum += x is the same as sum = sum + x, and sum = suggests it’s a local variable, since all variable is by default local in Python. Given that, expression sum + x can not be evaluated because sum, as a local variable, is still undefined here.

If the sum += x line is removed, and sum + x is returned directly, the result will be:

0 0
1 -2
2 -4
3 -6
4 -8
5 -10
6 -12
7 -14
8 -16
9 -18

It runs okay, but the result is wrong. Where does function f get the value of sum? If Python cannot find a variable in locals(), it will try to find it from the scope above it, i.e. function adder, and sum is indeed defined in it. The real Python equivelent of the Go program above will be:

class adder:
    def __init__(self):
        self.sum = 0
    def __call__(self, x):
        self.sum += x
        return self.sum


def main():
    pos, neg = adder(), adder()
    for i in xrange(0, 10):
        print pos(i), neg(-2 * i)


if __name__ == '__main__':
    main()

Functions are already first class objects in Python. Here we create a class that its instance behaves like a function, so it is a function because of duck typing.

Swap every windows between workspaces in awesome wm

awesome is the tiling windows manager I use daily and I just wrote a very useful (at least for me) function for it. Basically it swaps every client (similar to windows) between the current tag (similar to workspace) and the target tag. Normally I put terminal clients for my current task at tag#3, right next to my Emacs-only tag#2 so I can quickly switch between browser (#1), editor and terminals. Now if I want to switch to a different task I just need to swap in terminals from another tag while swap out current terminals to that tag.

-- i is the index of target tag in variable `tags'
function ()
   local screen = mouse.screen
   local from = client.focus:tags()[1]
   local to = tags[screen][i]
   if to then
       t = to:clients()
       for i, c in ipairs(from:clients()) do
           awful.client.movetotag(to, c)
       end
       for i, c in ipairs(t) do
           awful.client.movetotag(from, c)
       end
   end
end

I put this under

-- Bind all key numbers to tags.
-- Be careful: we use keycodes to make it works on any keyboard layout.
-- This should map on the top row of your keyboard, usually 1 to 9.
for i = 1, keynumber do
    globalkeys = awful.util.table.join(globalkeys,
        awful.key({ modkey }, "#" .. i + 9,
                  function ()
                        local screen = mouse.screen
                        if tags[screen][i] then
                            awful.tag.viewonly(tags[screen][i])
                        end
                  end),
        awful.key({ modkey, "Control" }, "#" .. i + 9,

in my copy of default rc.lua.

Multi-thread testing in Pyramid

If you want to do multi-thread testing in Pyramid, it probably won’t work the first time because request and registry are thread local, and things like get_renderer will call get_current_registry. When it happens in a thread, it won’t get the same value as it would have in the main thread.

So, here is a hack to address this:

import pyramid.threadlocal
from threading import Thread, Lock

candidates = [
    (self._test1, ()),
    (self._test2, ()),
    (self._test3, ()),
    ]

def random_func(pyramid_thread_locals):
    pyramid.threadlocal.manager.push(pyramid_thread_locals)
    time.sleep(random.random())  # 0 ~ 1 sec
    func, args = random.choice(candidates)
    func(*args)

pyramid_thread_locals = pyramid.threadlocal.manager.get()
threads = [Thread(target=random_func, args=(pyramid_thread_locals, ),)
           for i in range(100)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

There is no guarantee that pyramid.threadlocal.manager will always be there. Even if it’s there, there’s no guarantee it can be used this way. So, this should only be considered as a temporary workaround.

Launching New Studio

JJ’s Studio is a software consultancy specialized in Android technologies. We are experienced in both system and application layers, plus several years of experience before that in Linux mobile phone development.

We have co-founded the well-known 0xlab.org, as well as created/contributed to several Free and Open Source Software projects, such as 0xdroid, 0xbench, OpenEmbedded, and OpenWrt.

Our services include:

  1. Android application: standard Android application that fits into the varieties of Android devices.
  2. Framework development: develop or integrate new features into device firmware in framework layer and below.
  3. Performance analysis: get more out of your hardware by profiling and tuning.

JJ’s Studio differentiates ourselves by the unique combination of knowledge in different layers of Android architecture. This enables us to work in different areas from developing a mobile application to delivering a complete device firmware.

Please also check my partner Julian’s article on this (in Traditional Chinese).

Contact:

John Lee
john@0xlab.org

(Traditional Chinese)

JJ’s Studio 為專精於 Android 相關科技的軟體顧問工作室。我們對於系統與應用層皆有經驗,並且在 Android 之前就有數年的 Linux 手機開發資歷。

我們與朋友共同創立了知名的 0xlab.org,並曾創立或貢獻給以下的開放原始碼專案:0xdroid, 0xbench, OpenEmbedded, OpenWrt.

我們的服務包括:

  1. Android 應用程式:著重於對不同 Android 裝置的相容與一致性。
  2. 框架層開發:將新功能開發或整合進 Android 框架或底層系統之中,直接包含在出廠韌體內。
  3. 效能分析:對系統剖析與調整,讓硬體發揮更大效能。

JJ’s Studio 的獨特競爭優勢來自於對 Android 系統各不同層面的理解。這讓我們可以勝任從應用程式開發,到發布完整的裝置韌體等不同的工作領域。

請參考我的夥伴 Julian 對此發布的文章

聯絡資訊:

John Lee
john@0xlab.org