__init__.py 1.59 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
# coding: utf-8
class Result(dict):
    def __init__(self):
        super(Result, self).__init__()
Wei Shoulin's avatar
error    
Wei Shoulin committed
5
6
7
        self["code"] = 0
        self["message"] = ""
        self["data"] = None
Wei Shoulin's avatar
Wei Shoulin committed
8
    
Wei Shoulin's avatar
Wei Shoulin committed
9
    @property
Wei Shoulin's avatar
Wei Shoulin committed
10
    def success(self):
Wei Shoulin's avatar
Wei Shoulin committed
11
        return self["code"] == 0
Wei Shoulin's avatar
Wei Shoulin committed
12

Wei Shoulin's avatar
Wei Shoulin committed
13
    @property
Wei Shoulin's avatar
error    
Wei Shoulin committed
14
15
    def data(self):
        return self["data"]
Wei Shoulin's avatar
Wei Shoulin committed
16

Wei Shoulin's avatar
Wei Shoulin committed
17
    @property
Wei Shoulin's avatar
error    
Wei Shoulin committed
18
19
20
    def message(self):
        return self["message"]
        
Wei Shoulin's avatar
Wei Shoulin committed
21
22
23
    @staticmethod
    def error(code = -1, message = ""):
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
24
25
        r["code"] = code
        r["message"] = message
Wei Shoulin's avatar
Wei Shoulin committed
26
27
28
        return r

    @staticmethod
Wei Shoulin's avatar
Wei Shoulin committed
29
    def ok_data(data=None):
Wei Shoulin's avatar
Wei Shoulin committed
30
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
31
        r["data"] = data
Wei Shoulin's avatar
Wei Shoulin committed
32
33
34
35
36
        return r
        
    @staticmethod
    def ok_msg(message):
        r = Result()
Wei Shoulin's avatar
error    
Wei Shoulin committed
37
38
39
40
41
        r["message"] = message
        return r

    def append(self, k: str, v):
        self[k] = v
Wei Shoulin's avatar
Wei Shoulin committed
42
43
        return self

Wei Shoulin's avatar
C9    
Wei Shoulin committed
44
class Request(dict):
Wei Shoulin's avatar
Wei Shoulin committed
45
    def __init__(self, **kvargs):
Wei Shoulin's avatar
Wei Shoulin committed
46
        super(Request, self).__init__()
Wei Shoulin's avatar
Wei Shoulin committed
47
48
        for k, v in kvargs.items():
            self.append(k, v)
Wei Shoulin's avatar
Wei Shoulin committed
49
50
51
52
53
54
55
56

    @staticmethod
    def from_dict(d: dict):
        r = Request()
        for k, v in d.items():
            r.append(k, v)
        return r

Wei Shoulin's avatar
C9    
Wei Shoulin committed
57
58
59
60
61
62
63
64
    @staticmethod
    def from_tuple(data: tuple, columns: tuple):
        return Request.from_dict(dict(zip(columns, data)))
    
    @staticmethod
    def from_list(data: list, columns: tuple):
        return [Request.from_tuple(rec, columns) for rec in data]
    
Wei Shoulin's avatar
Wei Shoulin committed
65
    def append(self, k: str, v):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
66
        self[k] = v
Wei Shoulin's avatar
Wei Shoulin committed
67
68
69
        return self

    def __getattr__(self, attr):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
70
71
72
73
74
        if attr in self.keys():
            return self[attr]
        return ''

Record = Request