Aiur – ZelluX 的技术博客

Security, Kernel, Virtualization, Programming Languages

Archive for October, 2008

Winsock2 Story

without comments

Written by zellux

October 30th, 2008 at 7:18 pm

Posted in Programming

Tagged with ,

D2Loader命令行参数

without comments

Written by zellux

October 29th, 2008 at 10:34 am

Posted in Games

Tagged with

Programming Language Pragmatics 笔记 [2]

without comments

1. Algol 60为了模仿宏定义的行为,采用了正则序求值,这本书上貌似没详细介绍正则序和应用序。记得SICP(Structures and Interpretation of Computer Programmes)上对这两个概念讲得很详细。所谓正则序,normal-order,是一种’fully expand and then reduce’的计算方式。举个SICP上的计算例子

(sum-of-squares (+ 5 1) (* 5 2))
(+ (square (+ 5 1)) (square (* 5 2)) )
(+ (* (+ 5 1) (+ 5 1)) (* (+ 5 2) (+ 5 2)) )
(+ (* 6 6) (* 10 10))
(+ 36 100)
136

而应用序则是’evaluate the arguments then apply’。
P.S GeSHi默认的lisp着色怎么是这样的 =_=

2. 惰性求值。应用序通常会更高效一点,但有时会出现不必要运行出错的情况,比如SICP中的习题1.5

(define (p) (p))
(define (test x y)
    (if (= x 0)
        0
        y))
(test 0 (p))

在应用序求值的情况下是会出现不必要的死循环的。于是有了折中的惰性求值lazy evaluation,需要的时候再求值。

一个用途就是创建惰性数据结构,Scheme手册中的一个例子,创建了一个表,内容是所有的自然数:

(define naturals
    (letrec ((next (lambda (n) (cons n (delay (next (+ n 1)))))))
       (next 1)))
(define head car)
(define tail (lambda (stream) (force (cdr stream))))

(head naturals) => 1
(head (tail naturals)) => 2
(head (tail (tail naturals))) => 3

Haskell中求值没有任何副作用,所有的参数都采用惰性求值。

[417/899]

Written by zellux

October 27th, 2008 at 9:33 pm

Why explicit self has to stay

without comments

Bruce Eckel的一篇日志建议把self从方法的参数列表中移除,并把它作为一个关键字使用。
http://www.artima.com/weblogs/viewpost.jsp?thread=239003

Guido的这篇日志说明了self作为参数是必不可少的。
http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

第一个原因是保证foo.meth(arg)和C.meth(foo, arg)这两种方法调用的等价(foo是C的一个实例),关于后者可以参见Python Reference Manual 3.4.2.3。这个原因理论上的意义比较大。

第二个原因在于通过self参数我们可以动态修改一个类的行为:

# Define an empty class:
class C:
pass

# Define a global function:
def meth(myself, arg):
myself.val = arg
return myself.val

# Poke the method into the class:
C.meth = meth

这样类C就新增了一个meth方法,并且所有C的实例都可以通过c.meth(newval)调用这个方法。

