]> Zhao Yanbai Git Server - acecode.git/commitdiff
add dict2object.py
authoracevest <zhaoyanbai@126.com>
Wed, 3 Dec 2014 10:41:23 +0000 (18:41 +0800)
committeracevest <zhaoyanbai@126.com>
Wed, 3 Dec 2014 10:41:23 +0000 (18:41 +0800)
learn/python/dict2object.py [new file with mode: 0755]

diff --git a/learn/python/dict2object.py b/learn/python/dict2object.py
new file mode 100755 (executable)
index 0000000..2b3daa5
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: dict2object.py
+#      Author: Zhao Yanbai
+#              Wed Dec  3 18:40:09 2014
+# Description: none
+# ------------------------------------------------------------------------
+import json
+
+js = json.loads('{"s" : "Hello World."}')
+
+
+
+def obj_dic(d):
+    top = type('new', (object,), d)
+    seqs = tuple, list, set, frozenset
+    for i, j in d.items():
+        if isinstance(j, dict):
+            setattr(top, i, obj_dic(j))
+        elif isinstance(j, seqs):
+            setattr(top, i, 
+            type(j)(obj_dic(sj) if isinstance(sj, dict) else sj for sj in j))
+        else:
+            setattr(top, i, j)
+
+    return top
+
+
+j = obj_dic(js)
+
+
+print j.s