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.
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2015-09-09 15:17:17 +02:00
parent d8db1858ae
commit 9330090362

View File

@ -23,13 +23,17 @@
import datetime import datetime
import re 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): class Rstring (str):
'''String subclass with some custom methods''' '''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): def autocast(self):
''' '''
Returns the automatic cast for this Returns the automatic cast for this
@ -56,7 +60,7 @@ class Rstring (str):
the following regexp: the following regexp:
r'^[\+\-]{0,1}[0-9]+$' 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 return False
else: else:
return True return True
@ -67,7 +71,7 @@ class Rstring (str):
the following regexp: the following regexp:
r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$' 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 return False
else: else:
return True return True
@ -83,9 +87,8 @@ class Rstring (str):
except: except:
pass pass
r = re.match( r = Rstring.date_regexp.match(self)
r'^([0-9]{1,4})(\\|-|/)([0-9]{1,2})(\\|-|/)([0-9]{1,2})$', self) if r is None:
if r == None:
self._isdate = False self._isdate = False
self._date = None self._date = None
return False return False