10月 2012

2012年10月24日 星期三

Using Borg to instead of singleton in Python


上篇有說到可以使用singleton方式,當多個物件同時對一個資源做修改時,不會造成race condition,這篇將繼續介紹另一種更方便的方式 Borg



def borg(cls):
    cls._state = {}
    orig_init = cls.__init__
    def new_init(self, *args, **kwargs):
        self.__dict__ = cls._state
        orig_init(self, *args, **kwargs)
    cls.__init__ = new_init
    return cls

class animal(object):
    def __init__(self,name):
        self.name = name

    def assign_count(self):
        self.count = 0

    def pringInfo(self):
        self.count = self.count + 1
        print "%s appear %s times"%(self.name,self.count)

animal = borg(animal)

if __name__ == '__main__':
    b1 = animal("dog")
    b1.assign_count()
    b1.pringInfo()
    b2 = animal("cat")
    b2.pringInfo()


執行結果:

root@ubuntu:/mnt/test# python borg.py
dog appear 1 times
cat appear 2 times
cat appear 3 times




2012年10月23日 星期二

Singleton in Python


運用Singleton可以讓物件同時間只會產生一份,這樣就可以避免共用資源的race condition發生。



def singleton(cls):
    instances = {}

    def getinstance(*args, **kwds):
        return instances.setdefault(cls, cls(*args, **kwds))

    return getinstance

class Foo(object):
    def __init__(self, attr=1):
        self.attr = attr
        self.counter = 0

    def add_count(self):
        self.counter = self.counter + 1

Foo = singleton( Foo )

if __name__ == "__main__":
    ins1 = Foo(2)
    ins1.add_count()
    print "Foo(2) -> id(ins)=%d, ins.attr=%d,%s" % (id(ins1), ins1.attr, ('error','ok')[ins1.attr==2])
    print "Foo(2) counter: %d" %ins1.counter
    ins2 = Foo(3)
    ins2.add_count()
    print "Foo(3) -> id(ins)=%d, ins.attr=%d,%s" % (id(ins2), ins2.attr, ('error','ok')[ins2.attr==2])
    print "Foo(3) counter: %d" %ins2.counter
    ins2.attr = 5
    print "ins.attr=5 -> ins.attr=%d, %s" %(ins2.attr,('error','ok')[ins2.attr==5])



執行結果:
root@ubuntu:/mnt/test# python singleton.py
Foo(2) -> id(ins)=140287766113104, ins.attr=2,ok
Foo(2) counter: 1
Foo(3) -> id(ins)=140287766113104, ins.attr=2,ok
Foo(3) counter: 2
ins.attr=5 -> ins.attr=5, ok

2012年10月21日 星期日

Monkey Patching in Python


這陣子接了一個專案

而上層又提出要增加unitest的功能

但是如果依照舊程式碼要來改的話

就會改到天荒地老

所以這邊使用了一個小撇步 Monkey Patching

透過這個方式就可以把import的Module用自己的module給替換掉

非常方便

main.py
#!/usr/bin/env python
import sys


if __name__ == '__main__':
        import os
        from test import os as fake_os
        old = os
        sys.modules['os'] = fake_os

        import os
        os.system("touch a")

        sys.modules['os'] = old



os.py
#!/usr/bin/env python

def system(cmd):
        print cmd


Refactoring in Python


接觸了一陣子的Python

開始想要讓自己的程式碼能更有彈性化與更好Debug

所以在網路上搜尋了一下 Design Pattern for Python

真的有一堆東西

其中剛好有人寫到 Refactoring Python

就開始著手修改自己的Coding Style

底下會先用一個列出 動物有幾隻腳 的程式做為範例

在拿Refactoring後的程式與一開始常寫的東西做對比


重構前:
#!/usr/bin/env python

def animal_info(animal_type):
        feet = 0
        if animal_type == "lion":
                feet=4
        elif animal_type == "duck":
                feet=2
        elif animal_type == "dog":
                feet=4
        print "%s has %s feet"%(animal_type,feet)


if __name__ == "__main__":

        animal_info("lion")
        animal_info("duck")
        animal_info("dog")



重構後:
#!/usr/bin/env python

def animal_inspect(animal_type):
        animal_dict = {
        "dog" : { "feet":"4"},
        "duck" : { "feet":"2"},
        "lion" : { "feet":"4"}
        }

        animal_info = animal_dict[animal_type]
        animal_feet = animal_info["feet"]
        print "%s has %s feet"%(animal_type,animal_feet)

def main():
        animal_array = ["lion","duck","dog"]
        for animal_type in animal_array:
                animal_inspect(animal_type)


if __name__ == "__main__":
        main()



雖然重構後的架構會稍微的複雜些

但是如果要修改或是擴增上 就會便利許多

尤其是if else當越寫越長

就會越來越難debug

透過dict方式來替代if else的確是很棒的方式