From 93300903626f6f20ede62058d7e735211d9ad8fd Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Wed, 9 Sep 2015 15:17:17 +0200 Subject: [PATCH] Switch to compiled regular expressions This introduces several advantages: No longer rely on the cache to be able to contain as many as needed. It works within relational but it is not assured to be so if it is used within other projects. Using compile flags allows slightly simpler expressions. --- relational/rtypes.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/relational/rtypes.py b/relational/rtypes.py index ad804cb..90883bb 100644 --- a/relational/rtypes.py +++ b/relational/rtypes.py @@ -23,13 +23,17 @@ import datetime import re -RELATION_NAME_REGEXP = r'^[_a-zA-Z]+[_a-zA-Z0-9]*$' +RELATION_NAME_REGEXP = re.compile(r'^[_a-z][_a-z0-9]*$', re.IGNORECASE) class Rstring (str): '''String subclass with some custom methods''' + int_regexp = re.compile(r'^[\+\-]{0,1}[0-9]+$') + float_regexp = re.compile(r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$') + date_regexp = re.compile(r'^([0-9]{1,4})(\\|-|/)([0-9]{1,2})(\\|-|/)([0-9]{1,2})$') + def autocast(self): ''' Returns the automatic cast for this @@ -56,7 +60,7 @@ class Rstring (str): the following regexp: r'^[\+\-]{0,1}[0-9]+$' ''' - if re.match(r'^[\+\-]{0,1}[0-9]+$', self) == None: + if Rstring.int_regexp.match(self) is None: return False else: return True @@ -67,7 +71,7 @@ class Rstring (str): the following regexp: r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$' ''' - if re.match(r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$', self) == None: + if Rstring.float_regexp.match(self) is None: return False else: return True @@ -83,9 +87,8 @@ class Rstring (str): except: pass - r = re.match( - r'^([0-9]{1,4})(\\|-|/)([0-9]{1,2})(\\|-|/)([0-9]{1,2})$', self) - if r == None: + r = Rstring.date_regexp.match(self) + if r is None: self._isdate = False self._date = None return False