سؤال

I am trying to write a function in python that deletes entries in my datastore that are more than five minutes old. I'm making a kitten picture database for a class, so my code looks something like this:

class KittenImg(db.Model):
    """Models a Gallery entry with kitten_name, image, and date."""
    kitten_name = db.StringProperty(multiline=True)
    image = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)

A user uploads a KittenImg and it loads into the datastore and returns just fine, but I don't think I understand really what format a kitten.date value would return and how I can compare it to datetime.now() using Python. I have tried a few different options in the python datetime module documentation, but I just really don't think I have a good enough understanding of what I'm getting when I call datetime.now() and when I ask for a kitten.date.

I feel like after looking at the documentation for about three hours, I still have no idea how to even begin getting the solution.

I've been trying things like:

now = datetime.now()
then = kitten.date
tdelta = now - then

And:

now = total_seconds(datetime.now())
then = total_seconds(kitten.date)
tdelta = now - then

But in each case, it gives me an unauthorized operator for the - sign.

It seems like datetime.timedelta() should have something to do with it, but I have absolutely no idea how to use that function even after staring at it for hours.

Can someone please help me either: 1. Understand what's going on with the datetime module better or 2. Give me another way to approach my problem?

Thanks

هل كانت مفيدة؟

المحلول

Sorry, I answered my own question. I must have been doing something different last night, but I got it to work tonight. This is what I did:

for kitten in kittens:
    then = kitten.date
    now = datetime.datetime.now()
    tdelta = now - then

if tdelta.total_seconds() > 300:
    kitten.delete()

Should probably have put a static of FIVE_MIN instead of using the magic number 300, so forgive me for that, but it worked.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top