From ad3a86ab1e044c881524c614131da7d17b792862 Mon Sep 17 00:00:00 2001 From: invlpg Date: Sun, 20 Jul 2014 17:30:56 +0800 Subject: [PATCH] Refactor: Extract to_re_flags() --- rex.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/rex.py b/rex.py index 16ac149..bad6613 100644 --- a/rex.py +++ b/rex.py @@ -29,16 +29,27 @@ def __unicode__(self): return six.u(self[0]) if self[0] else u'' +RE_FLAGS = { + 'd': re.DEBUG, + 'i': re.IGNORECASE, + 'l': re.LOCALE, + 'm': re.MULTILINE, + 's': re.DOTALL, + 'u': re.UNICODE, + 'x': re.VERBOSE, +} + + +def to_re_flags(s): + """Convert string suffixes to numeric flags of re module.""" + try: + re_flags = [RE_FLAGS[f] for f in s] + except KeyError: + raise ValueError('Bad flags') + return reduce(operator.or_, re_flags, 0) + + class Rex(object): - FLAGS = { - 'd': re.DEBUG, - 'i': re.IGNORECASE, - 'l': re.LOCALE, - 'm': re.MULTILINE, - 's': re.DOTALL, - 'u': re.UNICODE, - 'x': re.VERBOSE, - } def __init__(self, action, pattern, replacement='', flags=0): self.action = action @@ -95,12 +106,8 @@ def rex(expression, text=None, cache=True): replacement = pattern[index + 1:] pattern = pattern[:index] - try: - re_flags = [Rex.FLAGS[f] for f in expression[end + 1:]] - except KeyError: - raise ValueError('Bad flags') - - rex_obj = Rex(action, pattern, replacement, reduce(operator.or_, re_flags, 0)) + flags = to_re_flags(expression[end + 1:]) + rex_obj = Rex(action, pattern, replacement, flags) if cache: REX_CACHE[expression] = rex_obj @@ -108,6 +115,7 @@ def rex(expression, text=None, cache=True): return text == rex_obj else: return rex_obj + rex.group = RexMatch()