-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjns.py
More file actions
155 lines (122 loc) · 3.84 KB
/
objns.py
File metadata and controls
155 lines (122 loc) · 3.84 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2024 KuraLabs S.R.L
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Transform a Python dictionary into an object.
"""
__author__ = 'KuraLabs S.R.L'
__email__ = 'info@kuralabs.io'
__version__ = '1.1.0'
from copy import deepcopy
from collections.abc import Mapping
try:
from pprintpp import pformat
except ImportError:
from pprint import pformat
def update(to_update, update_with):
"""
Recursively update a dictionary with another.
:param dict to_update: Dictionary to update.
:param dict update_with: Dictionary to update with.
:return: The first dictionary recursively updated by the second.
:rtype: dict
"""
for key, value in update_with.items():
if isinstance(value, Mapping):
to_update[key] = update(
to_update.get(key, type(value)()),
value,
)
else:
to_update[key] = value
return to_update
class Namespace:
"""
Transform a Python dictionary into an object.
"""
def __init__(self, *args, **kwargs):
spread = list(args) + [kwargs]
assert all(
isinstance(element, Mapping)
for element in spread
)
head, *tail = spread
for element in tail:
update(head, element)
data = type(head)(
(
key, (
Namespace(value)
if isinstance(value, Mapping)
else value
)
)
for key, value in head.items()
)
super().__setattr__('_data', data)
def __getattr__(self, attr):
try:
return self[attr]
except KeyError as e:
raise AttributeError(e)
def __setattr__(self, attr, value):
self[attr] = value
def __iter__(self):
data = super().__getattribute__('_data')
for key, value in data.items():
if isinstance(value, self.__class__):
yield key, type(data)(value)
continue
yield key, value
def __getitem__(self, key):
data = super().__getattribute__('_data')
return data[key]
def __setitem__(self, key, value):
if isinstance(value, Mapping):
value = Namespace(value)
data = super().__getattribute__('_data')
data[key] = value
def __contains__(self, key):
data = super().__getattribute__('_data')
return key in data
def __repr__(self):
data = super().__getattribute__('_data')
return pformat(type(data)(self))
def __str__(self):
return repr(self)
def update(self, update_with):
data = super().__getattribute__('_data')
if isinstance(update_with, self.__class__):
update_with = type(data)(update_with)
to_update = type(data)(self)
update(to_update, update_with)
data = type(data)(
(
key, (
Namespace(value)
if isinstance(value, Mapping)
else value
)
)
for key, value in to_update.items()
)
super().__setattr__('_data', data)
def copy(self):
data = super().__getattribute__('_data')
return Namespace(deepcopy(data))
__all__ = [
'Namespace',
]