views:

161

answers:

3

I'm pretty new to Python and App Engine, but what I'm trying to do is store a model which contains a DateProperty, and that DateProperty is populated with a Date entered by the user in a web form.

I've got the model of:

class Memory(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateProperty()

and then create an instance with:

memory = Memory()
memory.author = users.get_current_user()
memory.content = self.request.get('content')

But as soon as I try to do anything with the date value, I break it. I'm assuming - and entering - the date value in this format: 2009-07-21

I've tried:

memory.date = time.strptime(self.request.get('date'), '%Y-%m-%d')
memory.date = db.DateProperty(self.request.get('date')) 
memory.date = self.request.get('date') (wishful thinking I guess)

and a few other options I can't even remember now. Everything I try leads to an ImportError with a giant stack trace ending in:

: No module named _multiprocessing args = ('No module named _multiprocessing',) message = 'No module named _multiprocessing'

I have no idea what to make of that.

I come from a PHP world where strtotime() was my magic function that gave me everything I needed for conversions, and the date() function could handle the rest of the formatting. Now I see things with inline lambda (??) functions and what not.

What am I missing on something that would seem to be so simple.

A: 

You were right with strptime:

>>> dt = time.strptime('2009-07-21', '%Y-%m-%d')
>>> dt
    time.struct_time(tm_year=2009, tm_mon=7, tm_mday=21, tm_hour=0, tm_min=0, tm_sec
    =0, tm_wday=1, tm_yday=202, tm_isdst=-1)
>>>

You got struct that can be used by other functions. For example display date in M/D/Y convention:

>>> time.strftime('%m/%d/%Y', dt)
'07/21/2009'

Another example (import datetime module):

>>> dt = datetime.datetime.strptime('2009-07-21', '%Y-%m-%d')
>>> td = datetime.timedelta(days=20)
>>> dt+td
datetime.datetime(2009, 8, 10, 0, 0)
Michał Niklas
for this to work you need to `import time`
Paul Tarjan
Thanks for the response. I'm still not quite seeing how this will help me with initializing my date variable with any user-inputted string (assuming Y-m-d format)
Shane
I do have time being imported. And I should clarify that the strptime function works by itself, but as soon as I try to attach anything to my date variable, that's when things fall apart and I get an ImportError
Shane
A: 

Yeah, PHP's is much nicer. I'm using this library

http://labix.org/python-dateutil

>>> import dateutil.parser
>>> dateutil.parser.parse("may 2 1984")
datetime.datetime(1984, 5, 2, 0, 0)
Paul Tarjan
A: 

perhaps you are using the wrong class? I'm not sure what type your model should take

but try:

from datetime import datetime

myValue = datetime.strptime(self.request.get('date'), '%Y-%m-%d')

I use a datetime object in MySQL with MySQLdb (and a datetime field)

...likewise you can try

from datetime import datetime

myValue = datetime.strptime(self.request.get('date'), '%Y-%m-%d').date()

(notice the [obj].date() at the end)

Terence Honles