Lianchun Tech
百度空间 | 百度首页 
               
 
文章列表
 
2008-04-21 00:05
照着书,写了个简单的helloworld扩展:

先写一个helloworld.c文件,内容如下:
################# begin helloworld.c ##############
#include <Python.h>
#include <stdio.h>

static PyObject*
helloworld(PyObject* selfi, PyObject* args)
{
        const char* name;
        int len;
        if (!PyArg_ParseTuple(args, "s", &name))
                return NULL;
        len = strlen(name);
        char* buf = (char*) malloc(len * sizeof(char) + 7);
        sprintf(buf, "hello %s", name);
        return Py_BuildValue("s", buf);
}



static char helloworld_docs[] = "hellowrold(): return a popular greeting phrase\n";

static PyMethodDef helloworld_funcs[] = {
        {"helloworld", (PyCFunction)helloworld, METH_VARARGS, helloworld_docs},
        {NULL}
};

void
inithelloworld(void)
{
        Py_InitModule3("helloworld", helloworld_funcs, "Toy-level extension module");
}
################### end helloworld.c ##################

然后再编写一个setup.py,内容如下:
################ begin setup.py ##################
from distutils.core import setup, Extension
setup(name='helloworld', ext_modules=[ Extension('helloworld', sources=['helloworld.c']) ])
################# end setup.py #####################

steven@smartbox:~/test$ sudo python setup.py install
running install
running build
running build_ext
building 'helloworld' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c helloworld.c -o build/temp.linux-i686-2.5/helloworld.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-i686-2.5/helloworld.o -o build/lib.linux-i686-2.5/helloworld.so
running install_lib
copying build/lib.linux-i686-2.5/helloworld.so -> /usr/lib/python2.5/site-packages
running install_egg_info
Removing /usr/lib/python2.5/site-packages/helloworld-0.0.0.egg-info
Writing /usr/lib/python2.5/site-packages/helloworld-0.0.0.egg-info

steven@smartbox:~/test$ python
Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from helloworld import helloworld
>>> msg = helloworld('steven')
>>> msg
'hello steven'
>>>


 
2008-02-17 00:13
在Python中, __add__方法对应如下形式的操作:
c = a + b
__iadd__方法对应如下形式的操作:
a += b

如果存在__add__方法,但是不存在__iadd__方法,那么上面两种形式都是调用的__add__方法.

如果__add__方法和__iadd__方法都存在, 那么 c=a+b对应__add__方法, a+=b对应__iadd__方法

如果存在__iadd__方法,但是不存在__add__方法,那么a+=b对应__iadd__方法, c=a+b这种用法会报错

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Test:

    def __add__(self, t):
        print 'call add'

a=Test()
b=Test()

c = a + b
print '+ works'
a += b
print '+= works'

输出:
call add
+ works
call add
+= works



#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Test:

    def __add__(self, t):
        print 'call add'

    def __iadd__(self, t):
        print 'call iadd'

a=Test()
b=Test()

c = a + b
print '+ works'
a += b
print '+= works'

输出:
call add
+ works
call iadd
+= works



#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Test:

    def __iadd__(self, t):
        print 'call iadd'

a=Test()
b=Test()

a += b
print '+= works'
c = a + b
print '+ works'


输出:
call iadd
+= works
Traceback (most recent call last):
File "test.py", line 14, in <module>
    c = a + b
TypeError: unsupported operand type(s) for +: 'NoneType' and 'instance'


 
2008-01-27 23:36
今天学习一下popen。

popen的好处就是能调用一个外部程序处理任务,然后得到处理后的结果。

>>> result = popen('ls /').read()    
>>> result
'bin\nboot\ncdrom\ndev\netc\nhome\ninitrd\ninitrd.img\nlib\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nsbin\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz\n'

上面是调用
ls /
并将其输出结果作为字符串返回。

