Moved casting function in rstring

rstring now has an autocast() function that returns the automatic casting
for that value.
The casting is cached so is really performed only once, at most, during
the lifetime of the object.
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2015-06-01 09:12:22 +02:00
parent e4158a3685
commit de405480b5
2 changed files with 25 additions and 30 deletions

View File

@ -100,18 +100,6 @@ class relation (object):
','.join(self.header.attributes) , ','.join(other.header.attributes) ','.join(self.header.attributes) , ','.join(other.header.attributes)
)) ))
def _autocast(self, string):
'''Depending on the regexp matched by the string,
it will perform automatic casting'''
if len(string) > 0 and string.isInt():
return int(string)
elif len(string) > 0 and string.isFloat():
return float(string)
elif len(string) > 0 and string.isDate():
return rdate(string)
else:
return string
def selection(self, expr): def selection(self, expr):
'''Selection, expr must be a valid boolean expression, can contain field names, '''Selection, expr must be a valid boolean expression, can contain field names,
constant, math operations and boolean ones.''' constant, math operations and boolean ones.'''
@ -121,7 +109,7 @@ class relation (object):
for i in self.content: for i in self.content:
# Fills the attributes dictionary with the values of the tuple # Fills the attributes dictionary with the values of the tuple
for j in range(len(self.header.attributes)): for j in range(len(self.header.attributes)):
attributes[self.header.attributes[j]] = self._autocast(i[j]) attributes[self.header.attributes[j]] = i[j].autocast()
try: try:
if eval(expr, attributes): if eval(expr, attributes):
@ -448,7 +436,7 @@ class relation (object):
# new_content=[] #New content of the relation # new_content=[] #New content of the relation
for i in self.content: for i in self.content:
for j in range(len(self.header.attributes)): for j in range(len(self.header.attributes)):
attributes[self.header.attributes[j]] = self._autocast(i[j]) attributes[self.header.attributes[j]] = i[j].autocast()
if eval(expr, attributes): # If expr is true, changing the tuple if eval(expr, attributes): # If expr is true, changing the tuple
affected += 1 affected += 1

View File

@ -28,6 +28,25 @@ import re
class rstring (str): class rstring (str):
'''String subclass with some custom methods''' '''String subclass with some custom methods'''
def autocast(self):
'''
Returns the automatic cast for this
value.
'''
try:
return self._autocast
except:
pass
self._autocast = self
if len(self) > 0:
if self.isInt():
self._autocast = int(self)
elif self.isFloat():
self._autocast = float(self)
elif self.isDate():
self._autocast = rdate(self)
return self._autocast
def isInt(self): def isInt(self):
'''Returns true if the string represents an int number '''Returns true if the string represents an int number
@ -35,16 +54,10 @@ class rstring (str):
the following regexp: the following regexp:
r'^[\+\-]{0,1}[0-9]+$' r'^[\+\-]{0,1}[0-9]+$'
''' '''
try:
return self._isint
except:
pass
if re.match(r'^[\+\-]{0,1}[0-9]+$', self) == None: if re.match(r'^[\+\-]{0,1}[0-9]+$', self) == None:
self._isint = False return False
else: else:
self._isint = True return True
return self._isint
def isFloat(self): def isFloat(self):
'''Returns true if the string represents a float number '''Returns true if the string represents a float number
@ -52,16 +65,10 @@ class rstring (str):
the following regexp: the following regexp:
r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$' r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$'
''' '''
try:
return self._isfloat
except:
pass
if re.match(r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$', self) == None: if re.match(r'^[\+\-]{0,1}[0-9]+(\.([0-9])+)?$', self) == None:
self._isfloat = False return False
else: else:
self._isfloat = True return True
return self._isfloat
def isDate(self): def isDate(self):
'''Returns true if the string represents a date, '''Returns true if the string represents a date,