[Checkins] SVN: Sandbox/malthe/chameleon.core/ Refactored string coercion code.

Malthe Borch mborch at gmail.com
Mon Oct 27 07:18:14 EDT 2008


Log message for revision 92611:
  Refactored string coercion code.

Changed:
  U   Sandbox/malthe/chameleon.core/CHANGES.txt
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/clauses.py
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/generation.py

-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt	2008-10-27 08:42:44 UTC (rev 92610)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt	2008-10-27 11:18:12 UTC (rev 92611)
@@ -4,6 +4,8 @@
 head
 ~~~~
 
+- Refactored string coercion. [malthe]
+
 1.0b3 (released 13/10/2008)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/clauses.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/clauses.py	2008-10-27 08:42:44 UTC (rev 92610)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/clauses.py	2008-10-27 11:18:12 UTC (rev 92611)
@@ -444,27 +444,56 @@
     >>> _out.getvalue()
     '<div alt="Hello World!">Hello Universe!</div>'
 
-    Self-closing tag:
-
-    >>> _out, _write, stream = testing.setup_stream()
-    >>> tag = Tag('br', {}, True)
+    >>> _out, _write, stream = testing.setup_stream('utf-8')
+    >>> tag = Tag('div', dict(alt=testing.pyexp(repr('Hello World!'))))
     >>> tag.begin(stream)
+    >>> stream.out('Hello Universe!')
     >>> tag.end(stream)
     >>> exec stream.getvalue()
     >>> _out.getvalue()
-    '<br />'
+    '<div alt="Hello World!">Hello Universe!</div>'
 
-    Unicode:
+    Verify that unicode data is handled correctly.
 
     >>> _out, _write, stream = testing.setup_stream()
-    >>> tag = Tag('div', dict(alt=testing.pyexp(repr('La Pe\xc3\xb1a'))))
+    >>> tag = Tag('div', dict(
+    ...     alt=testing.pyexp(repr(unicode('La Pe\xc3\xb1a', 'utf-8')))))
     >>> tag.begin(stream)
     >>> stream.out('Hello Universe!')
     >>> tag.end(stream)
     >>> exec stream.getvalue()
-    >>> _out.getvalue() == '<div alt="La Pe\xc3\xb1a">Hello Universe!</div>'
+    >>> 'Hello' in _out.getvalue()
     True
 
