Commits

Álvaro Justen - Turicas  committed ff23526

code_to_parse now on timetest.py

  • Participants
  • Parent commits 896ee9a

Comments (0)

Files changed (2)

File new_template.py

 
 
 def should_unindent_before(parsed_line):
-    if parsed_line.strip():
-        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_before
-    else:
-        return ''
+    return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_before
 
 
 def should_unindent_after(parsed_line):
-    if parsed_line.strip():
-        return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_after
-    else:
-        return ''
+    return parsed_line.split()[0].replace(':', '').strip() in unindentation_words_after
 
 
 def parse_python_code(python_code, indentation_level=0):
                 opened += 1
             elif character in close_something:
                 opened -= 1
+                
+            if character == '\n' and last_character != '\\' and not opened:
+                parsed_line = parse_line(''.join(line[:-1])).strip()
+                if parsed_line:
+                    if should_unindent_before(parsed_line):
+                        indentation_level -= 1
+                    if parsed_line and parsed_line != 'pass':
+                        response.append(indentation_level * indentation + parsed_line)
+                    if should_unindent_after(parsed_line):
+                        indentation_level -= 1
+                line = []
+                if last_character == ':':
+                    indentation_level += 1
+
         if character in quotes and (not last_quote or character == last_quote):
             last_quote = character
             in_string = not in_string
             if not in_string:
                 last_quote = ''
-        if character == '\n' and not in_string and last_character != '\\' and not opened:
-            parsed_line = parse_line(''.join(line[:-1])).strip()
-            if parsed_line:
-                if should_unindent_before(parsed_line):
-                    indentation_level -= 1
-                if parsed_line and parsed_line != 'pass':
-                    response.append(indentation_level * indentation + parsed_line)
-                if should_unindent_after(parsed_line):
-                    indentation_level -= 1
-            line = []
-            if last_character == ':':
-                indentation_level += 1
         last_character = character
 
     if line:
             response.append(indentation_level * indentation + \
                             string_on_html % parse_string(''.join(line)))
     return '\n'.join(response)
-    
-
-code_to_parse = '''{{if something:
-some_code
-other_code
-elif other_condition:
-elif_code
-other_elif_code
-else:
-code_on_else
-other_code_on_else
-pass
-code_outside
-for a in b:
-code_on_for
-other_code
-pass
-some_code_outside
-for c in d:
-code_on_second_for
-other_code_on_second_for
-else:
-code_on_else
-other_code_on_else
-pass
-other_code_outside
-try:
-something
-code_to_try
-except:
-code_on_the_exception
-blablabla
-pass
-code_outside
-try:
-other_try_code
-more_one_line
-except SomeException, e:
-lets_solve_this_exception
-more_code
-except:
-code_to_general_exception
-goes_here
-pass
-more_code_outside_all_blocks}}'''
 import timeit
 
+code_to_parse = '''{{if something:
+some_code
+other_code
+elif other_condition:
+elif_code
+other_elif_code
+else:
+code_on_else
+other_code_on_else
+pass
+code_outside
+for a in b:
+code_on_for
+other_code
+pass
+some_code_outside
+for c in d:
+code_on_second_for
+other_code_on_second_for
+else:
+code_on_else
+other_code_on_else
+pass
+other_code_outside
+try:
+something
+code_to_try
+except:
+code_on_the_exception
+blablabla
+pass
+code_outside
+try:
+other_try_code
+more_one_line
+except SomeException, e:
+lets_solve_this_exception
+more_code
+except:
+code_to_general_exception
+goes_here
+pass
+more_code_outside_all_blocks}}'''
+
 t1 = timeit.Timer('parse_template(code_to_parse)',
-                 'from new_template import parse_template, code_to_parse').timeit(number=1000)
+                 '''from new_template import parse_template
+from __main__ import code_to_parse''').timeit(number=1000)
 print t1
 
 t2 = timeit.Timer('parse(code_to_parse)',
-                 'from template import parse, code_to_parse').timeit(number=1000)
+                 '''from template import parse
+from __main__ import code_to_parse''').timeit(number=1000)
 print t2
 
 print t1 / t2