前面两个原因或许都可以通过一些workaround使得不使用self参数时实现同样的效果,但是在存在decorator的代码中Bruce的方法存在致命的缺陷。(关于decorator的介绍可以参见http://www.python.org/dev/peps/pep-0318/)

根据修饰对象,decorator分两种,类方法和静态方法。两者在语法上没有什么区别,但前者需要self参数,后者不需要。而Python在实现上也没有对这两种方法加以区分。Bruce日志评论中有一些试图解决decorator问题的方法,但这些方法都需要修改大量底层的实现。

最后提到了另一种语法糖实现,新增一个名为classmethod的decorator,为每个方法加上一个self参数,当然这种实现也没必要把self作为关键字使用了。不过我觉得这么做还不如每次写类方法时手工加个self =_=

Written by zellux

October 27th, 2008 at 11:07 am

Posted in Programming

Tagged with

Python函数属性

without comments

定义一个简单的Python函数

def f(x, y = 10, z = 16):
    print z

dir(f)可以看到函数的几个主要属性 ‘func_closure’, ‘func_code’, ‘func_defaults’, ‘func_dict’, ‘func_doc’, ‘func_globals’, ‘func_name’。在funcobject.c中对PyFunction_Type的定义中可以看到这些属性的来源:

static PyMemberDef func_memberlist[] = {
    {"func_closure", T_OBJECT, OFF(func_closure), RESTRICTED|READONLY},
    {"func_doc",     T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
    {"__doc__",      T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
    {"func_globals", T_OBJECT, OFF(func_globals), RESTRICTED|READONLY},
    {"__module__",   T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
    {NULL}  /* Sentinel */
};
static PyGetSetDef func_getsetlist[] = {
    {"func_code", (getter)func_get_code, (setter)func_set_code},
    {"func_defaults", (getter)func_get_defaults, (setter)func_set_defaults},
    {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
    {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
    {"func_name", (getter)func_get_name, (setter)func_set_name},
    {"__name__", (getter)func_get_name, (setter)func_set_name},
    {NULL} /* Sentinel */
};

f.func_defaults记录函数的默认值,这个例子中就是一个tuple(10,16)

f.func_code是一个code对象,对应于源码中的PyCodeObject。除了《Python源码剖析》中提到的几个属性外,有三个tuple记录了不同类型的变量,co_varnames, co_freevars和co_cellvars,分别对应local variables, free variables和cell variables。

free variables指enclosing scope中的变量,cell variables则是指会被多个scope访问的变量。

def foo():
    a = 5
    def bar():
        return a
    print "cellvars:", bar.func_code.co_cellvars
    print "freevars:", bar.func_code.co_freevars
    return bar

g = foo()
print foo.func_code.co_freevars
print foo.func_code.co_cellvars

最后的结果显示bar的cellvars为空,freevars为(‘a’, 0);foo的cellvars为(‘a’, 0),freevars为空。

Written by zellux

October 24th, 2008 at 9:22 pm

Posted in Programming

Tagged with

ArchLinux中架设svn服务器

without comments

http://wiki.archlinux.org/index.php/Subversion_Setup

参照这篇wiki设置了下subversion。上传的时候报错
svn: Can’t open file ‘/home/svn/repositories/python/db/txn-current-lock’: Permission denied

最后发现是因为没有把apache httpd和http这个用户关联起来,在/etc/httpd/conf/httpd.conf中把User和Group都改成http即可。

Written by zellux

October 23rd, 2008 at 8:21 pm

Posted in Tools

Tagged with ,

Rotating Calipers

without comments

Written by zellux

October 22nd, 2008 at 3:16 pm

[zz]Decorator的另一种实现方式

without comments

http://blog.csdn.net/balabalamerobert/archive/2008/04/19/2308217.aspx

这一切都源自python-cn邮件列表上的一个话题,这个话题中,leopay给出了另一种实现decorator的方式:
一般decorator都是写一个嵌套函数,

def A(func):
    def new_func(*args, **argkw):
        #做一些额外的工作
        return func(*args, **argkw) #调用原函数继续进行处理
    return new_func
@A
def f(args):pass

其实也有另外一种”优雅”点的写法:

def A(func, *args, **argkw):
    #做一些额外的工作
    return func(*args, **argkw) #调用原函数继续进行处理

@A.__get__
def f(args):pass

很有趣,对吧。我分析了一下这种方式起作用的原因,很有意思,特记录如下。
Read the rest of this entry »

Written by zellux

October 21st, 2008 at 4:10 pm

Posted in Programming

Tagged with ,

Most Influential PLDI Paper Award

without comments

2008年评出了1998年最具影响力的PLDI论文,获奖论文的作者将分摊1000美元的奖金 -_-b

2008 (for 1998): The Implementation of the Cilk-5 Multithreaded Language, Matteo Frigo, Charles E. Leiserson, and Keith H. Randall

Citation

“The 1998 PLDI paper “Implementation of the Cilk-5 Multithreaded Language” by Matteo Frigo, Charles E. Leiserson, and Keith H. Randall introduced an efficient form of thread-local deques to control scheduling of multithreaded programs. This innovation not only opened the way to faster and simpler runtimes for fine-grained parallelism, but also provided a basis for simpler parallel recursive programming techniques that elegantly extend those of sequential programming. The stack-like side of a deque acts just like a standard procedure stack, while the queue side enables breadth-first work-stealing by other threads. The work-stealing techniques introduced in this paper are beginning to influence common practice, such as the Intel Threading Building Blocks project, an upcoming standardized fork-join framework for Java, and a variety of projects at Microsoft.”

另外前几年的获奖paper有:
2007 (for 1997): Exploiting Hardware Performance Counters with Flow and Context Sensitive Profiling, Glenn Ammons, Thomas Ball, and James R. Larus

2006 (for 1996): TIL: A Type-Directed Optimizing Compiler for ML, David Tarditi, Greg Morrisett, Perry Cheng, Christopher Stone, Robert Harper, and Peter Lee

2005 (for 1995): Selective Specialization for Object-Oriented Languages, Jeffrey Dean, Craig Chambers, and David Grove

2004 (for 1994): ATOM: a system for building customized program analysis tools, Amitabh Srivastava and Alan Eustace

2003 (for 1993): Space Efficient Conservative Garbage Collection, Hans Boehm

2002 (for 1992): Lazy Code Motion, Jens Knoop, Oliver Rüthing, Bernhard Steffen.

2001 (for 1991): A data locality optimizing algorithm, Michael E. Wolf and Monica S. Lam.

2000 (for 1990): Profile guided code positioning, Karl Pettis and Robert C. Hansen.

Written by zellux

October 17th, 2008 at 7:50 pm

Posted in Programming

Tagged with ,

Zotero与Endnote的互相导入

without comments

两者配合,更完美的知识管理方案

http://hi.baidu.com/qq303520912/blog/item/de5cba082db83e36e924889e.html

Endnote是目前国内科研人员使用最多的文献管理软件,功能最完备,各数据库或大学图书馆等和它的兼容也是最好。它的Filter和style也最为丰富,而且可以自己创建修改。看看周围的人,大部分都是Endnote的用户。   Zotero作为一个新的文件管理系统,与Endnote相比还是稚嫩了些,特别对于国内数据库的支持不佳,更是限制了它的应用。

不过,Zotero作为Firefox浏览器的插件,还是有一些特别之处。
Read the rest of this entry »

Written by zellux

October 17th, 2008 at 7:11 pm

Posted in Tools

Tagged with , ,

FireStats icon Powered by FireStatsBetter Tag Cloud