+    Dictionary attributes:
+
+    >>> _out, _write, stream = testing.setup_stream()
+    >>> tag = Tag('div', expression=testing.pyexp(repr({'alt': 'Hello World!'})))
+    >>> tag.begin(stream)
+    >>> stream.out('Hello Universe!')
+    >>> tag.end(stream)
+    >>> exec stream.getvalue()
+    >>> _out.getvalue()
+    '<div alt="Hello World!">Hello Universe!</div>'
+    
+    >>> _out, _write, stream = testing.setup_stream('utf-8')
+    >>> tag = Tag('div', expression=testing.pyexp(repr({'alt': 'Hello World!'})))
+    >>> tag.begin(stream)
+    >>> stream.out('Hello Universe!')
+    >>> tag.end(stream)
+    >>> exec stream.getvalue()
+    >>> _out.getvalue()
+    '<div alt="Hello World!">Hello Universe!</div>'
+
+    Self-closing tag:
+
+    >>> _out, _write, stream = testing.setup_stream()
+    >>> tag = Tag('br', {}, True)
+    >>> tag.begin(stream)
+    >>> tag.end(stream)
+    >>> exec stream.getvalue()
+    >>> _out.getvalue()
+    '<br />'
     """
 
     def __init__(self, tag, attributes=None,
@@ -512,25 +541,10 @@
             # whether we're dealing with unicode strings or not,
             # before writing out the attribute
             if stream.encoding is not None:
-                # attribute name
-                stream.write("if isinstance(%s, unicode):" % temp)
-                stream.indent()
-                stream.write("%s = %s.encode('%s')" % (temp, temp, stream.encoding))
-                stream.outdent()
-
-                # attribute expression
-                stream.write("if isinstance(%s, unicode):" % temp2)
-                stream.indent()
-                stream.write("%s = %s.encode('%s')" % (temp2, temp2, stream.encoding))
-                stream.outdent()
-                stream.write("elif not isinstance(%s, str):" % temp2)
+                stream.convert_to_string(temp)
+                stream.coerce_to_string(temp2)
             else:
-                stream.write("if not isinstance(%s, (str, unicode)):" % temp2)
-
-            # make sure this is a string
-            stream.indent()
-            stream.write("%s = str(%s)" % (temp2, temp2))
-            stream.outdent()
+                stream.coerce_to_unicode(temp2)
                 
             # escape expression
             stream.escape(temp2)
@@ -554,19 +568,10 @@
             # whether we're dealing with unicode strings or not,
             # before writing out the attribute
             if stream.encoding is not None:
-                stream.write("if isinstance(%s, unicode):" % temp)
-                stream.indent()
-                stream.write("%s = %s.encode('%s')" % (temp, temp, stream.encoding))
-                stream.outdent()
-                stream.write("elif not isinstance(%s, str):" % temp)
+                stream.coerce_to_string(temp)
             else:
-                stream.write("if not isinstance(%s, (str, unicode)):" % temp)
+                stream.coerce_to_unicode(temp)
                 
-            # make sure this is a string
-            stream.indent()
-            stream.write("%s = str(%s)" % (temp, temp))
-            stream.outdent()
-
             # escape expression
             stream.escape(temp)
 
@@ -737,6 +742,14 @@
     >>> _out.getvalue()
     'New York'
 
+    >>> _out, _write, stream = testing.setup_stream('utf-8')
+    >>> write = Write(testing.pyexp("'New York'"))
+    >>> write.begin(stream)
+    >>> write.end(stream)
+    >>> exec stream.getvalue()
+    >>> _out.getvalue()
+    'New York'
+
     Try-except parts:
 
     >>> _out, _write, stream = testing.setup_stream()
@@ -799,19 +812,10 @@
         stream.indent()
 
         if stream.encoding is not None:
-            write("if isinstance(%(tmp)s, unicode):")
-            stream.indent()
-            write("%%(tmp)s = %%(tmp)s.encode('%s')" % stream.encoding)
-            stream.outdent()
-            write("elif not isinstance(%(tmp)s, str):")
+            stream.coerce_to_string(stream.symbols.tmp)
         else:
-            write("if not isinstance(%(tmp)s, (str, unicode)):")
-            
-        # make sure this is a string
-        stream.indent()
-        write("%(tmp)s = str(%(tmp)s)")
-        stream.outdent()
-
+            stream.coerce_to_unicode(stream.symbols.tmp)
+                        
         if self.structure:
             write("%(write)s(%(tmp)s)")
         else:

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/generation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/generation.py	2008-10-27 08:42:44 UTC (rev 92610)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/generation.py	2008-10-27 11:18:12 UTC (rev 92611)
@@ -156,7 +156,32 @@
         self.indent()
         self.write("%s = %s.replace('\"', '&quot;')" % (variable, variable))
         self.outdent()
+
+    def convert_to_string(self, variable):
+        self.write("if isinstance(%s, unicode):" % variable)
+        self.indent()
+        self.write("%s = %s.encode('%s')" % (variable, variable, self.encoding))
+        self.outdent()
+
+    def coerce_to_string(self, variable):
+        self.write("if isinstance(%s, unicode):" % variable)
+        self.indent()
+        self.write("%s = %s.encode('%s')" % (variable, variable, self.encoding))
+        self.outdent()
+        self.write("elif not isinstance(%s, str):" % variable)
+        self.indent()
+        self.write("%s = str(%s)" % (variable, variable))
+        self.outdent()
+
+    def coerce_to_unicode(self, variable):
+        """Coerces variable to unicode (actually ``str``, because it's
+        much faster)."""
         
+        self.write("if not isinstance(%s, (str, unicode)):" % variable)
+        self.indent()
+        self.write("%s = str(%s)" % (variable, variable))
+        self.outdent()
+        
     def begin(self, clauses):
         if isinstance(clauses, (list, tuple)):
             for clause in clauses:



More information about the Checkins mailing list