forked from Boris-code/feapder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperfect_dict.py
More file actions
50 lines (45 loc) · 1.02 KB
/
Copy pathperfect_dict.py
File metadata and controls
50 lines (45 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Created on 2021/4/8 11:32 上午
---------
@summary:
---------
@author: Boris
@email: boris_liu@foxmail.com
"""
class PerfectDict(dict):
"""
>>> data = PerfectDict(id=1, url="xxx")
>>> data
{'id': 1, 'url': 'xxx'}
>>> data.id
1
>>> data.get("id")
1
>>> data["id"]
1
>>> id, url = data
>>> id
1
>>> url
'xxx'
>>> data[0]
1
>>> data[1]
'xxx'
>>> data = PerfectDict({"id":1, "url":"xxx"})
>>> data
{'id': 1, 'url': 'xxx'}
"""
def __init__(self, _dict: dict = None, _values: list = None, **kwargs):
self.__dict__ = _dict or kwargs or {}
super().__init__(self.__dict__, **kwargs)
self.__values__ = _values or list(self.__dict__.values())
def __getitem__(self, key):
if isinstance(key, int):
return self.__values__[key]
else:
return self.__dict__[key]
def __iter__(self, *args, **kwargs):
for value in self.__values__:
yield value