留給晚上分享用的python代碼

來源:互聯網
上載者:User

#encoding=utf8
__author__ = 'Administrator'

#如果是直接運行檔案的話
if __name__=="__main__":
    print("main")

#怎麼做效能測試
import time
def timer(reptimes,func,*pargs,**kargs):
    start=time.clock()
    for i in range(reptimes):
        func(*pargs,**kargs)
    return time.clock()-start
def test(test):
    [i for i in range(test)]
print(timer(100,test,100))

#字串格式化
print("{0}-{1}-{2}".format(1,1,1))
print("{name}-{age}".format(name="name",age="age"))

#代替linq
x=range(10)
print(str(x))
#解析運算式 速度最快
x1=[i for i in x if i%2==0]
print(x1)
x2=x[0::2]
print(x2)
x3=list(map((lambda i:i%2==0 and i or None),x))
print(x3)
x4=list(filter(lambda i:i%2==0,x))
print(x4)

#普通類
class Person(object):
    def __init__(self,name,age):
        self._name=name
        self._age=age
    @property
    def name(self):
        return self._name
    @name.setter
    def name(self,value):
        self._name=(value+"...")
    @name.deleter
    def name(self):
        raise RuntimeError("no")
    @staticmethod
    def Run(someone):
        if isinstance(someone,Person):
            print("run")
        else:
            raise RuntimeError("只有人能跑")
    def __str__(self):
        return "my name is "+self._name+" i'm "+str(self._age)
john=Person("john",11)
print(john)
Person.Run(john)
#Person.Run(1)
john.name="john"
print(john)
#del john.name

#示範繼承
class Man(Person):
    def __init__(self,name,age,sex):
        super(Man,self).__init__(name,age)
        self.sex=sex
    def __str__(self):
        return super(Man,self).__str__()+self.sex
man=Man("man",21,"男")
print(man)

from abc import abstractmethod,abstractproperty,ABCMeta
#抽象類別
class abClass():
    __metaclass__=ABCMeta
    @abstractmethod
    def abMethod(self):
        pass
    @abstractproperty
    def abPr(self):
        pass
#abEntity=abClass()

#示範AOP
class trace(object):
    def __init__(self,func):
        self.func=func
    def __call__(self, *args, **kwargs):
        print("------begin------")
        print(dir(self.func))
        print(self.func.__code__)
        self.func(*args,**kwargs)
        print("----------end----------")
#示範AOP
def mydecorator(func):
    def _mydecorator(*pargs,**kargs):
        print("ffff")
        res=func(*pargs,**kargs)
        return res
    return _mydecorator

def complexDecorator(name):
    def _mydecorator(func):
        def _mydecorator(*pargs,**kargs):
            print(name)
            res=func(*pargs,**kargs)
            return res
        return _mydecorator
    return _mydecorator

@complexDecorator("bw")
@mydecorator
@trace
def demoTrace():
    print("me")
demoTrace()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.