Exemplo n.º 1
0
def typefilter(elt, fields):
    if isinstance(elt, StreamMeta):
        return elt
    else:
        d = {}
        f = fields if fields else elt._fields
        for t in f:
            v = getattr(elt, t)
            if not isinstance(v, basestring):
                continue
            g = pattern.match(v)
            if g:
                if g.group('int'):
                    d[t] = int(v)
                elif g.group('float'):
                    d[t] = float(v)
            else:
                try:
                    d[t] = parse_datetime(v)
                except ValueError:
                    try:
                        d[t] = parse_date(v)
                    except ValueError:
                        pass
        if len(d) > 0:
            return elt._replace(**d)
        else:
            return elt
Exemplo n.º 2
0
def typefilter(elt, fields):
    if isinstance(elt, StreamMeta):
        return elt
    else:
        d = {}
        f = fields if fields else elt._fields
        for t in f:
            v = getattr(elt, t)
            if not isinstance(v, basestring):
                continue
            g = pattern.match(v)
            if g: 
                if g.group('int'):
                    d[t] = int(v)
                elif g.group('float'):
                    d[t] = float(v)
            else:
                try: 
                    d[t] = parse_datetime(v)
                except ValueError: 
                    try: 
                        d[t] = parse_date(v)
                    except ValueError:
                        pass
        if len(d) > 0:
            return elt._replace(**d)
        else:
            return elt
Exemplo n.º 3
0
def convert_to_datetime(d, referent_timezone):
    if isinstance(d, basestring):
        try:
            d = parse_datetime(d)
        except ValueError:
            d = parse_date(d)
    if isinstance(d, datetime.datetime):
        return d
    elif isinstance(d, datetime.date):
        # Convert to a datetime, taking 'referent_timezone in consideration'
        # e.g. if 'CET' is the timezone, the day 2012-01-15 interpreted as '2012-01-15 00:00 CET' hence '2012-01-14 23:00 UTC'
        tz = timezone(referent_timezone)
        ldt = tz.localize(datetime.datetime(d.year, d.month, d.day, 0, 0, 0))
        return ldt.astimezone(utc)
Exemplo n.º 4
0
def convert_to_datetime(d, referent_timezone):
    if isinstance(d, basestring):
        try:
            d = parse_datetime(d)
        except ValueError:
            d = parse_date(d)
    if isinstance(d, datetime.datetime):
        return d
    elif isinstance(d, datetime.date):
        # Convert to a datetime, taking 'referent_timezone in consideration'
        # e.g. if 'CET' is the timezone, the day 2012-01-15 interpreted as '2012-01-15 00:00 CET' hence '2012-01-14 23:00 UTC'
        tz = timezone(referent_timezone)
        ldt = tz.localize(datetime.datetime(d.year, d.month, d.day, 0, 0, 0))
        return ldt.astimezone(utc)
Exemplo n.º 5
0
 def filter(self, elt):
     if isinstance(elt, MetaInfo):
         return elt
     else:
         self.d.clear()
         for t in elt._fields:
             v = getattr(elt, t)
             if not isinstance(v, str):
                 continue
             g = self.pattern.match(v)
             if g:
                 if g.group("int"):
                     self.d[t] = int(v)
                 elif g.group("float"):
                     self.d[t] = float(v)
                 elif g.group("date"):
                     self.d[t] = parse_date(v)
                 elif g.group("datetime"):
                     self.d[t] = parse_datetime(v)
         if len(self.d) > 0:
             return elt._replace(**self.d)
         else:
             return elt
Exemplo n.º 6
0
 def filter(self, elt):
     if isinstance(elt, MetaInfo):
         return elt
     else:
         self.d.clear()
         for t in elt._fields:
             v = getattr(elt, t)
             if not isinstance(v, str):
                 continue
             g = self.pattern.match(v)
             if g:
                 if g.group('int'):
                     self.d[t] = int(v)
                 elif g.group('float'):
                     self.d[t] = float(v)
                 elif g.group('date'):
                     self.d[t] = parse_date(v)
                 elif g.group('datetime'):
                     self.d[t] = parse_datetime(v)
         if len(self.d) > 0:
             return elt._replace(**self.d)
         else:
             return elt