Xupeng's blog

Sep 27, 2006 - 1 minute read - Comments

列出子类的新方法

刚才在python邮件列表中看到有人提出这样的一个问题:

I am writing a library in which I need to find the names of methods
which are implemented in a class, rather than inherited from another
class. To explain more, and to find if there is another way of doing
it, here is what I want to do: I am defining two classes, say A and B,
as:

class A(object):
   def list_cmds(self):
       ’implementation needed’
       ?
   def __init__(self):
   … (rest of class)

class B(A):
   def cmd1(self, args):
       pass
   def cmd2(self, args):
       pass

I need an implementation of list_cmds in A above so that I can get a
result:

>>> b=B()
>>> b.list_cmds()
['cmd1','cmd2']                    #order not important

I will be happy if anybody can point to me any way of doing it, using
class attributes, metaclasses or otherwise. What I don’t want to do is
modifying class B, which contains just the cmds, if possible.
想了一下我使用下面的代码完成了他所需要的功能:
#!/usr/bin/python
#filename: p.y

class A(object):
    def listcmds(self):
        allInChild = dir(self)
        allInParent = dir(A)

        allOnlyInChild = []
        for att in allInChild:
            if att not in allInParent:
                allOnlyInChild.append(att)

        return allOnlyInChild

    def __init__(self):
        pass

class B(A):
    def m1(self):
        pass

    def m2(self):
        pass
b = B()
print b.listcmds()
功能是实现了,就是不知道方法是不是笨了点。等我把邮件发出去发现在几分钟前也有人回复了这个邮件,其中一个解决方案和我的几乎是一个样的,也没有出现更高明的方法,看来我的想法还是有可取之处的,不早了,早上起来再看看有没有人给出更好的方案。

Sep 12, 2006 - 1 minute read - Comments

把gnome升级到2.16的一个参考方案

昨天参考了网上一份升级gnome到2.14的文档,把gnome从2.14升级到了2.16,方法也很简单,我写的比较简略,需要更详细的介绍和解释,可以参考原文档(http://gentoo-wiki.com/HOWTO_Update_to_GNOME_unstable):

gnome2.16 9月6号才进入portgae吧,首先要更新一下portage:

代码:
emerge --sync
先把gnome2.16相关的包给unmask了:
代码:
sed -n '/# New GNOME 2.16 gtk# components/,/# End GNOME 2.16 mask/p' /usr/portage/profiles/package.mask >> /etc/portage/package.unmask
把这些包加入/etc/portage/package.keywords中:
代码:
sed -n '/# New GNOME 2.16 gtk# components/,/# End GNOME 2.16 mask/p' /usr/portage/profiles/package.mask |awk '{ print $1 " ~x86" }' >> /etc/portage/package.keywords
把gnome依赖的包也加入到keywords中:
代码:
ACCEPT_KEYWORDS="~x86" emerge -p =gnome-base/gnome-2.16* | grep \[ebuild |awk '{ print $4 }' | sed -e 's/^/~/' -e 's/-r[0-9]{1,2}//' |uniq -u | sort >>/etc/portage/package.keywords
最后,升级gnome:
代码:
emerge -DuN gnome

注:要把~x86替换为与你的平台相对应的关键字

Sep 10, 2006 - 1 minute read - Comments

升级gentoo到2006.1

gentoo发布了2006.1,系统有很久没有更新过了,借此机会更新到2006.1吧。

因为这是一个新的release,先更改系统的profile:
rm /etc/make.profile
ln -s /usr/protage/profiles/default-linux/x86/2006.1 /etc/make.profile

接下来下载需要更新的软件包:
emerge -feuDv system
emerge -feuDv world

更新system:
emerge -euDv system
emerge -euDv world


这又是一个漫长的过程,估计明天要工作一整天了。