-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotdict.py
executable file
·44 lines (37 loc) · 1.42 KB
/
dotdict.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 17 December 2012 by Eiríkur Hallgrímsson
# Character geek: The 2nd and 3rd i in my name have diacriticals.
# If there is a file-encoding cookie, Python will accept this. (2.x, too)
# 18 December 2012 EH Added StrictDotDict which doesn't allow new keys.
# 28 March 2013 EH PEP-8 compliance, requested by RackSpace
"""Simple Library to hold the DotDict class."""
class DotDict(dict):
"""
Enable use of Javascript/Coffeescript style object notation for dicts.
The __missing__ method allows chaining creation of nested DotDicts:
a = DotDict() ; a.foo.bar = 'content'
Limitation: Expressing keys as attributes limits keys to valid Python
identifiers.
"""
__getattr__ = dict.__getitem__
__delattr__ = dict.__delitem__
def __setattr__(self, key, value):
if hasattr(value, 'keys'):
value = DotDict(value)
self[key] = value
def __missing__(self, key):
self[key] = DotDict()
return self[key]
class StrictDotDict(DotDict):
"""Strict form (subclass) of DotDict. Does not allow new keys."""
def __setattr__(self, key, value):
if key in self:
if hasattr(value, 'keys'):
value = DotDict(value)
self[key] = value
else:
raise KeyError
# Fails PyLint for unused arguments, but this is the API.
def __missing__(self, key):
raise KeyError