From cd572b9b2de73eea8e83b028c7b4c3906ab60ab2 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 24 Apr 2014 15:56:40 +0200 Subject: Optimize prolog.util.stringify (~15% faster) --- prolog/util.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'prolog/util.py') diff --git a/prolog/util.py b/prolog/util.py index fc243f2..eb026fe 100644 --- a/prolog/util.py +++ b/prolog/util.py @@ -19,24 +19,14 @@ operators = set([ 'PLUS', 'MINUS', 'STAR', 'DIV', 'IDIV', 'MOD', 'POW', 'SEMI' ]) -def stringify(tokens, indent=''): - s = indent - for t in tokens: - if t[0] in operators: - s += ' ' - - if t[0] == 'FROM': - s += ':-\n ' + indent - elif t[0] == 'PERIOD': - s += '.\n' + indent - elif t[0] == 'COMMA': - s += ', ' +def stringify(tokens): + def token_str(t): + if t[0] in ('PERIOD', 'COMMA'): + return t[1] + ' ' elif t[0] in operators: - s += t[1] + ' ' - else: - s += t[1] - - return s.strip().replace('\n', ' ').replace('\t', ' ') + return ' ' + t[1] + ' ' + return t[1] + return ''.join(map(token_str, tokens)) # return a list of lines in 'code', and a list of rule indexes def decompose(code): -- cgit v1.2.1