diff --git a/relational/relation.py b/relational/relation.py index 82ce1e9..b928113 100644 --- a/relational/relation.py +++ b/relational/relation.py @@ -100,18 +100,6 @@ class relation (object): ','.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): '''Selection, expr must be a valid boolean expression, can contain field names, constant, math operations and boolean ones.''' @@ -121,7 +109,7 @@ class relation (object): for i in self.content: # Fills the attributes dictionary with the values of the tuple 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: if eval(expr, attributes): @@ -448,7 +436,7 @@ class relation (object): # new_content=[] #New content of the relation for i in self.content: 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 affected += 1 diff --git a/relational/rtypes.py b/relational/rtypes.py index 4698b6b..d0b9bed 100644 --- a/relational/rtypes.py +++ b/relational/rtypes.py @@ -28,6 +28,25 @@ import re class rstring (str): '''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): '''Returns true if the string represents an int number @@ -35,16 +54,10 @@ class rstring (str): the following regexp: r'^[\+\-]{0,1}[0-9]+$' ''' - try: - return self._isint - except: - pass - if re.match(r'^[\+\-]{0,1}[0-9]+$', self) == None: - self._isint = False + return False else: - self._isint = True - return self._isint + return True def isFloat(self): '''Returns true if the string represents a float number @@ -52,16 +65,10 @@ class rstring (str): the following regexp: 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: - self._isfloat = False + return False else: - self._isfloat = True - return self._isfloat + return True def isDate(self): '''Returns true if the string represents a date,