• 留给晚上分享用的python代码


    #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()

  • 相关阅读:
    asp.net 汉字转拼音类
    NET分页实现及代码
    Web.config配置文件详解(新手必看) (转载)
    偶开通博客啦
    转帖不会乱码的,powershell网络蜘蛛
    ConvertFrom-String 命令研究
    powershell玩转xml之20问
    powershell 判断操作系统版本 命令
    powershell加win的dns服务器,解决网站负载均衡问题
    PowerShell并发控制-命令行参数之四问
  • 原文地址:https://www.cnblogs.com/brightwang/p/2068547.html
Copyright © 2020-2023  润新知