day27软件开发的规范

时间:2022-03-17 20:48:07
========================================开发规范=======================================
    1.软件开发规范
        1.1目录结构----有组织的目录结构
        Project-
            bin
                start.py#启动脚本
            conf
                setting.py#设置
            db
                mysql.sql#数据库
            lib
                commons.py#公共的类库
            log
                log.py#日志信息
            src/core
                #主要的核心的程序逻辑
========================================call方法=======================================
    class Foo():
        pass
        def __call__(self, *args, **kwargs):
            print('我执行啦obj()')

    f1=Foo()
    f1()#调用的就是__call__方法
========================================doc属性=========================================
    class Foo():
        '我是描述信息'
        pass
    class Bar():
        pass
    print(Foo.__dict__)
    print(Bar.__dict__)
========================================反射的详细介绍===================================
    import test as obj
    print(obj)
    print(hasattr(obj,'say_hi'))
    print(hasattr(obj,'say_hissssssssssssssss'))

    if hasattr(obj,'say_hi'):
        func=getattr(obj,'say_hi')
        func()
    else:
        print('其他的函数')

    import sys
    print('来自自己的函数')
    b1=sys.modules[__name__]


    # import fangse as obj1
    import sys
    obj1=sys.modules[__name__]
    print('你好',hasattr(obj1,'x'))
========================================file文件========================================
    import sys
    #print(sys.argv)#获取平台获取当前的文件路径
    sys.exit()
    sys.version
    sys.path
    sys.stdin
    sys.stdout
    sys.stderror
========================================getattribute====================================
    class Foo():
        def __init__(self,x):
            self.x=x

        def __getattr__(self, item):
            print('执行的是我')
        def __getattribute__(self, item):
            print('执行的是getattribute')
            raise AttributeError('抛出异常啦')

    f1=Foo(10)
    f1.xxx#执行找不到的属性访问执行getattr方法
    f1.x
    #调用首先执行—- __getattribute方法,不存在的情况下执行
========================================getattribute补充================================
    print('----------------------')
    print('----------------------')
    print('----------------------')
    # class Foo():
    #     def __init__(self,x):
    #         self.x=x
    #
    #     def __getattr__(self, item):
    #         print('执行的是我')
    #     def __getattribute__(self, item):
    #         print('执行的是getattribute')
    #         raise AttributeError('抛出异常啦')
    #
    # f1=Foo(10)
    # f1.xxx#执行找不到的属性访问执行getattr方法
    # f1.x
    # #调用首先执行—- __getattribute方法,不存在的情况下执行
    raise AttributeError('自己找事')

    print('----------------------')
    print('----------------------')
    print('----------------------')
========================================index方法=======================================
    from lib.aa import C
    c1=C()
    print(c1.name)
    print(c1.__module__)#来之哪一个模块
    print(c1.__class__)#来之哪一个类产生

========================================item系列方法====================================
    class Foo():
        def __getitem__(self, item):
            print('getitem')
        def __setitem__(self, key, value):
            print('setitem')
        def __delitem__(self, key):
            print('delitem')

    f1=Foo()
    print(f1.__dict__)
    #通过点访问的死 getattr
    #当调用字典的时候才调用item

    f1['name']='eoulen'
    f1['age']=18
    print('---->>>',f1.__dict__)
    del f1['name']
    print(f1.__dict__)
========================================slots方法========================================
    #__slots__是什么:是一个类变量,变量值可以使列表,元组,
    class Foo():
        __slots__ = {'name','age'}
    f1=Foo()

    print(Foo.__slots__)
    print(f1.__slots__)

    #使用__slots主要是进行内存的减小
    f1.name='oulen'
    f1.age=18
    #f1.gender='male'/////AttributeError: 'Foo' object has no attribute 'gender'
    #当当前的类存在__slots__ , __dict__不会被使用
========================================内置函数=========================================
    class Foo():
        pass
    class Bar(Foo):
        pass
    f1=Foo()
    print(isinstance(f1,Foo))#检查obj是否是CLS类的实例
    print(issubclass(Foo,Bar))#检查Foo是否是继承BAR
========================================描述符===========================================
    class Foo():
        def __get__(self, instance, owner):
            print('get方法')
        def __set__(self, instance, value):
            print('set方法')
            instance.__dict__['x']=value
        def __delete__(self, instance):
            print('delete方法')
    f1=Foo()
    f1.name='oulen'
    #当前的实例方法不会触发这个方法
    #用来代理另外一个类的属性的时候
    class Bar():
        x=Foo()#当另外一个类的属性是另外一个类
        #在何地
        def __init__(self,n):
            self.x=n
    b1=Bar(11)
    print(b1.x)
    print(b1.__dict__)
    b1.x=18
    #del b1.x

    print(b1.__dict__)

    #数据描述符号
    #有__set__数据描述符
    #没有__set__就是非数据描述符
    #描述符必须在新式类中


    #注意事项:
    #1.类属性
    #2.数据描述符
    #3.实例属性
    #4.非数据描述符
    #5.找不到属性触发__getattr__()
