Commits

Andrew Godwin committed 11e7488

Memoise the FakeORM construction (#213).

  • Participants
  • Parent commits 255218e

Comments (0)

Files changed (2)

File south/orm.py

             return self.data[key.lower()]
 
 
-class FakeORM(object):
+# Stores already-created ORMs.
+_orm_cache = {}
+
+def FakeORM(*args):
+    """
+    Creates a Fake Django ORM.
+    This is actually a memoised constructor; the real class is _FakeORM.
+    """
+    if not args in _orm_cache:
+        _orm_cache[args] = _FakeORM(*args)  
+    return _orm_cache[args] 
+
+
+class _FakeORM(object):
     
     """
     Simulates the Django ORM at some point in time,

File south/utils.py

 """
 
 
-def ask_for_it_by_name(name):
+def _ask_for_it_by_name(name):
     "Returns an object referenced by absolute path."
     bits = name.split(".")
 
     return getattr(module, bits[-1])
 
 
+def ask_for_it_by_name(name): 
+    "Returns an object referenced by absolute path. (Memoised outer wrapper)"
+    if name not in ask_for_it_by_name.cache: 
+        ask_for_it_by_name.cache[name] = _ask_for_it_by_name(name) 
+    return ask_for_it_by_name.cache[name] 
+ask_for_it_by_name.cache = {} 
+
+
 def get_attribute(item, attribute):
     """
     Like getattr, but recursive (i.e. you can ask for 'foo.bar.yay'.)