Jonathan Eunice avatar Jonathan Eunice committed 815e05b

added comments on possible extension; moved tests to separate file

Comments (0)

Files changed (2)

             self.cache[key] = instance
             return instance
 
+# Looking to possibility of creating a parametrized metaclass, metaclass
+# factory, or similar mecahnism to be able to configure MementoMetaclass on the
+# fly. This would provide a mechanism for MementoMetaclass users to stipulate
+# what parameters are used as the object-identifying key (currently: all of
+# them, in exactly the call signature). Not ready to pull the trigger on that,
+# however. Metaclasses are a bit tricky, and must be "done right." 
 
-if __name__ == '__main__':
-    
-    class Thing(object):
-        
-        __metaclass__ = MementoMetaclass
-        
-        def __init__(self, name):
-            self.name = name
-        
-
-    t1 = Thing("one")
-    t2 = Thing("one")
-    assert t1 is t2
-    
-    o1 = Thing("lovely")
-    o2 = Thing(name="lovely")
-    assert o1 is not o2   # because the call signature is different
-    
-    class OtherThing(object):
-            
-        __metaclass__ = MementoMetaclass
-        
-        def __init__(self, name):
-            self.name = name
-            self.color = None
-            self.weight = None
-            
-        def set(self, color=None, weight=None):
-            self.color = color or self.color
-            self.weight = weight or self.weight
-            return self
-    
-    ot1 = OtherThing("one").set(color='blue')
-    ot2 = OtherThing("one").set(weight='light')
-    assert ot1 is ot2
-    assert ot1.color == ot2.color == 'blue'
-    assert ot1.weight == ot2.weight == 'light'
-
-    print "working as designed!"
-    
+# Some reading:
+# http://bytes.com/topic/python/answers/40084-parameterized-metaclass-metametaclass
+# http://www.acooke.org/cute/PythonMeta0.html
+# http://www.python.org/dev/peps/pep-3115/
+
+
+from mementos import *
+
+if __name__ == '__main__':
+    
+    class Thing(object):
+        
+        __metaclass__ = MementoMetaclass
+        
+        def __init__(self, name):
+            self.name = name
+        
+
+    t1 = Thing("one")
+    t2 = Thing("one")
+    assert t1 is t2
+    
+    o1 = Thing("lovely")
+    o2 = Thing(name="lovely")
+    assert o1 is not o2   # because the call signature is different
+    
+    class OtherThing(object):
+            
+        __metaclass__ = MementoMetaclass
+        
+        def __init__(self, name):
+            self.name = name
+            self.color = None
+            self.weight = None
+            
+        def set(self, color=None, weight=None):
+            self.color = color or self.color
+            self.weight = weight or self.weight
+            return self
+    
+    ot1 = OtherThing("one").set(color='blue')
+    ot2 = OtherThing("one").set(weight='light')
+    assert ot1 is ot2
+    assert ot1.color == ot2.color == 'blue'
+    assert ot1.weight == ot2.weight == 'light'
+
+    print "working as designed!"
+    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.