========================================描述符优先级======================================
    class Foo:
        def __get__(self, instance, owner):
            print('===>get方法')
        def __set__(self, instance, value):
            print('===>set方法',instance,value)
            # instance.__dict__['x']=value #b1.__dict__
        def __delete__(self, instance):
            print('===>delete方法')


    class Bar:
        x=Foo() #在何地?

    Bar.x=1
    print(Bar.x)
    #----------------优先级的定义
    #注意事项:
    #1.类属性
    #2.数据描述符
    #3.实例属性
    #4.非数据描述符 没有__set__方法
    #5.找不到属性触发__getattr__()
    b1=Bar()#
    b1.x #get
    b1.x=1 #set
    del b1.x #delete

========================================改变对象的字符串显示===============================
    # l=list('hello')
    # print(1)
    # file=open('test.txt','w')
    # print(file)
    #
    # class Foo():
    #     def __init__(self,name,age):
    #         self.name=name
    #         self.age=age
    #     def __str__(self):
    #         return '名字是%s 年龄是%s'%(self.name,self.age)
    #     # def __str__(self):
    #     #     return "自定制的显示方式"
    # f1=Foo('engon',18)
    # print(f1)
    # print(f1.name)#--str __ f1.str
    # x=str(f1)
    # print(x)


    class Foo():
        def __init__(self,name,age):
            self.name=name
            self.age=age
        # def __str__(self):
        #     return '名字是%s 年龄是%s'%(self.name,self.age)
        # def __str__(self):
        #     return "自定制的显示方式"
        def __repr__(self):
            return '名字是%s 年龄是%s'%(self.name,self.age)
    f1=Foo()
    #repl 用于在解释器使用
    #str 使用的是print 使用   print -str _>>  没有的情况下选择到 __repl__的方法

    #
    '''
    str 函数或者是print 函数 ——》》 obj.__str__()
    repr 或是解释器交互式——————》》obj.__repr__()
    如果定义的__str__没有被定义,那么就使用__repr__来代替输出
    注意这两者的方法的返回值必须是字符串,否则抛出异常

    '''
========================================析构方法==========================================
    class Foo():
        def __init__(self,name):
            self.name=name
        def __del__(self):
            print('我执行啦')
    f1=Foo('oulen')
    del f1.name
    print('-----------》》》》》》')
========================================自定制格式化方法===================================
    # x='{0}{0}{0}'.format('dog')
    #
    # print(x)
    #
    formate_dic={
        'ymd':'{0.year}:{0.mon}:{0.day}',
        'm-d-y':'{0.year}:{0.mon}:{0.day}',
        'y:m:d':'{0.mon}-{0.day}-{0.year}'
    }
    class Date:
        def __init__(self,year,mon,day):
            self.year=year
            self.mon=mon
            self.day=day
        def __format__(self, format_spec):
            print('我执行啦')
            print('我执',format_spec)
            if not format_spec or format_spec not in formate_dic:
                format_spec='ymd'
            fm=formate_dic[format_spec]
            return fm.format(self)
            #return '{0.year}:{0.mon}:{0.day}'.format(self)

    d1=Date(2016,12,26)
    format(d1)
    print(format(d1))
    print(format(d1,'y:m:d'))
    print(format(d1,'m-d-y'))
    print(format(d1,'m-d'))
    #
    # x='{0.year}:{0.mon}:{0.day}'.format(d1)
    # print(x)
    # y='{0.year}:{0.mon}:{0.day}'.format(d1)
    # z='{0.mon}-{0.day}-{0.year}'.format(d1)
    # print(x)
    # print(y)
    # print(z)
    #

========================================迭代器协议=========================================
    class Foo():
        def __init__(self,n):
            self.n=n
        def __iter__(self):
            print('你好')
            return self
        def __next__(self):
            if self.n==13:
                raise StopIteration('终止啦')
            self.n+=1
            return self.n


    # l=list('hello')
    # for i in l:
    #     print(i)
    #
    f1=Foo(10)
    print(f1.__next__())
    print(f1.__next__())
    print(f1.__next__())
    print(f1.__next__())
    print(f1.__next__())
    print(f1.__next__())
    print(f1.__next__())
    # for i in f1:#iter(f1)---------f1.__iter()
    #     print(i)
========================================迭代器实现斐波那契数列==============================
    class Fib():
        def __init__(self):
            self._a=1
            self._b=1

        def __iter__(self):
            return self
        def __next__(self):
            if self._a > 5000:
                raise StopIteration('终止啦')
            self._a,self._b=self._b,self._a+self._b
            return self._a
    f1=Fib()
    print(next(f1))
    print(next(f1))
    print(next(f1))
    print(next(f1))
    print(next(f1))
    print(next(f1))
    for i in f1:
        print(i)