Commits

Sven Hendriks committed 77255c9

Optimizations to handling of self.reference in Cell; Should speed up the creation of large documents

  • Participants
  • Parent commits ccc4bd8

Comments (0)

Files changed (2)

xlsxcessive/cache.py

+class CacheDecorator(object):
+    def __init__(self): 
+        self.cache = {} 
+        self.func = None
+
+    def cached_func(self, *args): 
+        if args not in self.cache: 
+            self.cache[args] = self.func(*args) 
+        return self.cache[args]
+
+    def __call__(self, func): 
+        self.func = func 
+        return self.cached_func
+

xlsxcessive/worksheet.py

 
 from xml.sax.saxutils import escape
 from xlsxcessive import markup
+from xlsxcessive.cache import CacheDecorator
 
 class UnsupportedDateBase(Exception): pass
 
+
+@CacheDecorator()
+def _coords_to_a1_helper(coords):
+    # the following closure was adapted from
+    # http://stackoverflow.com/questions/22708/how-do-i-find-the-excel-column-name-that-corresponds-to-a-given-integer
+    def num_to_a(n):
+        n -= 1
+        if (n >= 0 and n < 26):
+            return chr(65 + n)
+        else:
+            return num_to_a(n / 26) + num_to_a(n % 26 + 1)
+    a1_col = num_to_a(coords[1] + 1)
+    return "%s%d" % (a1_col, coords[0] + 1)
+
+
 class Worksheet(object):
     """An OOXML Worksheet."""
 
             self._set_value(value)
         self.format = format
         self.merge_range = None
+        if not self._reference and self._coords:
+            self._reference = self._coords_to_a1()
 
     @classmethod
     def from_reference(cls, ref):
 
     @property
     def reference(self):
-        if self._reference:
-            return self._reference
-        if self._coords:
-            return self._coords_to_a1()
+        return self._reference
 
     class coords(object):
         def __get__(self, instance, other):
     coords = coords()
 
     def _coords_to_a1(self):
-        # the following closure was adapted from
-        # http://stackoverflow.com/questions/22708/how-do-i-find-the-excel-column-name-that-corresponds-to-a-given-integer
-        def num_to_a(n):
-            n -= 1
-            if (n >= 0 and n < 26):
-                return chr(65 + n)
-            else:
-                return num_to_a(n / 26) + num_to_a(n % 26 + 1)
-        a1_col = num_to_a(self._coords[1] + 1)
-        return "%s%d" % (a1_col, self._coords[0] + 1)
+        return _coords_to_a1_helper(self._coords)
 
     def _a1_to_coords(self):
         def _p():