美文网首页
DRF源码基础

DRF源码基础

作者: 錦魚 | 来源:发表于2019-11-18 15:27 被阅读0次

    APIView的简单过程

    class Request(object):
        def __init__(self, obj):
            self.obj = obj
        @property      # 方法变属性
        def user(self):
            return self.obj.authticate()
    
    class Auth(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
        def authticate(self):
            return self.name
    
    class APIView(object):
        def dispatch(self):
            self.f2()
        def f2(self):
            a = Auth('Good boys', 18)
            b = Auth('Hello world', 22)
            req = Request(b)
            print(req.user)
    
    obj = APIView()
    obj.dispatch()
    

    通过列表生成式,生成对象
    class first(object):
        pass
    class second(object):
        pass
    
    cls = [item() for item() in [first, second]]
    

    封装
      1. 把同一类方法封装至一个类中
      1. 把数据封装至一个类中

    # 与文件相关操作的类
    class File:
        ... 文件增删改查方法
    # 与数据库相关操作的类
    class DB:
        ... 数据库方法
    

    class Data:
        def __init__(self, a1, a2):
            self.a1 = a1
            self.a2 = a2
        def get:     ...
        def delete:  ...
        def update:  ...
        def add:     ...
    
    obj1 = Data(11,12)
    obj2 = Data(43,99)
    

    反射
    • 通过字符串的映射
    • 动态操作
    hasattr()
    • 用于判断对象是否包含对应属性

    class A(object):
        x = 1
        y = 2
        z = 3
    
    a = A()
    print(hasattr(a, 'x'))    # True
    print(hasattr(a, 'y'))    # True
    print(hasattr(a, 'z'))    # True
    print(hasattr(a, 'w'))    # False
    
    getattr()
    • 函数用于返回一个对象属性值, 该属性必须存在

    class A(object):
        bar = 1
    
    a = A()
    getattr(a, 'bar')    # 1
    
    setattr()
    • setattr()函数对应 getattr(), 用于设置属性值

    class A(object):
        bar1 = 1
    
    a = A()
    print(getattr(a, 'bar1'))    # 1
    setattr(a, 'bar1', 5)        # 5
    setattr(a, 'bar2', 6)        # 6
    
    delattr()
    • delattr 函数用于删除属性

    class A(object):
        x = 1
        y = 2
        z = 3
    
    hasattr(A, 'z')    # True
    delattr(A, 'z')
    hasattr(A, 'z')    # False
    

    反射应用例子:
    def sing(self):
        print('{}: 在唱歌'.format(self.name))
    
    class User(object):
        def __init__(self, name):
            self.name = name
        def run(self):
            print('{}: 在跑步'.format(self.name))
        def eat(self):
            print('{}: 在吃饭'.format(self.name))
    
    p = User('张三')
    send_choices = input('>>>>>>')
    if  hasattr(p, send_choices):
        func = getattr(p, send_choices)
        func()
    else:
        setattr(p, send_choices, sing) # 对象/ 名字/ 属性
        func = getattr(p, send_choices)
        func()
    
    反射实际例子:
    import request
    
    class HTTP(object):
        def get(self):
            res = request.get(url)
            return res.text
    
        def post(self):
            ...Post的行为...
            res = request.get(url)
            return res.text
    
    url = 'http://www.baidu.com'
    method = input(">>>请输入请求方式").lower()
    h = HTTP()
    if hasattr(h, method):        # 若有方法
        res = getattr(h, method)  # 获取方法
        res = func(url)
        print(res)
    else:
        print('请求方式有误')
    

    相关文章

      网友评论

          本文标题:DRF源码基础

          本文链接:https://www.haomeiwen.com/subject/gftbictx.html