想文件一样,也可以调用readlines返回所有的行。
>>> result = popen('ls /').readlines()
>>> result
['bin\n', 'boot\n', 'cdrom\n', 'dev\n', 'etc\n', 'home\n', 'initrd\n', 'initrd.img\n', 'lib\n', 'lost+found\n', 'media\n', 'mnt\n', 'opt\n', 'proc\n', 'root\n', 'sbin\n', 'srv\n', 'sys\n', 'tmp\n', 'usr\n', 'var\n', 'vmlinuz\n']
 
2008-01-26 23:41
在Python中像下面这样转换字符编码。
>>> a=u'测试'
>>> a
u'\u6d4b\u8bd5'

现在a是unicode编码的。

>>> a.encode('utf-8')
'\xe6\xb5\x8b\xe8\xaf\x95'

由unicode转换成utf-8编码。

>>> a.encode('gb2312')
'\xb2\xe2\xca\xd4'

转换成gb2312编码


想在各个编码之间转换就是先转换成unicode然后再转换成要编码的格式。比如:

>>> a='测试'
>>> a
'\xb2\xe2\xca\xd4'
>>> b=unicode(a, 'gb2312')
>>> b
u'\u6d4b\u8bd5'
>>> b.encode('utf-8')
'\xe6\xb5\x8b\xe8\xaf\x95'
>>>

python文本编码自动检查工具
http://chardet.feedparser.org/
 
2008-01-24 00:48
向一个class中添加新的method,方法是使用classobj:

>>> from new import classobj
>>> class Test:
...     pass
...
>>> Test2 = classobj('Test2', (Test,), {'hello':lambda self: 'this is hello'})
>>> t2=Test2()
>>> t2.hello()
'this is hello'
 
2008-01-23 23:37
Python的class可以动态添加方法,但是后添加的方法和类定义中的方法是不一样的。

>>> class Test:
...     def hello(self):
...             print 'this is hello'
...
>>> t=Test()
>>> dir(t)
['__doc__', '__module__', 'hello']
>>> t.hello
<bound method Test.hello of <__main__.Test instance at 0
>>> def good():
...     print 'this is good'
...
>>> t.mygood=good
>>> t.mygood
<function good at 0x00CB4630>

可以看到t.hello输出的是bound method, 而t.mygood输出是function....
所以说一个是bound method, 一个是function.

那有没有unbound method呢?还真有:)

>>> h=Test.hello
>>> h
<unbound method Test.hello>

调用一下试试
>>> h()
Traceback (most recent call last):
File "<stdin>", line 1, in <modul
TypeError: unbound method hello() m
ument (got nothing instead)
出错了,要一个参数,那我们就把刚才构建的t传给它
>>> h(t)
this is hello
这样就可以调用了:)

试试下面的定义,注意hello这个method没有参数self.
>>> class Test:
...     def hello():
...             print 'this is hello without self'
...
不错可以定义
>>> t=Test()
创建也没有问题
>>> t.hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello() takes no arguments (1 given)

