From efc4a4a3c6bcf57fee33af933b94beb7bbaac49e Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Fri, 1 Apr 2016 08:54:06 +0200 Subject: [PATCH] Reduce the call count to eval. This reduce by 3 the time of fast(q|a) header processing --- python/obitools3/parsers/header.pyx | 37 ++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/python/obitools3/parsers/header.pyx b/python/obitools3/parsers/header.pyx index e0099e6..0b87dab 100644 --- a/python/obitools3/parsers/header.pyx +++ b/python/obitools3/parsers/header.pyx @@ -8,13 +8,38 @@ Created on 25 mars 2016 import re -__ret__ = re.compile('''(([^ ]+)=('[^']*'|"[^"]*"|[^;]+); *)+?''') - +__ret__ = re.compile('''(([^ ]+)=('[^']*'|"[^"]*"|[^;]+); *)+?''') +__re_int__ = re.compile("^[+-]?[0-9]+$") +__re_float__ = re.compile("^[+-]?[0-9]+(\.[0-9]*)?([eE][+-]?[0-9]+)?$") +__re_str__ = re.compile("""^"[^"]*"|'[^']*'$""") +__re_dict__ = re.compile("""^\{\ * + ( + ("[^"]*"|'[^']*') + \ *:\ * + ([^,}]+| + "[^"]*"| + '[^']*' + ) + )? + (\ *,\ * + ("[^"]*"|'[^']*') + \ *:\ * + ([^,}]+| + "[^"]*"| + '[^']*' + ) + )*\ *\}$""", re.VERBOSE) cdef object __etag__(str x): - try: - v = eval(x,{},{}) - except: - v = x + if __re_int__.match(x): + v=int(x) + elif __re_float__.match(x): + v=float(x) + elif __re_str__.match(x): + v=x[1:-1] + elif __re_dict__.match(x): + v=eval(x) + else: + v=x return v cpdef tuple parseHeader(str header):