Added support for dates
git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@35 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
parent
ee37a74088
commit
15707f89c5
@ -27,3 +27,4 @@
|
|||||||
|
|
||||||
0.5
|
0.5
|
||||||
- Added support for float numbers
|
- Added support for float numbers
|
||||||
|
- Added support for dates
|
@ -16,7 +16,7 @@
|
|||||||
#
|
#
|
||||||
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
||||||
|
|
||||||
import rtypes
|
from rtypes import *
|
||||||
|
|
||||||
class relation (object):
|
class relation (object):
|
||||||
'''This objects defines a relation (as a group of consistent tuples) and operations
|
'''This objects defines a relation (as a group of consistent tuples) and operations
|
||||||
@ -78,8 +78,10 @@ class relation (object):
|
|||||||
for j in range(len(self.header.attributes)):
|
for j in range(len(self.header.attributes)):
|
||||||
if i[j].isdigit():
|
if i[j].isdigit():
|
||||||
attributes[self.header.attributes[j]]=int(i[j])
|
attributes[self.header.attributes[j]]=int(i[j])
|
||||||
elif rtypes.rstring(i[j]).isFloat():
|
elif rstring(i[j]).isFloat():
|
||||||
attributes[self.header.attributes[j]]=float(i[j])
|
attributes[self.header.attributes[j]]=float(i[j])
|
||||||
|
elif isDate(i[j]):
|
||||||
|
attributes[self.header.attributes[j]]=rdate(i[j])
|
||||||
else:
|
else:
|
||||||
attributes[self.header.attributes[j]]=i[j]
|
attributes[self.header.attributes[j]]=i[j]
|
||||||
|
|
||||||
|
73
rtypes.py
73
rtypes.py
@ -17,12 +17,83 @@
|
|||||||
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
||||||
|
|
||||||
'''Custom types for relational algebra'''
|
'''Custom types for relational algebra'''
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
class rstring (str):
|
class rstring (str):
|
||||||
'''String subclass with some custom methods'''
|
'''String subclass with some custom methods'''
|
||||||
def isFloat(self):
|
def isFloat(self):
|
||||||
'''True if the string is a float number, false otherwise'''
|
'''True if the string is a float number, false otherwise'''
|
||||||
lst=['0','1','2','3','4','5','6','7','8','9','.']
|
lst=('0','1','2','3','4','5','6','7','8','9','.')
|
||||||
for i in self:
|
for i in self:
|
||||||
if i not in lst:
|
if i not in lst:
|
||||||
return False;
|
return False;
|
||||||
return True;
|
return True;
|
||||||
|
|
||||||
|
class rdate (object):
|
||||||
|
'''Represents a date'''
|
||||||
|
def __init__(self,date):
|
||||||
|
sep=('-','/','\\')
|
||||||
|
splitter=None
|
||||||
|
for i in sep:
|
||||||
|
if i in date:
|
||||||
|
splitter=i
|
||||||
|
break;
|
||||||
|
elems=date.split(splitter)
|
||||||
|
|
||||||
|
year=int(elems[0])
|
||||||
|
month=int(elems[1])
|
||||||
|
day=int(elems[2])
|
||||||
|
|
||||||
|
self.intdate=datetime.date(year,month,day)
|
||||||
|
self.day= self.intdate.day
|
||||||
|
self.month=self.intdate.month
|
||||||
|
self.weekday=self.intdate.weekday()
|
||||||
|
self.year=self.intdate.year
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.intdate.__str__()
|
||||||
|
def __add__(self,days):
|
||||||
|
res=self.intdate+datetime.timedelta(days)
|
||||||
|
return rdate(res.__str__())
|
||||||
|
|
||||||
|
def __eq__(self,other):
|
||||||
|
return self.intdate==other.intdate
|
||||||
|
def __ge__(self,other):
|
||||||
|
return self.intdate>=other.intdate
|
||||||
|
def __gt__ (self,other):
|
||||||
|
return self.intdate>other.intdate
|
||||||
|
def __le__ (self,other):
|
||||||
|
return self.intdate<=other.intdate
|
||||||
|
def __lt__ (self,other):
|
||||||
|
return self.intdate<other.intdate
|
||||||
|
def __ne__(self,other):
|
||||||
|
return self.intdate!=other.intdate
|
||||||
|
def __sub__ (self,other):
|
||||||
|
return (self.intdate-other.intdate).days
|
||||||
|
def isDate(date):
|
||||||
|
sep=('-','/','\\')
|
||||||
|
splitter=None
|
||||||
|
for i in sep:
|
||||||
|
if i in date:
|
||||||
|
splitter=i
|
||||||
|
break;
|
||||||
|
elems=date.split(splitter)
|
||||||
|
if len(elems)!=3:
|
||||||
|
return False #Wrong number of elements
|
||||||
|
year=elems[0]
|
||||||
|
month=elems[1]
|
||||||
|
day=elems[2]
|
||||||
|
if not (year.isdigit() and month.isdigit() and day.isdigit()):
|
||||||
|
return False
|
||||||
|
year=int(year)
|
||||||
|
month=int(month)
|
||||||
|
day=int(day)
|
||||||
|
|
||||||
|
if year<datetime.MINYEAR or year>datetime.MAXYEAR:
|
||||||
|
return False
|
||||||
|
if month<1 or month>12:
|
||||||
|
return False
|
||||||
|
if day<1 or day >31:
|
||||||
|
return False
|
||||||
|
return True
|
6
samples/dates.tlb
Normal file
6
samples/dates.tlb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
date
|
||||||
|
2008-12-12
|
||||||
|
2007-08-12
|
||||||
|
1985-05-09
|
||||||
|
1988-4-21
|
||||||
|
1992-7-27
|
@ -1,5 +1,5 @@
|
|||||||
id name chief age
|
id name chief age
|
||||||
0 jack 0 22.1
|
0 jack 0 22
|
||||||
1 carl 0 20
|
1 carl 0 20
|
||||||
2 john 1 30
|
2 john 1 30
|
||||||
3 dean 1 33
|
3 dean 1 33
|
||||||
|
Loading…
Reference in New Issue
Block a user