|
1 | 1 | # -*- encoding: utf-8 -*- |
2 | 2 | import enum |
| 3 | +import six |
3 | 4 |
|
4 | 5 | from sqlalchemy import Integer, Column, ForeignKey, func, DateTime |
5 | 6 |
|
@@ -61,29 +62,90 @@ def reference_col(tablename, nullable=False, pk_name='id', **kwargs): |
61 | 62 |
|
62 | 63 |
|
63 | 64 | class EnumExt(enum.Enum): |
64 | | - """ serialize/deserialize sqlalchemy enum field |
| 65 | + """ Extension for serialize/deserialize sqlalchemy enum field. |
| 66 | +
|
| 67 | + Be sure ``type(key)`` is ``int`` and ``type(value)`` is ``str`` |
| 68 | + (``label = (key, value)``). |
| 69 | +
|
| 70 | + Examples:: |
| 71 | +
|
| 72 | + class TaskState(EnumExt): |
| 73 | + # label = (key, value) |
| 74 | + CREATED = (0, '新建') |
| 75 | + PENDING = (1, '等待') |
| 76 | + STARTING = (2, '开始') |
| 77 | + RUNNING = (3, '运行中') |
| 78 | + FINISHED = (4, '已完成') |
| 79 | + FAILED = (5, '失败') |
65 | 80 | """ |
| 81 | + |
66 | 82 | @classmethod |
67 | | - def strict_dump(cls, key, verbose=False): |
68 | | - pos = 1 if verbose else 0 |
69 | | - return cls[key].value[pos] |
| 83 | + def strict_dump(cls, label, verbose=False): |
| 84 | + """Get key or value by label. |
| 85 | +
|
| 86 | + Examples:: |
| 87 | +
|
| 88 | + TaskState.strict_dump('CREATED') # 0 |
| 89 | + TaskState.strict_dump('CREATED', verbose=True) # '新建' |
| 90 | +
|
| 91 | + Returns: |
| 92 | + int|str: Key or value, If label not exist, raise ``KeyError``. |
| 93 | + """ |
| 94 | + |
| 95 | + return cls[label].value[1 if verbose else 0] |
70 | 96 |
|
71 | 97 | @classmethod |
72 | | - def dump(cls, key, verbose=False): |
73 | | - ret = {'key': cls[key].value[0], 'value': cls[key].value[1]} |
| 98 | + def dump(cls, label, verbose=False): |
| 99 | + """Dump one label to option. |
| 100 | +
|
| 101 | + Examples:: |
| 102 | +
|
| 103 | + TaskState.dump('CREATED') # {'key': 0, 'value': '新建'} |
| 104 | +
|
| 105 | + Returns: |
| 106 | +
|
| 107 | + dict: Dict of label's key and value. If label not exist, |
| 108 | + raise ``KeyError``. |
| 109 | + """ |
| 110 | + |
| 111 | + ret = {'key': cls[label].value[0], 'value': cls[label].value[1]} |
74 | 112 | if verbose: |
75 | | - ret.update({'label': key}) |
| 113 | + ret.update({'label': label}) |
76 | 114 | return ret |
77 | 115 |
|
78 | 116 | @classmethod |
79 | 117 | def load(cls, val): |
80 | | - pos = 1 if isinstance(val, str) else 0 |
| 118 | + """Get label by key or value. |
| 119 | +
|
| 120 | + Examples:: |
| 121 | +
|
| 122 | + TaskState.load(4) # 'FINISHED' |
| 123 | + TaskState.load('新建') # 'CREATED' |
| 124 | +
|
| 125 | + Returns: |
| 126 | + str|None: Label. |
| 127 | + """ |
| 128 | + |
| 129 | + pos = 1 if isinstance(val, six.string_types) else 0 |
81 | 130 | for elem in cls: |
82 | 131 | if elem.value[pos] == val: |
83 | 132 | return elem.name |
84 | 133 |
|
85 | 134 | @classmethod |
86 | 135 | def to_opts(cls, verbose=False): |
| 136 | + """Enum to options. |
| 137 | +
|
| 138 | + Examples:: |
| 139 | +
|
| 140 | + opts = TaskState.to_opts(verbose=True) |
| 141 | + print(opts) |
| 142 | +
|
| 143 | + [{'key': 0, 'label': 'CREATED', 'value': u'新建'}, ...] |
| 144 | +
|
| 145 | + Returns: |
| 146 | + list: List of dict which key is `key`, `value`, label. |
| 147 | + """ |
| 148 | + |
87 | 149 | opts = [] |
88 | 150 | for elem in cls: |
89 | 151 | opt = {'key': elem.value[0], 'value': elem.value[1]} |
|
0 commit comments