__init__.py 853 Bytes
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
42
        r["message"] = message
        return r

    def append(self, k: str, v):
        self[k] = v
        return self