File size: 8,843 Bytes
72268ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

# =========================================
#       IMPORT
# --------------------------------------

import rootpath

rootpath.append()

import collections

import attributedict.compat as compat


# =========================================
#       CONSTANTS
# --------------------------------------

DEFAULT_RESERVED_KEY_PREFIX = '__'
DEFAULT_RESERVED_KEY_SUFFIX = None


# =========================================
#       CLASSES
# --------------------------------------

class AttributeDict(dict):

    """
    :class:`~attributedict.collections.AttributeDict` is a seamlessly extended dictionary object (subclass of `dict`),
    with access to additional attribute get/set/delete of key/values.

    @example:

        data = AttributeDict({'foo': {'bar': [1, 2, 3]}})

        data.foo # => `{'bar': [1, 2, 3]}}`
        data.foo.bar # => `[1, 2, 3]`

        data.foo = {'baz': True}
        data.foo = # => `{'baz': True}`

        del data.foo

    """

    def __init__(self, entries = {}):
        entries = entries or {}
        entries = self._reject_reserved_keys(entries)

        super(AttributeDict, self).__init__(entries)

        self.update(entries)

    def _refresh(self):
        # HACK
        #
        # to make object encoding (e.g. `json` work), call `dict` constructor with updated data.
        #
        # it is terrible language design that this is the only way. >:/
        #
        # @see https://stackoverflow.com/questions/23088565/make-a-custom-class-json-serializable
        # @see https://stackoverflow.com/questions/2144988/python-multiple-calls-to-init-on-the-same-instance
        #
        # print('_refresh')
        dict.__init__(self, self.__dict__)

    def _reject_reserved_keys(self, object = {}):
        # NOTE:
        # tricky to override on the instance, because only special attribute `self__dict__` is not causing
        # recursive calls to `self.__getattr__`. =S
        reserved_key_prefix = DEFAULT_RESERVED_KEY_PREFIX
        reserved_key_suffix = DEFAULT_RESERVED_KEY_SUFFIX

        if isinstance(object, dict):
            for key, value in list(object.items()):
                is_reserved = False

                if len(reserved_key_prefix or ''):
                    is_reserved = key.startswith(reserved_key_prefix)

                if len(reserved_key_suffix or ''):
                    is_reserved = is_reserved or key.startswith(reserved_key_suffix)

                if is_reserved:
                    del object[key]

                else:
                    object[key] = self._reject_reserved_keys(object[key])

        return object

    def update(self, entries = {}, *args, **kwargs):
        """
        Update dictionary.

        @example:

            object.update({'foo': {'bar': 1}})

        """
        if isinstance(entries, dict):
            entries = self._reject_reserved_keys(entries)

        for key, value in dict(entries, *args, **kwargs).items():
            if isinstance(value, dict):
                self.__dict__[key] = AttributeDict(value)
            else:
                self.__dict__[key] = value

        self._refresh()

    def keys(self):
        return self.__dict__.keys()

    def values(self):
        return self.__dict__.values()

    def items(self, *args, **kwargs):
        return self.__dict__.items(*args, **kwargs)

    def iterkeys(self, *args, **kwargs):
        self.__dict__.iterkeys(*args, **kwargs)

    def itervalues(self, *args, **kwargs):
        self.__dict__.itervalues(*args, **kwargs)

    def iteritems(self, *args, **kwargs):
        self.__dict__.items(*args, **kwargs)

    def get(self, key, default = None):
        result = self.__dict__.get(key, default)

        return result

    def pop(self, key, value = None):
        result = self.__dict__.pop(key, value)

        self._refresh()

        return result

    def copy(self):
        return type(self)(self)

    def setdefault(self, key, default = None):
        result = self.__dict__.setdefault(key, default)

        self._refresh()

        return result

    def __getitem__(self, key):
        """
        Provides `dict` style property access to dictionary key-values.

        @example:

            value = object['key']

            # ignored
            object['__key__']

        """
        result = self.__dict__.__getitem__(key)

        self._refresh()

        return result

    def __setitem__(self, key, value):
        """
        Provides `dict` style property assignment to dictionary key-values.

        @example:

            object['key'] = value

            # ignored
            object['__key__'] = value

        """
        if isinstance(value, dict):
            value = AttributeDict(value)

        result = self.__dict__.__setitem__(key, value)

        self._refresh()

        return result

    def __delitem__(self, key):
        """
        Provides `dict` style property deletion to dictionary key-values.

        @example:

            del object['key']

            # ignored
            del object['__key__']

        """
        result = self.__dict__.__delitem__(key)

        self._refresh()

        return result

    def __getattr__(self, key):
        """
        Provides `object` style attribute access to dictionary key-values.

        @example:

            value = object.key

            # ignored
            object.__key__

        """
        try:
            return self.__getitem__(key)

        except Exception as error:
            raise AttributeError(error)

    def __setattr__(self, key, value):
        """
        Provides `object` style attribute assignment to dictionary key-values.

        @example:

            object.key = value

            # ignored
            object.__key__ = value

        """
        try:
            return self.__setitem__(key, value)

        except Exception as error:
            raise AttributeError(error)

    def __delattr__(self, key):
        """
        Provides `object` style attribute deletion to dictionary key-values.

        @example:

            del object.key

            # ignored
            del object.__key__

        """
        try:
            return self.__delitem__(key)

        except Exception as error:
            raise AttributeError(error)

    def __str__(self):
        """
        String value of the dictionary instance.
        """
        return str(self.__dict__)

    def __repr__(self):
        """
        String representation of the dictionary instance.
        """
        return repr(self.__dict__)

    def __dir__(self):
        return dir(type(self)) + list(self.__dict__.keys())

    def __iter__(self):
        """
        Iterate over dictionary key/values.
        """
        return iter(self.__dict__.keys())

    def __len__(self):
        """
        Get number of items.
        """
        return len(self.__dict__.keys())

    def __contains__(self, key):
        """
        Check if key exists.
        """
        return self.__dict__.__contains__(key)

    def __reduce__(self):
        """
        Return state information for pickling.
        """
        return self.__dict__.__reduce__()

    def __eq__(self, other):
        """
        Check dictionary is equal to another provided dictionary.
        """
        return self.__dict__.__eq__(other)

    def __ne__(self, other):
        """
        Check dictionary is inequal to another provided dictionary.
        """
        return self.__dict__.__ne__(other)

    def to_dict(self):
        return self.__class__.dict(self.__dict__)

    @classmethod
    def fromkeys(klass, keys, value = None):
        return AttributeDict(dict.fromkeys((key for key in keys), value))

    @classmethod
    def dict(klass, _dict = {}):
        if _dict is None:
            return None

        new_dict = {}

        for key, value in _dict.items():
            if isinstance(value, AttributeDict):
                new_dict[key] = AttributeDict.to_dict(value)
            else:
                new_dict[key] = value

        return new_dict


# =========================================
#       EXPORT
# --------------------------------------

attributedict = AttributeDict
attrdict = AttributeDict

__all__ = [
    'AttributeDict',

    'attributedict',
    'attrdict',
]


# =========================================
#       MAIN
# --------------------------------------

if __name__ == '__main__':

    data = {
        'a': {
            'b': {
                'c': [1, 2, 3]
            }
        }
    }

    object = AttributeDict(data)

    print('object = AttributeDict({0})\n'.format(data))

    print('object\n\n\t{0}\n'.format(object))
    print('object.a\n\n\t{0}\n'.format(object.a))
    print('object.a.b\n\n\t{0}\n'.format(object.a.b))
    print('object.a.b.c\n\n\t{0}\n'.format(object.a.b.c))