-
Notifications
You must be signed in to change notification settings - Fork 0
/
frozen_json.py
75 lines (58 loc) · 1.89 KB
/
frozen_json.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
from collections import abc
import keyword
class FrozenJSON:
"""
以动态属性 访问 json 数据
例如 d = {'name': 'xxx', 'identify': 'xxx'},
f = Class(d)
f.name = 'xxx'
"""
def __init__(self, mapping):
self.__data = dict(mapping)
def __getattr__(self, name):
if hasattr(self.__data, name):
print("Test hasattr......")
return getattr(self.__data, name)
else:
return FrozenJSON.build(self.__data[name])
@classmethod
def build(cls, obj):
if isinstance(obj, abc.Mapping):
return cls(obj)
elif isinstance(obj, abc.MutableSequence):
return [cls.build(item) for item in obj]
else:
return obj
class FrozenJSON1:
def __new__(cls, arg):
print("classss: ", cls, arg)
if isinstance(arg, abc.Mapping):
return super().__new__(cls)
elif isinstance(arg, abc.MutableSequence):
return [cls(item) for item in arg]
else:
print("argssss : ", arg)
return arg
def __init__(self, mapping):
self.__data = dict(mapping)
# 判断键值是否为关键词,经测试没效果
# for key, value in mapping.items():
# if keyword.iskeyword(key):
# key += '_'
# print("^^^^^^^^: ", key)
# self.__data[key] = value
def __getattr__(self, name):
if hasattr(self.__data, name):
return getattr(self.__data, name)
else:
print("######: ", self.__data)
return FrozenJSON1(self.__data[name])
if __name__ == '__main__':
f = FrozenJSON({'name': 'jack'})
print(f, type(f))
print(f.name)
print(f.items(), f.keys(), '\n')
ff = FrozenJSON1({'name': 'Tom', 'async': 2})
print(ff.name)
print(getattr(ff, 'async'))