|
97 | 97 | """ |
98 | 98 | __version__ = '2.0.9' |
99 | 99 | __all__ = [ |
100 | | - 'dump', 'dumps', 'load', 'loads', 'AttrDict', |
| 100 | + 'dump', 'dumps', 'load', 'loads', |
101 | 101 | 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', |
102 | 102 | ] |
103 | 103 |
|
@@ -357,53 +357,3 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None, |
357 | 357 | if parse_constant is not None: |
358 | 358 | kw['parse_constant'] = parse_constant |
359 | 359 | return cls(**kw).decode(s) |
360 | | - |
361 | | -class AttrDict(dict): |
362 | | - """Dict like object that supports attribute style dotted access. |
363 | | -
|
364 | | - This class is intended for use with the *object_hook* in json.loads(): |
365 | | -
|
366 | | - >>> from json import loads, AttrDict |
367 | | - >>> json_string = '{"mercury": 88, "venus": 225, "earth": 365, "mars": 687}' |
368 | | - >>> orbital_period = loads(json_string, object_hook=AttrDict) |
369 | | - >>> orbital_period['earth'] # Dict style lookup |
370 | | - 365 |
371 | | - >>> orbital_period.earth # Attribute style lookup |
372 | | - 365 |
373 | | - >>> orbital_period.keys() # All dict methods are present |
374 | | - dict_keys(['mercury', 'venus', 'earth', 'mars']) |
375 | | -
|
376 | | - Attribute style access only works for keys that are valid attribute names. |
377 | | - In contrast, dictionary style access works for all keys. |
378 | | - For example, ``d.two words`` contains a space and is not syntactically |
379 | | - valid Python, so ``d["two words"]`` should be used instead. |
380 | | -
|
381 | | - If a key has the same name as dictionary method, then a dictionary |
382 | | - lookup finds the key and an attribute lookup finds the method: |
383 | | -
|
384 | | - >>> d = AttrDict(items=50) |
385 | | - >>> d['items'] # Lookup the key |
386 | | - 50 |
387 | | - >>> d.items() # Call the method |
388 | | - dict_items([('items', 50)]) |
389 | | -
|
390 | | - """ |
391 | | - __slots__ = () |
392 | | - |
393 | | - def __getattr__(self, attr): |
394 | | - try: |
395 | | - return self[attr] |
396 | | - except KeyError: |
397 | | - raise AttributeError(attr) from None |
398 | | - |
399 | | - def __setattr__(self, attr, value): |
400 | | - self[attr] = value |
401 | | - |
402 | | - def __delattr__(self, attr): |
403 | | - try: |
404 | | - del self[attr] |
405 | | - except KeyError: |
406 | | - raise AttributeError(attr) from None |
407 | | - |
408 | | - def __dir__(self): |
409 | | - return list(self) + dir(type(self)) |
0 commit comments