clearf
clearf

Reputation: 586

Python Date Comparison throws an unexpected error

I'm using a Flask/SqlAlchemy app with the google calendar API. Essentially, I'm creating a database of events. Here's a stripped-down class snippet:

# Flask. We just need the basic stuff in this common moduel
from flask import Flask

# flask SQLAlchemy
from flask.ext.sqlalchemy import SQLAlchemy

# Sort
from sqlalchemy import desc,asc,and_

# Datetime
from datetime import datetime, date



class FlyingEvent(db.Model):
    # Event Data
    event_id = db.Column(db.Text, primary_key=True)
    updated_datetime = db.Column(db.DateTime)
    start_date = db.Column(db.DateTime)
    end_date = db.Column(db.DateTime)
    status = db.Column(db.Text)
    creator_email = db.Column(db.Text)
    summary = db.Column(db.Text)
    description = db.Column(db.Text)

    # Flying-specific entries
    flying = db.Column(db.Boolean)
    tach_start = db.Column(db.Float)
    tach_end = db.Column(db.Float)


def extract_hours():
        def get_digits(line):
          if re.compare('[\d\.]+', line):
            return line
          else:
            return None
        try: 
          right_now=date.today()
          if self.flying and self.end_date.date() < right_now:
            lines = self.description.split('\n')
            self.tach_start = get_digits(lines[0])
            self.tach_end = get_digits(lines[1])
        except Exception as e:
            raise Exception('%r, Could not parse tach %r %r' % (e, self, 
                right_now))

This raises an exception (which I reraise with the object's repr):

Exception: AttributeError("'module' object has no attribute 'compare'",), Could not parse tach { "event_id": u'go8bl9i615f7e7qljuoun9bqgk', "update_datetime": datetime.datetime(2013, 4, 12, 23, 58, 54, 371000), "start_date": datetime.datetime(2013, 4, 4, 0, 0), "end_date": datetime.datetime(2013, 4, 5, 0, 0), "status": u'confirmed',        "creator_email": u'[email protected]', "summary": u'jordan 10.6 to 10.9', "description": u'1010.7\n1010.9', "flying": True, "tach_start": 1010.7, "tach_end": 1010.9} datetime.date(2013, 4, 14)

It seems to me that the self.end_date is a datetime object (which I'm taking the .date() of), and right_now is a datetime.date().

What am I missing?

Upvotes: 0

Views: 88

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121924

You are using re.compare, which is not a function provided by the re module.

Perhaps you wanted to use re.search() or re.match() instead? Or you could just use float() and get an actual floating point value for the tach_start and tach_end columns:

def get_digits(line):
    try:
        return float(line)
    except ValueError:
        return None

Upvotes: 2

awesoon
awesoon

Reputation: 33661

You got this error since re module has no function compare. I think, you meant re.match:

if re.match('[\d\.]+', line):

Upvotes: 2

Related Questions