无法调用,因为定义的method不包含参数
>>> h=Test.hello
>>> h
<unbound method Test.hello>
>>> h()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method hello() must be called with Test instance as first ar
ument (got nothing instead)
>>> h(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello() takes no arguments (1 given)
这些方式都不行,不知道这么定义的method有什么用?谁知道告诉我一下,先谢谢啦!

 
2008-01-23 23:09
List是最常用的数据结构了。
reverse()是将List自身倒序。开始还以为是返回一个倒序的List.

>>> test=['hello',123,'world']
>>> test
['hello', 123, 'world']
>>> test.reverse()
>>> test
['world', 123, 'hello']

想List中指定位置添加一个元素的方法是insert(index, element)
>>> test.insert(1,444)
>>> test
['world', 444, 123, 'hello']
想要删除444有两种方法,1按索引删除,用del, 按元素值删除用remove(element)
>>> del test[1]
或者
>>> test.remove(444)

当List中有两个相同的元素会怎么样呢?
>>> test
['world', 444, 123, 444, 'hello']
>>> test.remove(444)
>>> test
['world', 123, 444, 'hello']
原来只会删除第一个值相同的元素

想看List有多少方法,很简单,用dir
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gets
lice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '
__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__r
educe_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__
', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'p
op', 'remove', 'reverse', 'sort']



 
2007-10-14 04:11
折腾了一天终于把T61的声卡安装上了。

Debian Etch 更新到最新。
smartbox:~$ uname -a
Linux smartbox 2.6.18-5-686 #1 SMP Wed Oct 3 00:12:50 UTC 2007 i686 GNU/Linux

下面的脚本是从网上Down下来的,运行前要先安装module-assistant

apt-get install module-assistant

然后运行下面脚本

#/bin/sh
# Small script to download,patch,build the alsa module for Thinkpad T61/R61/X61.
# t61-build-alsa-module.sh v0.4 (C) Franklin Piat 2007. released under GPL.
# thanks to
#   Alsa teams
#   http://www.thinkwiki.org/wiki/AD1984
#   http://www.linuxquestions.org/questions//showthread.php?s=0b8d3760255e43abaecdfc8e0e4cba12&t=564079&page=3
#
m-a prepare || exit
m-a clean alsa || exit
m-a get alsa-source || exit
m-a unpack alsa || exit
cd /usr/src/modules/alsa-driver/alsa-kernel/ || exit
## kernel alsa 1.0.14 don't need the first three patch
case "$(cat ../version)" in
    1.0.13*)
        wget -O - http://hg.alsa-project.org/alsa-kernel/raw-diff/ed48e4edc677/pci/hda/patch_analog.c | patch -p1 || exit
        wget -O - http://hg.alsa-project.org/alsa-kernel/raw-diff/45179b325c8e/pci/hda/patch_analog.c | patch -p1 || exit
        wget -O - http://www.klabs.be/~fpiat/linux/debian/Etch_on_Thinkpad_T61/t61-alsa-1.0.13-backport-patch.diff | patch -p1 || exit
     ;;
esac
wget -O - http://hg.alsa-project.org/alsa-kernel/raw-diff/958b39f3e8dd/pci/hda/patch_analog.c | patch -p1 || exit
wget -O - http://hg.alsa-project.org/alsa-kernel/raw-diff/47ca87407c84/pci/hda/patch_analog.c | patch -p1 || exit
wget -O - http://hg.alsa-project.org/alsa-kernel/raw-diff/ca37aeeeb0ea/pci/hda/patch_analog.c | patch -p1 || exit
m-a --not-unpack build alsa || exit
#Install with : dpkg -i /usr/src/alsa-modules-$(uname -r)_1.0.14-1+$(uname -r).deb
#You might have to enable "speaker" (in the gnome mixer's "switch" tab ; Or in "alsamixer").
#You might need to add "options snd-hda-intel model=thinkpad" at the end of /etc/modprobe.d/alsa-base
按照脚本最后的提示,安装生成的deb包。

到现在还是不行,要在/etc/rc.local里填入如下内容。

modprobe -r snd-hda-intel
rmmod snd-pcm-oss
rmmod snd-mixer-oss snd-pcm
rmmod snd-page-alloc
rmmod snd-timer
rmmod snd
rmmod soundcore
depmod -a
modprobe snd-hda-intel

重启系统,或手动运行脚本。

然后用alsamixer将speeker的静音取消。将pcm的音量也调成大于0的值.

到目前还差一步,就是需要按一下ThinkVantage左边的音量调节键,这样才能
真正将静音取消,因为少了这一步浪费了我好多时间,明明alsamixer里已经将静音取消,mp3blaster也能播放,但就是没声音。


OK,现在就可以找一首MP3听了。
 
     
 
 
个人档案
 
lianchuntech
男, 28岁
北京 海淀区 
上次登录:
2008年 4月
加为好友
 
   
 
RSS订阅
 
 
 
     
 
鲜果推荐
 
   
 
文章分类
 
 
 
 
     
 
最近访客
 
 

wangchao719

chglele

鱼塘边的小孩

ichuangmei

jiaoyupx

sharpmark

2只粽子

iapache
     
 
其它
 
已有人次访问本空间
 
订阅RSS  什么是RSS?

您也想拥有这样的空间?请点此申请。
     


©2009 Baidu