71
|
1 #!/usr/bin/env python3
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 class JSDict(dict):
|
|
5 """
|
|
6 Make a dict that acts something like a JavaScript object, in that we can
|
|
7 use both x["name"] and x.name to access something. Note that the latter
|
|
8 method fails for keys like x["get"] that duplicate dict methods, and keys
|
|
9 like x["1"] which are not legal Python identifiers.
|
|
10 """
|
|
11 def __getattr__(self, name):
|
|
12 return self[name]
|
|
13
|
|
14 def __setattr__(self, name, value):
|
|
15 self[name] = value
|
|
16
|
|
17 def __delattr__(self, name):
|
|
18 del self[name]
|
|
19
|
|
20 @classmethod
|
|
21 def from_dict(cls, d):
|
|
22 return cls(d)
|