Asleep from Day

November 11, 2008

Words from the Wise

Filed under: programming — John @ 12:24 pm

Quote from Interview with Donald Knuth

Still, I hate to duck your questions even though I also hate to offend other people’s sensibilities—given that software methodology has always been akin to religion. With the caveat that there’s no reason anybody should care about the opinions of a computer scientist/mathematician like me regarding software development, let me just say that almost everything I’ve ever heard associated with the term “extreme programming” sounds like exactly the wrong way to go…with one exception. The exception is the idea of working in teams and reading each other’s code. That idea is crucial, and it might even mask out all the terrible aspects of extreme programming that alarm me.

I also must confess to a strong bias against the fashion for reusable code. To me, “re-editable code” is much, much better than an untouchable black box or toolkit. I could go on and on about this. If you’re totally convinced that reusable code is wonderful, I probably won’t be able to sway you anyway, but you’ll never convince me that reusable code isn’t mostly a menace.

June 19, 2008

generate object methods at runtime

Filed under: programming, python — John @ 3:17 pm

I have been working on a dialer button class since yesterday. It makes sense to use the command design pattern here, and I want to separate the commands and the buttons so I can change the functionality of every button at runtime.

So we are talking about something like this:

class DialerButtons:
    def __init__(self):
        self.command_table = [
            self.numpad_1, self.numpad_2, self.numpad_3,
            self.numpad_4, self.numpad_5, self.numpad_6,
            self.numpad_7, self.numpad_8, self.numpad_9,
            self.cancel, self.numpad_0, self.dial]

    def numpad_0(self):
        self.text.append('0')

    def execute(self, n):
        self.command_table[n]()

<snipped>

and you can use this class like this:

dialer = DialerButtons()
dialer.execute(0)
dialer.execute(8)
....

Now obviously define numpad_0 to numpad_9 is a boring task. What happens if you need to define numpad 0 to 99? So, I came out with this code piece:

    @classmethod
    def _numpad_commands_factory(cls):
        for n in xrange(0, 10):
            setattr(cls, 'numpad_%d' % n, lambda self: self.text.append(str(n)))
...
DialerButtons._numpad_commands_factory()

This way you initialize DialerButtons AFTER you start the program and make methods numpad_0 to 9 on the fly. At least that’s what I was trying to do. However, it didn’t come out as I expected. Every numpad method will just add ‘9′ to self.text, instead of the respective ‘0′ to ‘9′. Why?

The reason is that the context of numpad_0, for example, is actually “f(self): self.text.append(str(n)))” instead of “f(self): self.text.append(’0′)”. so, what it does here is that it refers to the variable n inside _numpad_commands_factory, and the value of n is 9 after you executed it.

The correct code piece is:

    @classmethod
    def _numpad_commands_factory(cls):
        def f(chr):
            return lambda self: self.text.append(chr)
        for n in xrange(0, 10):
            setattr(cls, 'numpad_%d' % n, f(str(n)))

This way we can evaluate the value of str(n) first, then generate the appropriate function and assign it to numpad_n.

April 18, 2008

我果然不懂 C 啊。

Filed under: C, programming — John @ 1:09 am

from gcc-4.2.info:

5.34 An Inline Function is As Fast As a Macro

<..snipped..>

If you specify both `inline’ and `extern’ in the function definition,
then the definition is used only for inlining. In no case is the
function compiled on its own, not even if you refer to its address
explicitly. Such an address becomes an external reference, as if you
had only declared the function, and had not defined it.

This combination of `inline’ and `extern’ has almost the effect of a
macro. The way to use it is to put a function definition in a header
file with these keywords, and put another copy of the definition
(lacking `inline’ and `extern’) in a library file. The definition in
the header file will cause most calls to the function to be inlined.
If any uses of the function remain, they will refer to the single copy
in the library.

Since GCC 4.3 will implement ISO C99 semantics for inline functions,
it is simplest to use `static inline’ only to guarantee compatibility.
(The existing semantics will remain available when `-std=gnu89′ is
specified, but eventually the default will be `-std=gnu99′; that will
implement the C99 semantics, though it does not do so in versions of
GCC before 4.3. After the default changes, the existing semantics will
still be available via the `-fgnu89-inline’ option or the `gnu_inline’
function attribute.)

GCC does not inline any functions when not optimizing unless you
specify the `always_inline’ attribute for the function, like this:

/* Prototype. */
inline void foo (const char) __attribute__((always_inline));

So, consider the following program (download):

gcctest.c:

#include "inline.h"

int main()
{
     puts(externinline());
     return 0;
}

inline.h:

#include 

__inline__ extern char *externinline()
{
     return "inline";
}

lib.c:

char *externinline()
{
     return "extern";
}

The behavior of the executables will be different with or without the -O option of gcc. Compile it without -O, the program will print “extern”. Compile it with -O, the program will print “intern”.

September 29, 2007

GLOBAL and struct

Filed under: C, programming — John @ 12:28 pm

Refer to: Gnu – Global – Help – How to find the definition effectively

To put the story even shorter, the GNU GLOBAL will not treat C struct as definition, but as `other symbol’ . This means you may only find a struct with global -s, which will give you lots of irrelevant results.

This feature is in the GLOBAL plans for the future already, which means it might be implemented sometime in the future. For now, it’s better to use GLOBAL to trace how the program runs, the relationship between functions, rather then finding the semantic details. For the semantic side, the ctags or etags is still my first choice.

September 13, 2007

Hello World?

Filed under: programming, python — John @ 3:30 am

Guess this is appropriate for a blog about software.

>>> print "Hello, world!"
Hello, world!

Blog at WordPress.com.