[Tutor] clean text

spir denis.spir at free.fr
Tue May 19 11:36:17 CEST 2009


Hello,

This is a follow the post on performance issues.
Using a profiler, I realized that inside error message creation, most of the time was spent in a tool func used to clean up source text output.
The issue is that when the source text holds control chars such as \n, then the error message is hardly readible. MY solution is to replace such chars with their repr():

def _cleanRepr(text):
	''' text with control chars replaced by repr() equivalent '''
	result = ""
	for char in text:
		n = ord(char)
		if (n < 32) or (n > 126 and n < 160):
			char = repr(char)[1:-1]
		result += char
	return result

For any reason, this func is extremely slow. While the rest of error message creation looks very complicated, this seemingly innocent consume > 90% of the time. The issue is that I cannot use repr(text), because repr will replace all non-ASCII characters. I need to replace only control characters.
How else could I do that?

Denis
------
la vita e estrany


More information about the Tutor mailing list