Преглед изворни кода

[fix] :heavy_check_mark: fix python2 & python3 metaclass

jhao пре 6 година
родитељ
комит
421fd992f1
2 измењених фајлова са 16 додато и 3 уклоњено
  1. 2 2
      db/dbClient.py
  2. 14 1
      util/six.py

+ 2 - 2
db/dbClient.py

@@ -17,13 +17,13 @@ __author__ = 'JHao'
 import os
 import sys
 
-from util.six import urlparse
+from util.six import urlparse, withMetaclass
 from util.singleton import Singleton
 
 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
 
 
-class DbClient(metaclass=Singleton):
+class DbClient(withMetaclass(Singleton)):
     """
     DbClient DB工厂类 提供get/put/update/pop/delete/exists/getAll/clean/getCount/changeTable方法
 

+ 14 - 1
util/six.py

@@ -24,7 +24,6 @@ else:
     def iteritems(d, **kw):
         return d.iteritems(**kw)
 
-
 if PY3:
     from urllib.parse import urlparse
 else:
@@ -39,3 +38,17 @@ if PY3:
     from queue import Empty, Queue
 else:
     from Queue import Empty, Queue
+
+
+def withMetaclass(meta, *bases):
+    """Create a base class with a metaclass."""
+
+    # This requires a bit of explanation: the basic idea is to make a dummy
+    # metaclass for one level of class instantiation that replaces itself with
+    # the actual metaclass.
+    class MetaClass(meta):
+
+        def __new__(cls, name, this_bases, d):
+            return meta(name, bases, d)
+
+    return type.__new__(MetaClass, 'temporary_class', (), {})